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. It records applied migrations in its own table (cql_schema_migrations by default, see MigratorConfig), creating it when missing.

Example Creating a new migrator

migrator = CQL::Migrator.new(schema)

Example Applying migrations

migrator.up

Defined in:

migrations.cr

Constant Summary

Log = CQL.config.logger

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(schema : Schema, config : CQL::MigratorConfig = MigratorConfig.new) #

[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 bootstrap_schema #

Bootstraps the AppSchema.cr file from the current database state. This is useful when starting with an existing database. Example Bootstrapping schema from existing database

migrator.bootstrap_schema

[View source]
def config : MigratorConfig #

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

Rolls back the newest applied migrations, highest version first.

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

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

Rolls back every applied migration newer than version, which stays applied.

  • @param version [Int64] the version to roll back to
  • @raise [Error] if no migration has version 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 [BaseMigration.class | Nil]

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

Returns the pending migrations. It creates the migrations table if it is missing, so where DDL is not transactional (MySQL, CockroachDB) it raises CQL::DialectError inside an open transaction.

  • @return [Array(BaseMigration.class)] 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 given migrations as rolled back.

  • @param m [Array(BaseMigration.class)] the migrations to print
  • @return [Nil] Example Listing rolled back migrations
migrator.print_rolled_back_migrations(CQL::Migrator.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 newest applied migrations.

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

[View source]
def schema : CQL::Schema #

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

Applies the oldest pending migrations, in version order.

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

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

Applies every pending migration up to and including version.

  • @param version [Int64] the version to apply up to
  • @raise [Error] if no migration has version Example Applying to a specific version
migrator.up_to(1_i64)

[View source]
def update_schema_file #

Manually updates the AppSchema.cr file to reflect current database state. Example Manually updating schema file

migrator.update_schema_file

[View source]
def verify_schema_consistency : Bool #

Verifies that the current database state matches the AppSchema.cr file. Example Verifying schema consistency

consistent = migrator.verify_schema_consistency

[View source]