class

Avram::Migrator::CreateTableStatement

Inherits Avram::Migrator::MissingOnDeleteWithBelongsToError < Avram::Migrator::IndexStatementHelpers < Reference < Object

Constructors

new(table_name : TableName, *, if_not_exists : Bool = false)
Source

Instance methods

build

Accepts a block to build a table and indices using add and add_index methods.

The generated sql statements are aggregated in the statements method.

Usage

built = Avram::Migrator::CreateTableStatement.new(:users).build do
  add_belongs_to Account, on_delete: :cascade
  add :email : String, unique: true
end

built.statements
# => [
  "CREATE TABLE users (
    id serial PRIMARY KEY,
    created_at timestamptz NOT NULL,
    updated_at timestamptz NOT NULL,
    account_id bigint NOT NULL REFERENCES accounts (id) ON DELETE CASCADE,
    email text NOT NULL);",
  "CREATE UNIQUE INDEX users_email_index ON users USING btree (email);"
]

An optional second argument can toggle between the usage of a numeric or uuid based id column.

built = Avram::Migrator::CreateTableStatement.new(:users, PrimaryKeyType::UUID).build do
  add :email : String, unique: true
end

Partitioning can be specified by passing a partition_by option to the block.

built = Avram::Migrator::CreateTableStatement.new(:events).build do
  add id : Int64
  add type : Int32
  add :message : String
  add_timestamps

  composite_primary_key :id, :created_at
  partition_by :created_at, type: :range
end
Source
partition
Source
partition=(partition : String | Nil)
Source
statements
Source

Macros

add(type_declaration, default = nil, index = false, unique = false, using = :btree, **type_options)
Source
add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, unique = false, index = true)

Adds a references column and index given a model class and references option.

Source
add_timestamps
Source
belongs_to(type_declaration, *args, **named_args)
Source
composite_primary_key(*columns)
Source
partition_by(column, type)
Source
primary_key(type_declaration)
Source