Micrate

repository·master·Indexed 41 days ago

https://github.com/amberframework/micrate

A database migration tool written in Crystal, inspired by goose. It supports Postgres, MySQL, and SQLite3, allowing developers to manage schema changes through versioned SQL files using a CLI or programmatic integration via Crystal.

Tokens
1.3K
Snippets
9
Records
9
Agent score
1%

What's inside Micrate

  1. Handle complex SQL statements with StatementBegin/End

    master

    If your migration contains complex SQL statements that include semicolons (such as PL/pgSQL functions), you must wrap the block in StatementBegin and StatementEnd directives to prevent micrate from incorrectly splitting the script.

    -- +micrate Up
    -- +micrate StatementBegin
    CREATE OR REPLACE FUNCTION example_func() RETURNS void AS $$
    BEGIN
      -- complex logic with semicolons
    END;
    $$ language plpgsql;
    -- +micrate StatementEnd
  2. Create a new SQL migration

    master

    Use the scaffold subcommand to generate a new migration file. This creates a timestamped .sql file in the db/migrations/ directory containing Up and Down sections.

    Example command: micrate scaffold <migration_name>

    Migration files use special comments to define execution blocks:

    • -- +micrate Up: SQL to run when applying the migration.
    • -- +micrate Down: SQL to run when rolling back the migration.
    $ micrate scaffold add_users_table
  3. Install the micrate CLI

    master

    You can install the standalone micrate binary tool via Homebrew or by downloading a release from the GitHub repository. Once installed, micrate connects to your database using the DATABASE_URL environment variable.

    $ brew tap amberframework/micrate
    $ brew install micrate
  4. Integrate micrate into a Crystal application

    master

    To use micrate programmatically within a Crystal project, add it as a dependency in your shard.yml:

    dependencies:
      micrate:
        github: amberframework/micrate

    To run the CLI logic manually (e.g., in a CI runner or a custom script), require the driver you are using and call Micrate::Cli.run after setting the Micrate::DB.connection_url.

    require "micrate"
    require "pg"
    
    Micrate::DB.connection_url = "postgresql://..."
    Micrate::Cli.run
  5. Use the Micrate CLI for database migrations

    master

    Micrate is a database migration management system for Crystal projects. To use the CLI, you must first set the DATABASE_URL environment variable.

    Setup

    export DATABASE_URL=postgres://user:pswd@host:port/database

    Usage

    micrate [options] <subcommand> [subcommand options]
    export DATABASE_URL=postgres://user:pswd@host:port/database
    micrate up
  6. Manage database migrations via CLI

    master

    Use the following commands to control your database schema state:

    • micrate up: Executes all pending migrations.
    • micrate down: Rolls back the last applied migration.
    • micrate redo: Rolls back the last migration and immediately applies it again.
    • micrate status: Shows the state (Applied At or Pending) of all migrations.
    • micrate dbversion: Returns the current version of the database.
    $ micrate up
    $ micrate status
    $ micrate dbversion
    $ micrate down
    $ micrate redo
  7. Micrate CLI command reference

    master

    The following subcommands are available via the micrate CLI:

    CommandDescription
    createCreate the database (permissions required)
    dropDrop the database (permissions required)
    upMigrate the DB to the most recent version available
    downRoll back the version by 1
    redoRe-run the latest migration
    statusDump the migration status for the current DB
    scaffoldCreate the scaffolding for a new migration
    dbversionPrint the current version of the database
    Commands:
        create     Create the database (permissions required)
        drop       Drop the database (permissions required)
        up         Migrate the DB to the most recent version available
        down       Roll back the version by 1
        redo       Re-run the latest migration
        status     Dump the migration status for the current DB
        scaffold   Create the scaffolding for a new migration
        dbversion  Print the current version of the database