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
endMigration 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
Schema Definition - Define initial schema
Models - Map models to migrated tables
Last updated
Was this helpful?
