class Cql::Migrator

Overview

The Migrator class is used to manage migrations and provides methods to apply, rollback, and redo migrations. The Migrator class also provides methods to list applied and pending migrations. Example Creating a new migrator

schema = Cql::Schema.define(:northwind, "sqlite3://db.sqlite3") do |s|
  table :schema_migrations do
    primary :id, Int32
    column :name, String
    column :version, Int64, index: true, unique: true
    timestamps
  end
end
migrator = Cql::Migrator.new(schema)

Example Applying migrations

migrator.up

Defined in:

migrations.cr

Constant Summary

Log = ::Log.for(self)

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(schema : Schema) #

[View source]

Class Method Detail

def self.migrations : Array(BaseMigration.class) #

[View source]
def self.migrations=(migrations : Array(BaseMigration.class)) #

[View source]

Instance Method Detail

def applied_migrations : Array(MigrationRecord) #

Returns the applied migrations.

  • @return [Array(MigrationRecord)] Example Listing applied migrations
migrator.applied_migrations

[View source]
def down(steps : Int32 = Migrator.migrations.size) #

Rolls back the last migration.

  • @param steps [Int32] the number of migrations to roll back (default: 1) Example Rolling back migrations
migrator.down

[View source]
def down_to(version : Int64) #

Rolls back to a specific migration version.

  • @param version [Int64] the version to roll back to Example Rolling back to a specific version
migrator.down_to(1_i64)

[View source]
def last : BaseMigration.class | Nil #

Returns the last migration. Example Listing the last migration

migrator.last

@return [Migration.class | Nil]


[View source]
def pending_migrations : Array(BaseMigration.class) #

Returns the pending migrations.

  • @return [Array(MigrationRecord)] Example Listing pending migrations
migrator.pending_migrations

[View source]
def print_applied_migrations #

Prints the applied migrations. Example Listing applied migrations

migrator.print_applied_migrations

[View source]
def print_pending_migrations #

Prints the pending migrations. Example Listing pending migrations

migrator.print_pending_migrations

[View source]
def print_rolled_back_migrations(m : Array(BaseMigration.class)) #

Prints the rolled back migrations.

  • @param m [Array(Migration.class)] the migrations to print
  • @return [Nil] Example Listing rolled back migrations
migrator.print_rolled_back_migrations

[View source]
def redo #

Redoes the last migration. Example Redoing migrations

migrator.redo

[View source]
def repo : Repository(MigrationRecord, Int32) #

[View source]
def rollback(steps : Int32 = 1) #

Rolls back the last migration.

  • @param steps [Int32] the number of migrations to roll back (default: 1) Example Rolling back migrations
migrator.rollback

[View source]
def schema : Schema #

[View source]
def up(steps : Int32 = Migrator.migrations.size) #

Applies the pending migrations.

  • @param steps [Int32] the number of migrations to apply (default: all) Example Applying migrations
migrator.up

[View source]
def up_to(version : Int64) #

Applies migrations up to a specific version.

  • @param version [Int64] the version to apply up to Example Applying to a specific version
migrator.up_to(1_i64)

[View source]