Integrated Migration Workflow

This guide demonstrates CQL's integrated migration system that automatically maintains schema files in sync with your database changes, providing a seamless development experience when using the Active Record pattern.

Overview

CQL's integrated workflow combines three powerful components:

  1. Migrations: Version-controlled schema changes

  2. Schema Synchronization: Automatic schema file updates

  3. Active Record Models: Type-safe model definitions

This integration ensures your schema files always reflect the current database state, enabling compile-time type safety and seamless team collaboration.


Quick Start Example

Here's a complete example showing the integrated workflow:

# 1. Configure the integrated migrator
config = CQL::MigratorConfig.new(
  schema_file_path: "src/schemas/app_schema.cr",
  schema_name: :AppSchema,
  schema_symbol: :app_schema,
  auto_sync: true  # Automatically update schema file after migrations
)

# 2. Initialize your base schema connection
AppDB = CQL::Schema.define(
  :app_database,
  adapter: CQL::Adapter::Postgres,
  uri: ENV["DATABASE_URL"]
) do
  # Tables will be managed by migrations
end

migrator = AppDB.migrator(config)

# 3. Create migrations
class CreateUsersTable < CQL::Migration(1)
  def up
    schema.table :users do
      primary :id, Int32
      column :name, String
      column :email, String
      timestamps
    end
    schema.users.create!
  end

  def down
    schema.users.drop!
  end
end

# 4. Run migrations - schema file automatically updated!
migrator.up

# 5. Use the auto-generated schema in your Active Record models
require "./src/schemas/app_schema"

class User
  include CQL::ActiveRecord::Model(Int32)
  db_context AppSchema, :users

  property id : Int32?
  property name : String
  property email : String
  property created_at : Time?
  property updated_at : Time?
end

# 6. Now you have type-safe Active Record operations
user = User.create!(name: "Alice", email: "alice@example.com")
puts user.id  # Compile-time type safety guaranteed!

Configuration Setup

Basic Configuration

Environment-Specific Configurations


Migration Development Workflow

1. Initial Bootstrap (Existing Database)

Start with an existing database by bootstrapping your schema file:

Generated AppSchema.cr:

2. Creating Models from Generated Schema

Once you have the schema file, create Active Record models:

3. Adding New Features with Migrations

Create migrations to add new features:

4. Updated Models After Migration

After running the migration, update your models to use the new schema:


Advanced Migration Patterns

Complex Schema Changes

Adding Indexes for Performance


Team Collaboration Workflow

1. Developer Workflow

2. Code Review Process

3. Deployment Workflow


Integration with Active Record Features

Relationships with Generated Schema

Validations Based on Schema

Scopes with Schema Awareness


Manual Schema Operations

Manual Schema Updates

Schema Inspection


Best Practices

1. Version Control Integration

2. CI/CD Pipeline Integration

3. Development Environment Setup

4. Model Generation


Troubleshooting

Schema File Out of Sync

Migration Conflicts

Model Compilation Errors


Conclusion

The integrated migration workflow provides:

  • Automatic Schema Synchronization: No manual schema file maintenance

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

  • Team Coordination: Consistent schema files across team members

  • Production Safety: Manual control options for deployment

  • Developer Experience: Seamless workflow from migration to model usage

This workflow eliminates schema drift, reduces manual errors, and provides a smooth development experience when building applications with the Active Record pattern in CQL.

Last updated

Was this helpful?