Migrations

CQL provides a comprehensive migration system for managing database schema changes over time with automatic schema file synchronization. This integrated workflow ensures your schema files always match your database state, providing compile-time type safety for Active Record models.


What are Migrations?

Migrations are Crystal classes that define changes to your database schema. Each migration has:

  • Version Number: Unique identifier for ordering migrations

  • Up Method: Defines changes to apply

  • Down Method: Defines how to revert the changes

Benefits of Migrations:

  • Version Control: Schema changes are tracked with your code

  • Team Collaboration: Consistent schema across development environments

  • Deployment Safety: Reliable schema updates in production

  • Rollback Capability: Ability to undo problematic changes

  • πŸ†• Automatic Schema Sync: Schema files automatically updated after migrations

  • πŸ†• Type Safety: Compile-time guarantees for Active Record models


πŸ†• Integrated Migration Workflow

CQL now provides an integrated migration system that automatically maintains schema files in sync with your database changes:

Quick Setup

Benefits for Active Record

For a complete guide on this workflow, see Integrated Migration Workflow with Active Record.


Migration Structure

Basic Migration Class

All migrations inherit from CQL::Migration with a version number:

Version Numbers

Use timestamp-based version numbers for proper ordering:


Creating Tables

Table Creation Migration

Note: This approach assumes you've already defined the table structure in your schema. The migration just creates/drops the physical table.

Complete Table Definition Example

If you need to define table structure within the migration:


Altering Tables

Adding Columns

Removing Columns

Renaming Columns

Changing Column Types


Working with Indexes

Adding Indexes

Removing Indexes


Foreign Keys

Adding Foreign Keys

Complex Foreign Key Example


Table Operations

Renaming Tables


Running Migrations

Setting Up the Migrator

πŸ†• Environment-Specific Configurations

Basic Migration Commands

Migration Status

πŸ†• Schema Synchronization Methods


Complete Migration Example

Here's a comprehensive example showing a complete migration workflow:


Integration with Application

Development Workflow

Running from Command Line


Best Practices

Migration Naming

  • Use descriptive names: CreateUsersTable, AddEmailToUsers, RemoveDeprecatedColumns

  • Include timestamp-based version numbers

  • Keep migration files organized in a migrations/ directory

πŸ†• Schema Synchronization Best Practices

Safe Migration Practices

Data Migration Guidelines

Schema Tracking

  • The migration system automatically tracks applied migrations in a schema_migrations table

  • Don't modify or delete migration files after they've been applied in production

  • Use new migrations to make further changes


Troubleshooting

Common Issues

  1. Migration Order: Ensure version numbers are sequential and unique

  2. Rollback Logic: Always provide working down methods

  3. Schema Conflicts: Coordinate with team members on schema changes

  4. Data Dependencies: Be careful when dropping columns that contain data

Recovery from Failed Migrations


The CQL migration system provides a robust way to manage your database schema evolution while maintaining data integrity and team coordination.

Last updated

Was this helpful?