class ActiveRecord::ConnectionAdapters::TableDefinition
end
end
…
def down
end
end
puts t.class # => “ActiveRecord::ConnectionAdapters::TableDefinition”
create_table :foo do |t|
def up
class SomeMigration < ActiveRecord::Migration[7.0]
is actually of this type:
Inside migration files, the t
object in {create_table}[rdoc-ref:SchemaStatements#create_table]
provides methods for manipulating the schema representation.
Represents the schema of an SQL table in an abstract way. This class
def [](name)
def [](name) @columns_hash[name.to_s] end
def aliased_types(name, fallback)
def aliased_types(name, fallback) "timestamp" == name ? :datetime : fallback end
def check_constraint(expression, **options)
def check_constraint(expression, **options) check_constraints << new_check_constraint_definition(expression, options) end
def column(name, type, index: nil, **options)
t.references :taggable, polymorphic: { default: 'Photo' }, index: false
t.references :tagger, polymorphic: true
t.references :tag, index: { name: 'index_taggings_on_tag_id' }
create_table :taggings do |t|
Can also be written as follows using references:
add_index :taggings, [:tagger_id, :tagger_type]
add_index :taggings, :tag_id, name: 'index_taggings_on_tag_id'
end
t.string :taggable_type, default: 'Photo'
t.string :tagger_type
t.integer :tag_id, :tagger_id, :taggable_id
create_table :taggings do |t|
So what can be written like this:
will also create an index, similar to calling {add_index}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_index].
options, these will be used when creating the _type column. The :index option
column if the :polymorphic option is supplied. If :polymorphic is a hash of
TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
TableDefinition#timestamps that'll add +created_at+ and +updated_at+ as datetimes.
There's a short-hand method for each of the type values declared at the top. And then there's
end
t.timestamps null: false
t.string :name, :value, default: "Untitled"
t.string :item_number, index: true
t.integer :shop_id, :creator_id
create_table :products do |t|
can also be written as follows using the short-hand:
add_index :products, :item_number
end
t.column :updated_at, :datetime
t.column :created_at, :datetime
t.column :value, :string, default: "Untitled"
t.column :name, :string, default: "Untitled"
t.column :item_number, :string
t.column :creator_id, :integer
t.column :shop_id, :integer
create_table :products do |t|
What can be written like this with the regular calls to column:
in a single statement.
They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
Instead of calling #column directly, you can also work with the short-hand definitions for the default types.
== Short-hand examples
td.column(:granted, :boolean, index: true)
# Assuming +td+ is an instance of TableDefinition
== Examples
This method returns self.
Create an index for the column. Can be either true or an options hash.
* :index -
Additional options are:
for available options.
See {connection.add_column}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_column]
Instantiates a new column for the table.
def column(name, type, index: nil, **options) name = name.to_s type = type.to_sym if type if @columns_hash[name] if @columns_hash[name].primary_key? raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table." else raise ArgumentError, "you can't define an already defined column '#{name}'." end end if @conn.supports_datetime_with_precision? if type == :datetime && !options.key?(:precision) options[:precision] = 6 end end @columns_hash[name] = new_column_definition(name, type, **options) if index index_options = index.is_a?(Hash) ? index : {} index(name, **index_options) end self end
def columns; @columns_hash.values; end
def columns; @columns_hash.values; end
def create_column_definition(name, type, options)
def create_column_definition(name, type, options) ColumnDefinition.new(name, type, options) end
def foreign_key(to_table, **options)
def foreign_key(to_table, **options) foreign_keys << new_foreign_key_definition(to_table, options) end
def index(column_name, **options)
This is primarily used to track indexes that need to be created after the table
Adds index options to the indexes hash, keyed by column name
def index(column_name, **options) indexes << [column_name, options] end
def initialize(
def initialize( conn, name, temporary: false, if_not_exists: false, options: nil, as: nil, comment: nil, ** ) @conn = conn @columns_hash = {} @indexes = [] @foreign_keys = [] @primary_keys = nil @check_constraints = [] @temporary = temporary @if_not_exists = if_not_exists @options = options @as = as @name = name @comment = comment end
def integer_like_primary_key?(type, options)
def integer_like_primary_key?(type, options) options[:primary_key] && [:integer, :bigint].include?(type) && !options.key?(:default) end
def integer_like_primary_key_type(type, options)
def integer_like_primary_key_type(type, options) type end
def new_check_constraint_definition(expression, options) # :nodoc:
def new_check_constraint_definition(expression, options) # :nodoc: options = @conn.check_constraint_options(name, expression, options) CheckConstraintDefinition.new(name, expression, options) end
def new_column_definition(name, type, **options) # :nodoc:
def new_column_definition(name, type, **options) # :nodoc: if integer_like_primary_key?(type, options) type = integer_like_primary_key_type(type, options) end type = aliased_types(type.to_s, type) options[:primary_key] ||= type == :primary_key options[:null] = false if options[:primary_key] create_column_definition(name, type, options) end
def new_foreign_key_definition(to_table, options) # :nodoc:
def new_foreign_key_definition(to_table, options) # :nodoc: prefix = ActiveRecord::Base.table_name_prefix suffix = ActiveRecord::Base.table_name_suffix to_table = "#{prefix}#{to_table}#{suffix}" options = @conn.foreign_key_options(name, to_table, options) ForeignKeyDefinition.new(name, to_table, options) end
def primary_keys(name = nil) # :nodoc:
def primary_keys(name = nil) # :nodoc: @primary_keys = PrimaryKeyDefinition.new(name) if name @primary_keys end
def references(*args, **options)
t.belongs_to(:supplier, foreign_key: true, type: :integer)
t.belongs_to(:supplier, foreign_key: true)
t.references(:user)
Adds a reference.
def references(*args, **options) args.each do |ref_name| ReferenceDefinition.new(ref_name, **options).add_to(self) end end
def remove_column(name)
remove the column +name+ from the table.
def remove_column(name) @columns_hash.delete name.to_s end
def timestamps(**options)
:updated_at to the table. See {connection.add_timestamps}[rdoc-ref:SchemaStatements#add_timestamps]
Appends :datetime columns :created_at and
def timestamps(**options) options[:null] = false if options[:null].nil? if !options.key?(:precision) && @conn.supports_datetime_with_precision? options[:precision] = 6 end column(:created_at, :datetime, **options) column(:updated_at, :datetime, **options) end