Migration Best Practices
Migrations in CQL provide a structured way to evolve your database schema over time. They allow you to create, modify, and drop tables and columns in a versioned, reversible manner using Crystal code.
🆕 Integrated Workflow: CQL now provides automatic schema file synchronization with migrations. For the complete Active Record integration guide, see Integrated Migration Workflow.
What is a Migration?
A migration is a Crystal class that defines changes to your database schema. Each migration has a unique version number and two methods:
up: Applies the migration (e.g., creates or alters tables).down: Reverts the migration (e.g., drops or reverts changes).
Creating a Migration
Create a new migration by subclassing CQL::Migration and setting a unique version:
class CreateUsers < CQL::Migration
self.version = 1_i64
def up
schema.create :users do
primary_key :id, :serial
column :name, :string
column :email, :string
column :active, :bool, default: false
column :created_at, :timestamp
column :updated_at, :timestamp
end
end
def down
schema.drop :users
end
endself.versionmust be unique and increasing (often a timestamp or integer).The
upmethod defines the schema changes to apply.The
downmethod defines how to revert those changes.
Running Migrations
🆕 Modern Approach: Integrated Workflow
Traditional Approach
You can still use the traditional approach without automatic schema synchronization:
Modifying Tables
You can alter existing tables in a migration:
Best Practices
Use a unique, increasing version for each migration.
Write reversible migrations (always provide a
downmethod).Keep migrations in version control.
Test migrations on a development database before running in production.
Use descriptive class names (e.g.,
AddAgeToUsers).
Example: Multiple Migrations
For more on defining models and querying, see the other guides in this directory.
Last updated
Was this helpful?