class ActiveRecord::ConnectionAdapters::Table
end
t.remove_timestamps
t.remove_check_constraint
t.remove_index
t.remove_belongs_to
t.remove_references
t.remove_foreign_key
t.remove
t.virtual
t.json
t.foreign_key
t.boolean
t.blob
t.binary
t.date
t.time
t.timestamp
t.datetime
t.numeric
t.decimal
t.float
t.bigint
t.integer
t.text
t.string
t.check_constraint
t.belongs_to
t.references
t.rename
t.change_null
t.change_default
t.change
t.timestamps
t.rename_index
t.index
t.column
t.primary_key
change_table :table do |t|
Available transformations are:
Also see TableDefinition and {connection.create_table}[rdoc-ref:SchemaStatements#create_table]
Represents an SQL table in an abstract way for updating a table.
= Active Record Connection Adapters Table
def change(column_name, type, **options)
t.change(:description, :text)
t.change(:name, :string, limit: 80)
Changes the column's definition according to the new options.
def change(column_name, type, **options) raise_on_if_exist_options(options) @base.change_column(name, column_name, type, **options) end
def change_default(column_name, default_or_changes)
t.change_default(:status, from: nil, to: "draft")
t.change_default(:authorized, 1)
t.change_default(:qualification, 'new')
Sets a new default value for a column.
def change_default(column_name, default_or_changes) @base.change_column_default(name, column_name, default_or_changes) end
def change_null(column_name, null, default = nil)
t.change_null(:qualification, false, 0)
t.change_null(:qualification, true)
Sets or removes a NOT NULL constraint on a column.
def change_null(column_name, null, default = nil) @base.change_column_null(name, column_name, null, default) end
def check_constraint(*args, **options)
t.check_constraint("price > 0", name: "price_check")
Adds a check constraint.
def check_constraint(*args, **options) @base.add_check_constraint(name, *args, **options) end
def check_constraint_exists?(*args, **options)
end
t.check_constraint("price > 0", name: "price_check")
unless t.check_constraint_exists?(name: "price_check")
Checks if a check_constraint exists on a table.
def check_constraint_exists?(*args, **options) @base.check_constraint_exists?(name, *args, **options) end
def column(column_name, type, index: nil, **options)
t.column(:name, :string)
Adds a new column to the named table.
def column(column_name, type, index: nil, **options) raise_on_if_exist_options(options) @base.add_column(name, column_name, type, **options) if index index_options = index.is_a?(Hash) ? index : {} index(column_name, **index_options) end end
def column_exists?(column_name, type = nil, **options)
t.string(:name) unless t.column_exists?(:name, :string)
Checks to see if a column exists.
def column_exists?(column_name, type = nil, **options) @base.column_exists?(name, column_name, type, **options) end
def foreign_key(*args, **options)
t.foreign_key(:authors, column: :author_id, primary_key: "id")
t.foreign_key(:authors)
Adds a foreign key to the table using a supplied table name.
def foreign_key(*args, **options) raise_on_if_exist_options(options) @base.add_foreign_key(name, *args, **options) end
def foreign_key_exists?(*args, **options)
t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)
Checks to see if a foreign key exists.
def foreign_key_exists?(*args, **options) @base.foreign_key_exists?(name, *args, **options) end
def index(column_name, **options)
t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')
t.index([:branch_id, :party_id], unique: true)
t.index(:name)
an Array of Symbols.
Adds a new index to the table. +column_name+ can be a single Symbol, or
def index(column_name, **options) raise_on_if_exist_options(options) @base.add_index(name, column_name, **options) end
def index_exists?(column_name, **options)
end
t.index(:branch_id)
unless t.index_exists?(:branch_id)
Checks to see if an index exists.
def index_exists?(column_name, **options) @base.index_exists?(name, column_name, **options) end
def initialize(table_name, base)
def initialize(table_name, base) @name = table_name @base = base end
def raise_on_if_exist_options(options)
def raise_on_if_exist_options(options) unrecognized_option = options.keys.find do |key| key == :if_exists || key == :if_not_exists end if unrecognized_option conditional = unrecognized_option == :if_exists ? "if" : "unless" message = <<~TXT Option #{unrecognized_option} will be ignored. If you are calling an expression like `t.column(.., #{unrecognized_option}: true)` from inside a change_table block, try a conditional clause instead, as in `t.column(..) #{conditional} t.column_exists?(..)` TXT raise ArgumentError.new(message) end end
def references(*args, **options)
t.belongs_to(:supplier, foreign_key: true)
t.references(:user)
Adds a reference.
def references(*args, **options) raise_on_if_exist_options(options) args.each do |ref_name| @base.add_reference(name, ref_name, **options) end end
def remove(*column_names, **options)
t.remove(:qualification, :experience)
t.remove(:qualification)
Removes the column(s) from the table definition.
def remove(*column_names, **options) raise_on_if_exist_options(options) @base.remove_columns(name, *column_names, **options) end
def remove_check_constraint(*args, **options)
t.remove_check_constraint(name: "price_check")
Removes the given check constraint from the table.
def remove_check_constraint(*args, **options) @base.remove_check_constraint(name, *args, **options) end
def remove_foreign_key(*args, **options)
t.remove_foreign_key(column: :author_id)
t.remove_foreign_key(:authors)
Removes the given foreign key from the table.
def remove_foreign_key(*args, **options) raise_on_if_exist_options(options) @base.remove_foreign_key(name, *args, **options) end
def remove_index(column_name = nil, **options)
t.remove_index(:branch_id, name: :by_branch_party)
t.remove_index(name: :by_branch_party)
t.remove_index(column: [:branch_id, :party_id])
t.remove_index(:branch_id)
Removes the given index from the table.
def remove_index(column_name = nil, **options) raise_on_if_exist_options(options) @base.remove_index(name, column_name, **options) end
def remove_references(*args, **options)
t.remove_belongs_to(:supplier, polymorphic: true)
t.remove_references(:user)
Removes a reference. Optionally removes a +type+ column.
def remove_references(*args, **options) raise_on_if_exist_options(options) args.each do |ref_name| @base.remove_reference(name, ref_name, **options) end end
def remove_timestamps(**options)
t.remove_timestamps
Removes the timestamp columns (+created_at+ and +updated_at+) from the table.
def remove_timestamps(**options) @base.remove_timestamps(name, **options) end
def rename(column_name, new_column_name)
t.rename(:description, :name)
Renames a column.
def rename(column_name, new_column_name) @base.rename_column(name, column_name, new_column_name) end
def rename_index(index_name, new_index_name)
t.rename_index(:user_id, :account_id)
Renames the given index on the table.
def rename_index(index_name, new_index_name) @base.rename_index(name, index_name, new_index_name) end
def timestamps(**options)
t.timestamps(null: false)
Adds timestamps (+created_at+ and +updated_at+) columns to the table.
def timestamps(**options) raise_on_if_exist_options(options) @base.add_timestamps(name, **options) end