class Avram::Migrator::CreateTableStatement

Included Modules

Defined in:

avram/migrator/create_table_statement.cr

Constructors

Macro Summary

Instance Method Summary

Macros inherited from module Avram::Migrator::MissingOnDeleteWithBelongsToError

add_belongs_to(type_declaration, references = nil) add_belongs_to

Instance methods inherited from module Avram::Migrator::IndexStatementHelpers

add_index(column : Symbol, unique = false, using : Symbol = :btree) add_index, index_added?(index : String, column : Symbol) index_added?

Instance methods inherited from class Object

blank_for_validates_required? : Bool blank_for_validates_required?

Constructor Detail

def self.new(table_name : TableName, *, if_not_exists : Bool = false) #

[View source]

Macro Detail

macro add(type_declaration, default = nil, index = false, unique = false, using = :btree, **type_options) #

[View source]
macro 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.


[View source]
macro add_timestamps #

[View source]
macro belongs_to(type_declaration, *args, **named_args) #

[View source]
macro composite_primary_key(*columns) #

[View source]
macro primary_key(type_declaration) #

[View source]

Instance Method Detail

def build(&) : CreateTableStatement #

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

[View source]
def statements #

[View source]