Migration Generator
The Migration Generator creates CQL-based database migration files that use type-safe Schema DSL for defining schema changes.
Usage
azu generate migration MIGRATION_NAME [attributes] [OPTIONS]Description
Migrations in Azu applications use CQL's powerful migration system to version control database schema changes. They provide:
Type-Safe Migrations: Crystal classes with compile-time type checking
Automatic Schema Sync: Schema file automatically updates after migrations
CQL Schema DSL: Expressive syntax for defining tables, columns, and indexes
Version Tracking: Int64 timestamp-based version numbers
Rollback Support: Reversible migrations with
upanddownmethods
Migrations inherit from CQL::Migration(VERSION) and use CQL's Schema DSL for all database operations.
Options
MIGRATION_NAME- Name of the migration to generate (required)attributes- Column definitions inname:typeformat (optional)--timestamps- Add timestamps (default: true)-f, --force- Overwrite existing files-h, --help- Show help message
Examples
Generate a migration with attributes
This creates:
src/db/migrations/20240115103045_create_users.cr- The migration file
Generate a simple migration
Generate migration with foreign keys
Generate migration with various types
Generated Files
Migration File (src/db/migrations/TIMESTAMP_create_table_name.cr)
src/db/migrations/TIMESTAMP_create_table_name.cr)The generator creates a properly formatted CQL migration using the Schema DSL:
Migration Patterns
Create Table Migration
Add Column Migration
Create Index Migration
Create Table with Foreign Key
Create Join Table Migration
Column Types
Supported Column Types (CQL Schema DSL)
The generator supports the following attribute type mappings:
string, text
String
Text data
int32, integer
Int32
32-bit integers
int64
Int64
64-bit integers (default for primary)
float32
Float32
32-bit floating point
float64, float
Float64
64-bit floating point
bool, boolean
Bool
Boolean values (true/false)
datetime, time
Time
Date and time
date
Date
Date only
email
String
Email (with unique index auto-generated)
url
String
URL string
json
JSON::Any
JSON data
uuid
UUID
UUID values
references
Int64
Foreign key reference
CQL Schema DSL Syntax
Adding Indexes
Indexes are added using schema.alter:
Index Types
Single Column Indexes
Multi-Column Indexes
Partial Indexes
Running Migrations
Run All Migrations
Run Specific Number of Migrations
Migrate to Specific Version
Rollback Migrations
View Verbose Output
How Migrations Run
When you execute azu db:migrate, the CLI:
Creates a temporary Crystal script
Loads
src/db/schema.crand all migration files fromsrc/db/migrations/*.crInitializes CQL's
Migratorwith auto-sync enabledRuns pending migrations using
migrator.upAutomatically updates
src/db/schema.crto reflect current database stateCleans up temporary files
The schema file (src/db/schema.cr) is always kept in sync with your database!
Best Practices
1. Use Descriptive Names
2. Keep Migrations Focused
Each migration should make one logical change:
3. Always Provide Rollback Logic
4. Use Data Types Appropriately
5. Add Indexes for Performance
Testing Migrations
Unit Testing
Integration Testing
Common Migration Patterns
1. Table Creation
2. Adding Foreign Keys
3. Data Migration
4. Schema Changes
Related Commands
azu db:migrate- Run database migrationsazu db:rollback- Rollback database migrationsazu db:create- Create the databaseazu db:drop- Drop the databaseazu db:reset- Reset the databaseazu db:setup- Setup database (create and migrate)azu generate model- Generate CQL data modelsazu generate scaffold- Generate full CRUD scaffold
Key Differences from Other ORMs
CQL vs ActiveRecord/Eloquent
Traditional ORMs:
CQL Migrations:
Key Benefits:
✅ Type-safe at compile time
✅ Automatic schema file synchronization
✅ Int64 timestamp versions (no collisions)
✅ Crystal's performance benefits
✅ Explicit
up/downmethods (no magicchange)
Additional Resources
Migration System Guide: ../../MIGRATION_FIXES_SUMMARY.md
CQL Documentation: ../integration/cql-orm.md
Database Commands: ../commands/database.md
CQL GitHub: https://github.com/azutoolkit/cql
CQL Examples: https://github.com/azutoolkit/cql/tree/master/examples/migrations
Last updated