Migration Configuration Example

A comprehensive demonstration of CQL's integrated migration system with automatic schema synchronization, showing how to maintain up-to-date schema files through version-controlled migrations.

🎯 What You'll Learn

This example teaches you how to:

  • Integrate configuration with migration workflows

  • Create automatic schema synchronization between migrations and schema files

  • Manage environment-specific migration configurations

  • Define and execute migrations with automatic schema updates

  • Verify schema consistency between database and schema files

  • Handle migration rollbacks with schema synchronization

  • Use generated schema files in Active Record models

  • Implement team collaboration patterns for schema management

πŸš€ Quick Start

# Run the migration configuration example
crystal examples/configure_migration_example.cr

πŸ“ Code Structure

πŸ”§ Key Features

1. Basic Migration Configuration

2. Schema Creation with Configuration

3. Migration Definition

πŸ—οΈ Migration Workflow Architecture

πŸ“Š Migration Examples

Complete Migration Workflow

Environment-Specific Migration Configurations

πŸ”§ Migration Configuration Options

Core Migration Settings

Option
Type
Default
Description

migration_table_name

Symbol

:cql_schema_migrations

Migration tracking table

schema_path

String

"src/schemas"

Path where schema files are stored

schema_file_name

String

"app_schema.cr"

Default schema file name

schema_constant_name

Symbol

:AppSchema

Schema constant name

enable_auto_schema_sync

Bool

true

Auto schema synchronization

verify_schema_on_startup

Bool

false

Verify schema on startup

Environment-Specific Defaults

Environment
Auto Sync
Schema File
Verify Startup
Bootstrap

Development

true

app_schema.cr

true

false

Test

true

test_schema.cr

false

false

Production

false

production_schema.cr

true

false

🎯 Migration Workflow Patterns

Basic Migration Workflow

Migration Rollback with Schema Sync

Schema Verification

πŸ”§ Configuration Helpers

Using Configuration Helpers

Environment-Specific Helpers

🎯 Team Workflow Scenarios

New Team Member Setup

Resolving Schema Conflicts

Production Deployment

πŸ“Š Generated Schema Files

Schema File Structure

Using Generated Schema in Models

πŸ”§ Advanced Migration Patterns

Complex Migration with Foreign Keys

Adding Columns to Existing Tables

Index Management

🎯 Best Practices

1. Environment-Specific Configuration

2. Migration Naming Conventions

3. Schema File Management

πŸ“š Next Steps

Advanced Topics

Production Considerations

  • Migration Safety - Always test migrations in staging

  • Schema Consistency - Verify schema files match database

  • Rollback Strategy - Ensure migrations can be safely rolled back

  • Team Coordination - Coordinate schema changes across team

  • CI/CD Integration - Automate migration and schema verification

πŸ”§ Troubleshooting

Common Issues

  1. Schema file not generated - Check enable_auto_schema_sync setting

  2. Migration conflicts - Ensure migration versions are sequential

  3. Schema inconsistency - Run CQL.verify_schema(AppDB) to check

  4. Rollback issues - Ensure all migrations have proper down methods

Debug Migration Issues


🏁 Summary

This migration configuration example demonstrates:

  • βœ… Integrated migration workflow with automatic schema synchronization

  • βœ… Environment-specific configurations for different deployment stages

  • βœ… Automatic schema file generation from database migrations

  • βœ… Schema consistency verification between database and files

  • βœ… Team collaboration patterns for schema management

  • βœ… Production-ready deployment with proper migration handling

  • βœ… Configuration helpers for common migration operations

Ready to implement migration workflows in your CQL application? Start with basic migrations and gradually add advanced features like schema synchronization! πŸš€

Last updated

Was this helpful?