class Avram::Migrator::AlterTableStatement

Included Modules

Defined in:

avram/migrator/alter_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_exists : Bool = false) #

[View source]

Macro Detail

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

[View source]
macro add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, fill_existing_with = nil, unique = false, index = true) #

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


[View source]
macro allow_nulls_for(column_name) #

Change the column's nullability from whatever it is currently to true.

alter table_for(User) do
  allow_nulls_for :email
end

DEPRECATED Use make_optional instead


[View source]
macro change_default(type_declaration, default) #

Change the columns' default value to default

alter table_for(Post) do
  change_default published_at : Time, default: :now
end

[View source]
macro change_type(type_declaration, **type_options) #

Change the column's type from whatever it is currently to type_declaration.type. The only exceptions are when changing from text to citext with String using case_sensitive, or changing to a Float64 column which requires setting the precision, and scale.

alter table_for(User) do
  change_type email : String, case_sensitive: false
end

[View source]
macro forbid_nulls_for(column_name) #

Change the column's nullability from whatever it is currently to false.

alter table_for(User) do
  forbid_nulls_for :email
end

DEPRECATED Use make_required instead


[View source]
macro remove(name) #

[View source]
macro remove_belongs_to(association_name) #

[View source]
macro rename(old_name, new_name) #

[View source]
macro rename_belongs_to(old_association_name, new_association_name) #

[View source]
macro symbol_expected_error(action, name) #

[View source]

Instance Method Detail

def add_change_default_statement(column : Avram::Migrator::Columns::Base) #

[View source]
def add_change_type_statement(column : Avram::Migrator::Columns::Base) #

[View source]
def add_fill_existing_with_statements(column : Symbol | String, type, value, nilable) #

[View source]
def alter_statements : Array(String) #

[View source]
def build(&) #

Accepts a block to alter a table using the add method. The generated sql statements are aggregated in the #statements getter.

Usage

built = Avram::Migrator::AlterTableStatement.new(:users).build do
  add name : String
  add age : Int32
  remove old_field
end

built.statements
# => [
"ALTER TABLE users
  ADD name text NOT NULL,
  ADD age int NOT NULL,
  DROP old_field"
]

[View source]
def build_nullability_statement(column_name, nullability) #

[View source]
def change_default_statements : Array(String) #

[View source]
def change_nullability_statements : Array(String) #

[View source]
def change_type_statements : Array(String) #

[View source]
def dropped_rows : Array(String) #

[View source]
def fill_existing_with_statements : Array(String) #

[View source]
def if_exists_statement #

[View source]
def renamed_rows : Array(String) #

[View source]
def rows : Array(String) #

[View source]
def statements #

[View source]