mg 0.5.0

A minimal database migration tool for Crystal

MG

A minimal database migration tool for Crystal.

Installation

  1. Add the dependency to your shard.yml:
dependencies:
 mg:
   github: hkalexling/mg
  1. Run shards install

Usage

First define some database versions by inheriting from MG::Base. Here are two examples:

# migration/users.1.cr
class CreateUser < MG::Base
  def up : String
    <<-SQL
    CREATE TABLE users (
     username TEXT NOT NULL,
     password TEXT NOT NULL,
     email TEXT NOT NULL
    );
    SQL
  end

  def down : String
    <<-SQL
    DROP TABLE users;
    SQL
  end

  # Optional lifecycle method to be executed after the `up` query.
  def after_up(conn : DB::Connection)
    puts "Table users created"
  end

  # Optional lifecycle method to be executed after the `down` query.
  def after_down(conn : DB::Connection)
    puts "Table users dropped"
  end
end
# migration/users_index.2.cr
class UserIndex < MG::Base
  def up : String
    <<-SQL
    CREATE UNIQUE INDEX username_idx ON users (username);
    CREATE UNIQUE INDEX email_idx ON users (email);
    SQL
  end

  def down : String
    <<-SQL
    DROP INDEX username_idx;
    DROP INDEX email_idx;
    SQL
  end
end

Note that the migration files must be named as [filename].[non-negative-version-number].cr.

Now require the relevant files and the migrations in your application code, and start the migration.

require "mg"
require "sqlite3"
require "./migration/*"

Log.setup "mg", :debug
DB.open "sqlite3://file.db" do |db|
  mg = MG::Migration.new db

  # Migrates to the latest version (in our case, 2)
  mg.migrate

  # Migrates to a specific version
  mg.migrate to: 1

  # Migrates down to version 0
  mg.migrate to: 0

  # Returns the current version
  puts mg.user_version # 0
end

Contributors

Repository

mg

Owner
Statistic
  • 8
  • 1
  • 0
  • 8
  • 3
  • over 2 years ago
  • January 16, 2021
License

MIT License

Links
Synced at

Thu, 28 Mar 2024 19:19:44 GMT

Languages