abstract class Cql::Migration
- Cql::Migration
- Reference
- Object
Overview
Migrations are used to manage changes to the database schema over time.
Each migration is a subclass of Migration
and must implement the #up
and #down
methods.
The #up
method is used to apply the migration, while the #down
method is used to rollback the migration.
Migrations are executed in their version order defined.
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 migration
class CreateUsersTable < Cql::Migration
self.version = 1_i64
def up
schema.table :users do
primary :id, Int64
column :name, String
column :email, String
end
end
def down
schema.drop_table :users
end
end
Example Applying migrations
schema = Cql::Schema.build(:northwind, "sqlite3://db.sqlite3") do |s|
s.create_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)
migrator.up
Example Rolling back migrations
migrator.down
Example Redoing migrations
migrator.redo
Example Rolling back to a specific version
migrator.down_to(1_i64)
Example Applying to a specific version
migrator.up_to(1_i64)
Example Listing applied migrations
migrator.print_applied_migrations
Example Listing pending migrations
migrator.print_pending_migrations
Example Listing rolled back migrations
migrator.print_rolled_back_migrations
Example Listing the last migration
migrator.last