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 up and down methods

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 in name:type format (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)

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:

Generator Type
Crystal Type
Usage

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:

  1. Creates a temporary Crystal script

  2. Loads src/db/schema.cr and all migration files from src/db/migrations/*.cr

  3. Initializes CQL's Migrator with auto-sync enabled

  4. Runs pending migrations using migrator.up

  5. Automatically updates src/db/schema.cr to reflect current database state

  6. Cleans 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

  • azu db:migrate - Run database migrations

  • azu db:rollback - Rollback database migrations

  • azu db:create - Create the database

  • azu db:drop - Drop the database

  • azu db:reset - Reset the database

  • azu db:setup - Setup database (create and migrate)

  • azu generate model - Generate CQL data models

  • azu 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/down methods (no magic change)

Additional Resources

Last updated