Migrations

Migrations manage database schema changes in a version-controlled, reversible way.

Creating Migrations

Migrations are numbered classes with up and down methods:

# db/migrations/001_create_users.cr
class CreateUsers < CQL::Migration(1)
  def up
    schema.create :users do
      primary :id, Int64, auto_increment: true
      text :name, null: false
      text :email, null: false
      boolean :active, default: "true"
      timestamps

      index :email, unique: true
    end
  end

  def down
    schema.users.drop!
  end
end

Migration Methods

Creating Tables

Dropping Tables

Adding Columns

Removing Columns

Adding Indexes

Renaming Columns

Changing Column Types

Running Migrations

Migration Best Practices

Reversible Migrations

Always implement both up and down:

Data Migrations

Migrate data alongside schema changes:

Add Columns as Nullable

Complete Example

Next Steps

Last updated

Was this helpful?