### Install Micrate using Homebrew Source: https://github.com/amberframework/micrate/blob/master/README.md Install the standalone binary tool for Micrate using Homebrew by tapping the repository and installing the package. ```bash $ brew tap amberframework/micrate $ brew install micrate ``` -------------------------------- ### Example SQL Migration with Create and Drop Table Source: https://github.com/amberframework/micrate/blob/master/README.md An example SQL migration demonstrating how to create a 'users' table in the 'Up' section and drop it in the 'Down' section. ```sql -- +micrate Up CREATE TABLE users(id INT PRIMARY KEY, email VARCHAR NOT NULL); -- +micrate Down DROP TABLE users; ``` -------------------------------- ### SQL Migration with Statement Delimiters Source: https://github.com/amberframework/micrate/blob/master/README.md Example of a complex SQL migration using '+micrate StatementBegin' and '+micrate StatementEnd' to handle multi-statement scripts, such as creating a partitioned table function. ```sql -- +micrate Up -- +micrate StatementBegin CREATE OR REPLACE FUNCTION histories_partition_creation( DATE, DATE ) returns void AS $$ DECLARE create_query text; BEGIN FOR create_query IN SELECT 'CREATE TABLE IF NOT EXISTS histories_' || TO_CHAR( d, 'YYYY_MM' ) || ' ( CHECK( created_at >= timestamp ''' || TO_CHAR( d, 'YYYY-MM-DD 00:00:00' ) || ''' AND created_at < timestamp ''' || TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD 00:00:00' ) || ''' ) ) inherits ( histories );' FROM generate_series( $1, $2, '1 month' ) AS d LOOP EXECUTE create_query; END LOOP; -- LOOP END END; -- FUNCTION END $$ language plpgsql; -- +micrate StatementEnd ``` -------------------------------- ### Apply Pending Migrations Source: https://github.com/amberframework/micrate/blob/master/README.md Command to apply all pending database migrations using Micrate. Shows example output indicating successful migration. ```bash $ micrate up Migrating db, current version: 0, target: 20160524162947 OK 20160524162446_add_users_table.sql ``` -------------------------------- ### Crystal Runner Script for Micrate CLI Source: https://github.com/amberframework/micrate/blob/master/README.md Example Crystal script to build a standalone command-line client using Micrate. Requires the desired database driver and uses Micrate::Cli.run. ```crystal #! /usr/bin/env crystal # # To build a standalone command line client, require the # driver you wish to use and use `Micrate::Cli`. # require "micrate" require "pg" Micrate::DB.connection_url = "postgresql://..." Micrate::Cli.run ``` -------------------------------- ### Check Current Database Version Source: https://github.com/amberframework/micrate/blob/master/README.md Command to retrieve the current version of the database migrations applied by Micrate. ```bash $ micrate dbversion 20160524162446 ``` -------------------------------- ### Basic SQL Migration Structure Source: https://github.com/amberframework/micrate/blob/master/README.md Defines the basic structure for a Micrate SQL migration file, using '+micrate Up' and '+micrate Down' directives to specify SQL for applying and rolling back. ```sql -- +micrate Up -- SQL in section 'Up' is executed when this migration is applied -- +micrate Down -- SQL section 'Down' is executed when this migration is rolled back ``` -------------------------------- ### Redo Last Migration Source: https://github.com/amberframework/micrate/blob/master/README.md Command to roll back and then re-apply the last database migration using Micrate. ```bash $ micrate redo ``` -------------------------------- ### Check Migration Status Source: https://github.com/amberframework/micrate/blob/master/README.md Command to display the status of all migrations, indicating which have been applied and which are pending. ```bash $ micrate status Applied At Migration ======================================= 2016-05-24 16:31:07 UTC -- 20160524162446_add_users_table.sql Pending -- 20160524163425_add_address_to_users.sql ``` -------------------------------- ### Add Micrate Dependency to Shard.yml Source: https://github.com/amberframework/micrate/blob/master/README.md Specifies how to add Micrate as a dependency to a Crystal project's shard.yml file for programmatic usage. ```yaml dependencies: micrate: github: amberframework/micrate ``` -------------------------------- ### Rollback Last Migration Source: https://github.com/amberframework/micrate/blob/master/README.md Command to roll back the most recently applied database migration using Micrate. ```bash $ micrate down ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.