class
CQL::Migrator
- CQL::Migrator
- Reference
- Object
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.crConstant Summary
-
Log =
CQL.config.logger
Constructors
Class Method Summary
Instance Method Summary
-
#applied_migrations : Array(MigrationRecord)
Returns the applied migrations.
-
#bootstrap_schema
Bootstraps the AppSchema.cr file from the current database state.
- #config : MigratorConfig
-
#down(steps : Int32 = Migrator.migrations.size)
Rolls back the newest applied migrations, highest version first.
-
#down_to(version : Int64)
Rolls back every applied migration newer than version, which stays applied.
-
#last : BaseMigration.class | Nil
Returns the last migration.
-
#pending_migrations : Array(BaseMigration.class)
Returns the pending migrations.
-
#print_applied_migrations
Prints the applied migrations.
-
#print_pending_migrations
Prints the pending migrations.
-
#print_rolled_back_migrations(m : Array(BaseMigration.class))
Prints the given migrations as rolled back.
-
#redo
Redoes the last migration.
- #repo : Repository(MigrationRecord, Int32)
-
#rollback(steps : Int32 = 1)
Rolls back the newest applied migrations.
- #schema : CQL::Schema
-
#up(steps : Int32 = Migrator.migrations.size)
Applies the oldest pending migrations, in version order.
-
#up_to(version : Int64)
Applies every pending migration up to and including version.
-
#update_schema_file
Manually updates the AppSchema.cr file to reflect current database state.
-
#verify_schema_consistency : Bool
Verifies that the current database state matches the AppSchema.cr file.
Constructor Detail
Class Method Detail
Instance Method Detail
Returns the applied migrations.
- @return [Array(MigrationRecord)] Example Listing applied migrations
migrator.applied_migrations
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
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
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)
Returns the last migration. Example Listing the last migration
migrator.last
- @return [BaseMigration.class | Nil]
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
Prints the applied migrations. Example Listing applied migrations
migrator.print_applied_migrations
Prints the pending migrations. Example Listing pending migrations
migrator.print_pending_migrations
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)
Rolls back the newest applied migrations.
- @param steps [Int32] the number of migrations to roll back (default: 1) Example Rolling back migrations
migrator.rollback
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
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)
Manually updates the AppSchema.cr file to reflect current database state. Example Manually updating schema file
migrator.update_schema_file
Verifies that the current database state matches the AppSchema.cr file. Example Verifying schema consistency
consistent = migrator.verify_schema_consistency