Migration DSL

Complete reference for the CQL Migration DSL (Domain Specific Language).

Migration Class

Migrations inherit from CQL::Migration(VERSION) where VERSION is an integer (typically a timestamp):

class CreateUsers < CQL::Migration(20250125001401)
  def up
    # Apply changes
  end

  def down
    # Rollback changes
  end
end

Creating Tables

schema.table

Defines a new table structure:

class CreateUsers < CQL::Migration(1)
  def up
    schema.table :users do
      primary :id, Int64, auto_increment: true
      column :name, String, null: false
      column :email, String, null: false
      column :active, Bool, default: true
      timestamps

      index [:email], unique: true
    end

    schema.users.create!
  end

  def down
    schema.users.drop!
  end
end

Key methods:

  • schema.table :name do ... end - Define table structure

  • schema.table_name.create! - Execute CREATE TABLE

  • schema.table_name.drop! - Execute DROP TABLE

Column Methods

primary

Defines the primary key column:

Parameters:

  • name - Column name (Symbol)

  • type - Crystal type (Int32, Int64, String)

  • auto_increment - Enable auto-increment (default: true)

column

Adds a column to the table:

Options:

Option
Type
Description

null

Bool

Allow NULL values (default: false)

default

T

Default value

unique

Bool

Add unique constraint

index

Bool

Create index on column

size

Int32

Column size (for VARCHAR)

as

String

SQL column alias

Type-Specific Column Methods

CQL provides helper methods for common column types:

Supported Types

Crystal Type
SQL Type

Int32

INTEGER

Int64

BIGINT

Float32

FLOAT

Float64

DOUBLE PRECISION

String

VARCHAR / TEXT

Bool

BOOLEAN

Time

TIMESTAMP

Date

DATE

JSON::Any

JSON / JSONB

timestamps

Adds created_at and updated_at columns:

Both columns are timestamps with current timestamp as default.

Index Methods

index

Creates an index within a table definition:

create_index / drop_index

Manage indexes in alter operations:

Foreign Key Methods

foreign_key

Creates a foreign key constraint:

Parameters:

Parameter
Type
Description

columns

Array(Symbol)

Local column(s)

references

Symbol

Target table

references_columns

Array(Symbol)

Target column(s)

on_delete

Symbol

:cascade, :restrict, :set_null, :no_action

on_update

Symbol

:cascade, :restrict, :set_null, :no_action

name

String

Optional constraint name

Composite Foreign Keys

Altering Tables

schema.alter

Modifies an existing table:

Alter Operations

Method
Description

add_column :name, Type, options

Add a new column

drop_column :name

Remove a column

rename_column :old, :new

Rename a column

change_column :name, NewType

Change column type

create_index :name, [:cols]

Create an index

drop_index :name

Drop an index

foreign_key [:cols], references: :table

Add foreign key

drop_foreign_key :name

Drop foreign key

rename_table :new_name

Rename the table

unique_constraint [:cols]

Add unique constraint

check_constraint "expression"

Add check constraint

Alter Examples

Complete Example

Migration Execution

See Also

Last updated

Was this helpful?