class Jennifer::Migration::TableBuilder::ChangeTable

Overview

Component responsible for altering existing table based on specified columns and properties.

change_table(:contacts) do |t|
  t.rename_table :users
  t.add_column :name, :string, {:size => 30}
  t.drop_column :age
end

Defined in:

jennifer/migration/table_builder/change_table.cr

Constructors

Instance Method Summary

Instance methods inherited from class Jennifer::Migration::TableBuilder::Base

adapter : Adapter::Base adapter, column_exists?(*args, **options)
column_exists?(*args, **options, &)
column_exists?
, explain explain, index_exists?(*args, **options)
index_exists?(*args, **options, &)
index_exists?
, name : String name, process process, process_commands process_commands, schema_processor(*args, **options)
schema_processor(*args, **options, &)
schema_processor
, table_exists?(*args, **options)
table_exists?(*args, **options, &)
table_exists?

Constructor methods inherited from class Jennifer::Migration::TableBuilder::Base

new(adapter, name : String | Symbol) new

Constructor Detail

def self.new(adapter, name) #

[View source]

Instance Method Detail

def add_column(name : String | Symbol, type : Symbol? = nil, options : Hash(Symbol, AAllowedTypes) = DbOptions.new) #

Defines new column name of type with given options.

The type argument should be one of the following supported data types (see Jennifer::Adapter::TYPES).

You may use any type that isn't in this list as long as it is supported by your database by skipping type and passing sql_type option.

Available options are (none of these exists by default):

  • :array - creates and array of given type;
  • :serial - makes column SERIAL;
  • :sql_type - allow to specify custom SQL data type;
  • :size - requests a maximum column length; e.g. this is a number of characters in string column and number of bytes for text or integer;
  • :null - allows or disallows NULL values;
  • :primary - adds primary key constraint to the column; ATM only one field may be a primary key;
  • :default - the column's default value;
  • :auto_increment - add autoincrement to the column.
add_column :picture, :blob
add_column :status, :string, {:size => 20, :default => "draft", :null => false}
add_column :skills, :text, {:array => true}

[View source]
def add_foreign_key(to_table : String | Symbol, column = nil, primary_key = nil, name = nil, *, on_update : Symbol = DEFAULT_ON_EVENT_ACTION, on_delete : Symbol = DEFAULT_ON_EVENT_ACTION) #

Creates a foreign key constraint to to_table table.

For more details see Migration::Base#add_foreign_key.


[View source]
def add_index(fields : Array(Symbol), type : Symbol? = nil, name : String? = nil, lengths : Hash(Symbol, Int32) = {} of Symbol => Int32, orders : Hash(Symbol, Symbol) = {} of Symbol => Symbol) #

Adds new index.

For more details see Migration::Base#add_index


[View source]
def add_index(field : Symbol, type : Symbol? = nil, name : String? = nil, length : Int32? = nil, order : Symbol? = nil) #

[View source]
def add_reference(name, type : Symbol = :integer, options : Hash(Symbol, AAllowedTypes) = DbOptions.new) #

Adds a reference.

The reference column is an integer by default, type argument can be used to specify a different type.

If polymorphic option is true - additional string field "#{name}_type" is created and foreign key is not added.

:to_table, :column, :primary_key and :key_name options are used to create a foreign key constraint. See Migration::Base#add_foreign_key for details.

add_reference :user
add_reference :order, :bigint
add_reference :taggable, {:polymorphic => true}

[View source]
def add_timestamps(null : Bool = false) #

Add created_at and updated_at timestamp columns.

Argument null sets :null option for both columns.


[View source]
def change_column(name : String | Symbol, type : Symbol? = nil, options : Hash(Symbol, AAllowedTypes) = DbOptions.new) #

Changes the column's definition according to the new options.

See #add_column for details of the options you can use.

Additional available options:

  • :new_name - specifies a new name for the column;
  • :default - :none value drops any default value specified before.
change_column :description, {:new_name => "information"}

change_column :price, {:default => :none}

[View source]
def changed_columns : Hash(String, Hash(Symbol, Array(Bool | Float32 | Float64 | Int32 | Int64 | JSON::Any | String | Symbol | Nil) | Bool | Float32 | Float64 | Int32 | Int64 | JSON::Any | String | Symbol | Nil)) #

[View source]
def drop_column(name : String | Symbol) #

Drops column with given name.


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

[View source]
def drop_foreign_key(to_table : String | Symbol, column = nil, name = nil) #

Drops foreign key of to_table.

For more details see Migration::Base#drop_foreign_key.


[View source]
def drop_index(fields : Array(Symbol) = [] of Symbol, name : String? = nil) #

Drops the index from the table.

For more details see Migration::Base#drop_index.


[View source]
def drop_index(field : Symbol? = nil, name : String? = nil) #

[View source]
def drop_reference(name, options : Hash(Symbol, AAllowedTypes) = DbOptions.new) #

Drops the reference.

options can include :polymorphic, :to_table and :column options. For more details see #add_reference.


[View source]
def explain #
Description copied from class Jennifer::Migration::TableBuilder::Base

Returns string presentation of invoked changes.


[View source]
def new_table_name : String #

[View source]
def process #
Description copied from class Jennifer::Migration::TableBuilder::Base

Invokes current command.


[View source]
def rename_table(new_name : String | Symbol) #

Renames table to the given new_name.

rename_table :users

[View source]