Class: ActiveRecord::ConnectionAdapters::Table

Inherits:
Object
  • Object
show all
Includes:
ColumnMethods
Defined in:
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Overview

Represents an SQL table in an abstract way for updating a table. Also see TableDefinition and connection.create_table

Available transformations are:

change_table :table do |t|
  t.primary_key
  t.column
  t.index
  t.rename_index
  t.timestamps
  t.change
  t.change_default
  t.change_null
  t.rename
  t.references
  t.belongs_to
  t.check_constraint
  t.string
  t.text
  t.integer
  t.bigint
  t.float
  t.decimal
  t.numeric
  t.datetime
  t.timestamp
  t.time
  t.date
  t.binary
  t.blob
  t.boolean
  t.foreign_key
  t.json
  t.virtual
  t.remove
  t.remove_foreign_key
  t.remove_references
  t.remove_belongs_to
  t.remove_index
  t.remove_check_constraint
  t.remove_timestamps
end

Direct Known Subclasses

MySQL::Table, PostgreSQL::Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColumnMethods

#primary_key

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

#initialize(table_name, base) ⇒ Table

Returns a new instance of Table.



701
702
703
704
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 701

def initialize(table_name, base)
  @name = table_name
  @base = base
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name



699
700
701
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 699

def name
  @name
end

Instance Method Details

#change(column_name, type, **options) ⇒ Object

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

t.change(:name, :string, limit: 80)
t.change(:description, :text)

See TableDefinition#column for details of the options you can use.



778
779
780
781
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 778

def change(column_name, type, **options)
  raise_on_if_exist_options(options)
  @base.change_column(name, column_name, type, **options)
end

#change_default(column_name, default_or_changes) ⇒ Object

Sets a new default value for a column.

t.change_default(:qualification, 'new')
t.change_default(:authorized, 1)
t.change_default(:status, from: nil, to: "draft")

See connection.change_column_default



790
791
792
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 790

def change_default(column_name, default_or_changes)
  @base.change_column_default(name, column_name, default_or_changes)
end

#change_null(column_name, null, default = nil) ⇒ Object

Sets or removes a NOT NULL constraint on a column.

t.change_null(:qualification, true)
t.change_null(:qualification, false, 0)

See connection.change_column_null



800
801
802
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 800

def change_null(column_name, null, default = nil)
  @base.change_column_null(name, column_name, null, default)
end

#check_constraint(*args, **options) ⇒ Object

Adds a check constraint.

t.check_constraint("price > 0", name: "price_check")

See connection.add_check_constraint



910
911
912
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 910

def check_constraint(*args, **options)
  @base.add_check_constraint(name, *args, **options)
end

#check_constraint_exists?(*args, **options) ⇒ Boolean

Checks if a check_constraint exists on a table.

unless t.check_constraint_exists?(name: "price_check")
  t.check_constraint("price > 0", name: "price_check")
end

See connection.check_constraint_exists?

Returns:

  • (Boolean)


930
931
932
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 930

def check_constraint_exists?(*args, **options)
  @base.check_constraint_exists?(name, *args, **options)
end

#column(column_name, type, index: nil, **options) ⇒ Object

Adds a new column to the named table.

t.column(:name, :string)

See TableDefinition#column for details of the options you can use.



711
712
713
714
715
716
717
718
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 711

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

#column_exists?(column_name, type = nil, **options) ⇒ Boolean

Checks to see if a column exists.

t.string(:name) unless t.column_exists?(:name, :string)

See connection.column_exists?

Returns:

  • (Boolean)


725
726
727
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 725

def column_exists?(column_name, type = nil, **options)
  @base.column_exists?(name, column_name, type, **options)
end

#foreign_key(*args, **options) ⇒ Object

Adds a foreign key to the table using a supplied table name.

t.foreign_key(:authors)
t.foreign_key(:authors, column: :author_id, primary_key: "id")

See connection.add_foreign_key



880
881
882
883
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 880

def foreign_key(*args, **options)
  raise_on_if_exist_options(options)
  @base.add_foreign_key(name, *args, **options)
end

#foreign_key_exists?(*args, **options) ⇒ Boolean

Checks to see if a foreign key exists.

t.foreign_key(:authors) unless t.foreign_key_exists?(:authors)

See connection.foreign_key_exists?

Returns:

  • (Boolean)


901
902
903
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 901

def foreign_key_exists?(*args, **options)
  @base.foreign_key_exists?(name, *args, **options)
end

#index(column_name, **options) ⇒ Object

Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.

t.index(:name)
t.index([:branch_id, :party_id], unique: true)
t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')

See connection.add_index for details of the options you can use.



737
738
739
740
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 737

def index(column_name, **options)
  raise_on_if_exist_options(options)
  @base.add_index(name, column_name, **options)
end

#index_exists?(column_name, **options) ⇒ Boolean

Checks to see if an index exists.

unless t.index_exists?(:branch_id)
  t.index(:branch_id)
end

See connection.index_exists?

Returns:

  • (Boolean)


749
750
751
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 749

def index_exists?(column_name, **options)
  @base.index_exists?(name, column_name, **options)
end

#references(*args, **options) ⇒ Object Also known as: belongs_to

Adds a reference.

t.references(:user)
t.belongs_to(:supplier, foreign_key: true)

See connection.add_reference for details of the options you can use.



852
853
854
855
856
857
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 852

def references(*args, **options)
  raise_on_if_exist_options(options)
  args.each do |ref_name|
    @base.add_reference(name, ref_name, **options)
  end
end

#remove(*column_names, **options) ⇒ Object

Removes the column(s) from the table definition.

t.remove(:qualification)
t.remove(:qualification, :experience)

See connection.remove_columns



810
811
812
813
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 810

def remove(*column_names, **options)
  raise_on_if_exist_options(options)
  @base.remove_columns(name, *column_names, **options)
end

#remove_check_constraint(*args, **options) ⇒ Object

Removes the given check constraint from the table.

t.remove_check_constraint(name: "price_check")

See connection.remove_check_constraint



919
920
921
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 919

def remove_check_constraint(*args, **options)
  @base.remove_check_constraint(name, *args, **options)
end

#remove_foreign_key(*args, **options) ⇒ Object

Removes the given foreign key from the table.

t.remove_foreign_key(:authors)
t.remove_foreign_key(column: :author_id)

See connection.remove_foreign_key



891
892
893
894
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 891

def remove_foreign_key(*args, **options)
  raise_on_if_exist_options(options)
  @base.remove_foreign_key(name, *args, **options)
end

#remove_index(column_name = nil, **options) ⇒ Object

Removes the given index from the table.

t.remove_index(:branch_id)
t.remove_index(column: [:branch_id, :party_id])
t.remove_index(name: :by_branch_party)
t.remove_index(:branch_id, name: :by_branch_party)

See connection.remove_index



823
824
825
826
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 823

def remove_index(column_name = nil, **options)
  raise_on_if_exist_options(options)
  @base.remove_index(name, column_name, **options)
end

#remove_references(*args, **options) ⇒ Object Also known as: remove_belongs_to

Removes a reference. Optionally removes a type column.

t.remove_references(:user)
t.remove_belongs_to(:supplier, polymorphic: true)

See connection.remove_reference



866
867
868
869
870
871
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 866

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

#remove_timestamps(**options) ⇒ Object

Removes the timestamp columns (created_at and updated_at) from the table.

t.remove_timestamps

See connection.remove_timestamps



833
834
835
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 833

def remove_timestamps(**options)
  @base.remove_timestamps(name, **options)
end

#rename(column_name, new_column_name) ⇒ Object

Renames a column.

t.rename(:description, :name)

See connection.rename_column



842
843
844
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 842

def rename(column_name, new_column_name)
  @base.rename_column(name, column_name, new_column_name)
end

#rename_index(index_name, new_index_name) ⇒ Object

Renames the given index on the table.

t.rename_index(:user_id, :account_id)

See connection.rename_index



758
759
760
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 758

def rename_index(index_name, new_index_name)
  @base.rename_index(name, index_name, new_index_name)
end

#timestamps(**options) ⇒ Object

Adds timestamps (created_at and updated_at) columns to the table.

t.timestamps(null: false)

See connection.add_timestamps



767
768
769
770
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb', line 767

def timestamps(**options)
  raise_on_if_exist_options(options)
  @base.add_timestamps(name, **options)
end