Configure Migration Integration

This guide demonstrates the powerful integration between CQL's centralized configuration system and the migration workflow with automatic schema generation.

Overview

The integrated system provides:

  • Centralized Configuration: All database and migration settings in one place

  • Environment-Aware Migrations: Automatic environment-specific migration behavior

  • Auto Schema Sync: Schema files automatically generated and updated

  • Type-Safe Models: Compile-time safety for Active Record models

  • Team Collaboration: Consistent schema synchronization across team members

Quick Start

1. Configure CQL with Migration Settings

require "cql"

CQL.configure do |config|
  # Database settings
  config.database_url = "postgresql://localhost/myapp_development"
  config.pool_size = 10

  # Migration and schema settings
  config.schema_path = "src/schemas"
  config.schema_file_name = "app_schema.cr"
  config.schema_constant_name = :AppSchema
  config.enable_auto_schema_sync = true
  config.verify_schema_on_startup = true
end

2. Create Schema Using Configuration

3. Define Migrations

4. Run Migrations with Auto Schema Sync

5. Use Generated Schema in Active Record Models

Environment-Specific Configuration

Development Environment

Test Environment

Production Environment

Advanced Features

Bootstrap from Existing Database

Schema Verification and Auto-Fix

Helper Methods

Configuration Options

Migration-Specific Options

Option
Type
Default
Description

enable_auto_schema_sync

Bool

true

Enable automatic schema file synchronization

schema_file_name

String

"app_schema.cr"

Schema file name (without path)

schema_constant_name

Symbol

:AppSchema

Schema constant name in generated file

schema_symbol

Symbol

:app_schema

Schema symbol for internal use

bootstrap_on_startup

Bool

false

Bootstrap schema on first run

verify_schema_on_startup

Bool

false

Verify schema consistency on startup

Example Configuration

Complete Workflow Example

Here's a complete example from configuration to model usage:

Benefits

For Developers

  • Single Configuration Point: All database settings in one place

  • Type Safety: Compile-time guarantees for model properties

  • Auto-Generated Code: Schema files maintained automatically

  • Environment Awareness: Smart defaults per environment

For Teams

  • Consistent Setup: Same configuration across all team members

  • Version Control: Schema changes tracked with migrations

  • Easy Onboarding: New team members get schema automatically

  • Conflict Resolution: Automatic schema file updates

For Production

  • Manual Control: Disable auto-sync in production for safety

  • Verification: Built-in schema consistency checking

  • Rollback Safety: Full migration rollback with schema sync

  • Performance: Optimized for production workloads

Migration Commands

Best Practices

1. Environment-Specific Configuration

2. Migration Organization

3. Schema File Management

4. Testing Strategy

Troubleshooting

Schema File Not Generated

Schema Inconsistency

Migration Errors


Examples

For complete working examples, see:

  • examples/configure_migration_example.cr - Full integration demonstration

  • examples/schema_migration_workflow.cr - SQLite workflow example

  • examples/schema_migration_workflow_pg.cr - PostgreSQL workflow example

This integration provides a powerful, type-safe, and developer-friendly approach to database management in Crystal applications using CQL.

Last updated

Was this helpful?