diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 04cf72b..e733385 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -4,7 +4,7 @@
change_table :videos do |t|
t.timestamps # adds created_at, updated_at
- t.belongs_to :goat # add goat_id integer
+ t.belongs_to :goat # adds goat_id integer
t.string :name, :email, :limit => 20 # adds name and email both with a 20 char limit
t.remove :name, :email # removes the name and email columns
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index d73ffc3..12b9dec 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -18,11 +18,11 @@ module ActiveRecord
#
# +name+ is the column's name, as in supplier_id int(11).
# +default+ is the type-casted default value, such as sales_stage varchar(20) default 'new'.
- # +sql_type+ is only used to extract the column's length, if necessary. For example, company_name varchar(60).
+ # +sql_type+ is only used to extract the column's length, if necessary. For example, company_name varchar(60).
# +null+ determines if this column allows +NULL+ values.
def initialize(name, default, sql_type = nil, null = true)
@name, @sql_type, @null = name, sql_type, null
- @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
+ @limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
@type = simplified_type(sql_type)
@default = extract_default(default)
@@ -172,7 +172,7 @@ module ActiveRecord
def new_time(year, mon, mday, hour, min, sec, microsec)
# Treat 0000-00-00 00:00:00 as nil.
return nil if year.nil? || year == 0
-
+
Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
end
@@ -250,11 +250,11 @@ module ActiveRecord
end
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null) #:nodoc:
-
+
def sql_type
base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
end
-
+
def to_sql
column_sql = "#{base.quote_column_name(name)} #{sql_type}"
add_column_options!(column_sql, :null => null, :default => default) unless type.to_sym == :primary_key
@@ -309,39 +309,39 @@ module ActiveRecord
# * :default -
# The column's default value. Use nil for NULL.
# * :null -
- # Allows or disallows +NULL+ values in the column. This option could
+ # Allows or disallows +NULL+ values in the column. This option could
# have been named :null_allowed.
# * :precision -
- # Specifies the precision for a :decimal column.
+ # Specifies the precision for a :decimal column.
# * :scale -
- # Specifies the scale for a :decimal column.
+ # Specifies the scale for a :decimal column.
#
# Please be aware of different RDBMS implementations behavior with
# :decimal columns:
# * The SQL standard says the default scale should be 0, :scale <=
# :precision, and makes no comments about the requirements of
# :precision.
- # * MySQL: :precision [1..63], :scale [0..30].
+ # * MySQL: :precision [1..63], :scale [0..30].
# Default is (10,0).
- # * PostgreSQL: :precision [1..infinity],
+ # * PostgreSQL: :precision [1..infinity],
# :scale [0..infinity]. No default.
- # * SQLite2: Any :precision and :scale may be used.
+ # * SQLite2: Any :precision and :scale may be used.
# Internal storage as strings. No default.
# * SQLite3: No restrictions on :precision and :scale,
# but the maximum supported :precision is 16. No default.
- # * Oracle: :precision [1..38], :scale [-84..127].
+ # * Oracle: :precision [1..38], :scale [-84..127].
# Default is (38,0).
- # * DB2: :precision [1..63], :scale [0..62].
+ # * DB2: :precision [1..63], :scale [0..62].
# Default unknown.
- # * Firebird: :precision [1..18], :scale [0..18].
+ # * Firebird: :precision [1..18], :scale [0..18].
# Default (9,0). Internal types NUMERIC and DECIMAL have different
# storage rules, decimal being better.
- # * FrontBase?: :precision [1..38], :scale [0..38].
+ # * FrontBase?: :precision [1..38], :scale [0..38].
# Default (38,0). WARNING Max :precision/:scale for
# NUMERIC is 19, and DECIMAL is 38.
- # * SqlServer?: :precision [1..38], :scale [0..38].
+ # * SqlServer?: :precision [1..38], :scale [0..38].
# Default (38,0).
- # * Sybase: :precision [1..38], :scale [0..38].
+ # * Sybase: :precision [1..38], :scale [0..38].
# Default (38,0).
# * OpenBase?: Documentation unclear. Claims storage in double.
#
@@ -394,7 +394,7 @@ module ActiveRecord
# t.timestamps
# end
#
- # There's a short-hand method for each of the type values declared at the top. And then there's
+ # There's a short-hand method for each of the type values declared at the top. And then there's
# TableDefinition#timestamps that'll add created_at and updated_at as datetimes.
#
# TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
@@ -434,13 +434,13 @@ module ActiveRecord
def #{column_type}(*args)
options = args.extract_options!
column_names = args
-
+
column_names.each { |name| column(name, '#{column_type}', options) }
end
EOV
end
-
- # Appends :datetime columns :created_at and
+
+ # Appends :datetime columns :created_at and
# :updated_at to the table.
def timestamps
column(:created_at, :datetime)
@@ -456,9 +456,10 @@ module ActiveRecord
end
end
alias :belongs_to :references
+ alias :reference :references
# Returns a String whose contents are the column definitions
- # concatenated together. This string can then be prepended and appended to
+ # concatenated together. This string can then be prepended and appended to
# to generate the final SQL to create the table.
def to_sql
@columns * ', '
@@ -482,6 +483,7 @@ module ActiveRecord
# t.change
# t.change_default
# t.rename
+ # t.reference
# t.references
# t.belongs_to
# t.string
@@ -496,6 +498,7 @@ module ActiveRecord
# t.binary
# t.boolean
# t.remove
+ # t.remove_reference
# t.remove_references
# t.remove_belongs_to
# t.remove_index
@@ -510,15 +513,15 @@ module ActiveRecord
# Adds a new column to the named table.
# See TableDefinition#column for details of the options you can use.
- # ===== Examples
- # ====== Creating a simple columns
+ # ===== Example
+ # ====== Creating a simple column
# t.column(:name, :string)
def column(column_name, type, options = {})
@base.add_column(@table_name, column_name, type, options)
end
- # Adds a new index to the table. +column_name+ can be a single Symbol, or
- # an Array of Symbols. See SchemaStatements#add_index
+ # Adds a new index to the table. +column_name+ can be a single Symbol, or
+ # an Array of Symbols. See SchemaStatements#add_index
#
# ===== Examples
# ====== Creating a simple index
@@ -531,8 +534,8 @@ module ActiveRecord
@base.add_index(@table_name, column_name, options)
end
- # Adds timestamps (created_at and updated_at) columns to the table. See SchemaStatements#timestamps
- # ===== Examples
+ # Adds timestamps (created_at and updated_at) columns to the table. See SchemaStatements#add_timestamps
+ # ===== Example
# t.timestamps
def timestamps
@base.add_timestamps(@table_name)
@@ -547,7 +550,7 @@ module ActiveRecord
@base.change_column(@table_name, column_name, type, options)
end
- # Sets a new default value for a column. See
+ # Sets a new default value for a column.
# ===== Examples
# t.change_default(:qualification, 'new')
# t.change_default(:authorized, 1)
@@ -559,27 +562,27 @@ module ActiveRecord
# ===== Examples
# t.remove(:qualification)
# t.remove(:qualification, :experience)
- # t.removes(:qualification, :experience)
def remove(*column_names)
@base.remove_column(@table_name, column_names)
end
- # Remove the given index from the table.
+ # Removes the given index from the table.
#
- # Remove the suppliers_name_index in the suppliers table.
+ # ===== Examples
+ # ====== Remove the suppliers_name_index in the suppliers table
# t.remove_index :name
- # Remove the index named accounts_branch_id_index in the accounts table.
+ # ====== Remove the index named accounts_branch_id_index in the accounts table
# t.remove_index :column => :branch_id
- # Remove the index named accounts_branch_id_party_id_index in the accounts table.
+ # ====== Remove the index named accounts_branch_id_party_id_index in the accounts table
# t.remove_index :column => [:branch_id, :party_id]
- # Remove the index named by_branch_party in the accounts table.
+ # ====== Remove the index named by_branch_party in the accounts table
# t.remove_index :name => :by_branch_party
def remove_index(options = {})
@base.remove_index(@table_name, options)
end
# Removes the timestamp columns (created_at and updated_at) from the table.
- # ===== Examples
+ # ===== Example
# t.remove_timestamps
def remove_timestamps
@base.remove_timestamps(@table_name)
@@ -592,11 +595,11 @@ module ActiveRecord
@base.rename_column(@table_name, column_name, new_column_name)
end
- # Adds a reference. Optionally adds a +type+ column. reference,
- # references and belongs_to are all acceptable
- # ===== Example
- # t.references(:goat)
- # t.references(:goat, :polymorphic => true)
+ # Adds a reference. Optionally adds a +type+ column. reference,
+ # references and belongs_to are all acceptable.
+ # ===== Examples
+ # t.reference(:goat)
+ # t.reference(:goat, :polymorphic => true)
# t.references(:goat)
# t.belongs_to(:goat)
def references(*args)
@@ -608,10 +611,11 @@ module ActiveRecord
end
end
alias :belongs_to :references
+ alias :reference :references
- # Adds a reference. Optionally removes a +type+ column. remove_reference,
- # remove_references and remove_belongs_to are all acceptable
- # ===== Example
+ # Removes a reference. Optionally removes a +type+ column. remove_reference,
+ # remove_references and remove_belongs_to are all acceptable.
+ # ===== Examples
# t.remove_reference(:goat)
# t.remove_reference(:goat, :polymorphic => true)
# t.remove_references(:goat)
@@ -624,10 +628,11 @@ module ActiveRecord
@base.remove_column(@table_name, "#{col}_type") unless polymorphic.nil?
end
end
- alias :remove_belongs_to :remove_references
+ alias :remove_belongs_to :remove_references
+ alias :remove_reference :remove_references
# Adds a column or columns of a specified type
- # ===== Example
+ # ===== Examples
# t.string(:goat)
# t.string(:goat, :sheep)
%w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index 6aae556..b1874d2 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -13,7 +13,7 @@ module ActiveRecord
255
end
- # Truncates a table alias according to the limits of the current adapter.
+ # Truncates a table alias according to the limits of the current adapter.
def table_alias_for(table_name)
table_name[0..table_alias_length-1].gsub(/\./, '_')
end
@@ -148,7 +148,7 @@ module ActiveRecord
# t.remove :company
# end
#
- # ====== Remove a column
+ # ====== Remove several columns
# change_table(:suppliers) do |t|
# t.remove :company_id
# t.remove :width, :height
@@ -164,7 +164,7 @@ module ActiveRecord
def change_table(table_name)
yield Table.new(table_name, self)
end
-
+
# Renames a table.
# ===== Example
# rename_table('octopuses', 'octopi')
@@ -195,7 +195,7 @@ module ActiveRecord
end
end
alias :remove_columns :remove_column
-
+
# Changes the column's definition according to the new options.
# See TableDefinition#column for details of the options you can use.
# ===== Examples
@@ -379,7 +379,7 @@ module ActiveRecord
def distinct(columns, order_by)
"DISTINCT #{columns}"
end
-
+
# ORDER BY clause for the passed order option.
# PostgreSQL overrides this due to its stricter standards compliance.
def add_order_by_for_association_limiting!(sql, options)
@@ -391,17 +391,17 @@ module ActiveRecord
# add_timestamps(:suppliers)
def add_timestamps(table_name)
add_column table_name, :created_at, :datetime
- add_column table_name, :updated_at, :datetime
+ add_column table_name, :updated_at, :datetime
end
-
+
# Removes the timestamp columns (created_at and updated_at) from the table definition.
# ===== Examples
# remove_timestamps(:suppliers)
def remove_timestamps(table_name)
- remove_column table_name, :updated_at
- remove_column table_name, :created_at
+ remove_column table_name, :updated_at
+ remove_column table_name, :created_at
end
-
+
protected
def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?)
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index d4e8182..561dfab 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -1113,6 +1113,20 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ def test_add_reference_works_like_add_references
+ with_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
+ t.reference :customer
+ end
+ end
+
+ def test_remove_reference_works_like_remove_references
+ with_change_table do |t|
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
+ t.remove_reference :customer
+ end
+ end
+
def test_add_belongs_to_works_like_add_references
with_change_table do |t|
@connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})