mirror of
https://github.com/github/rails.git
synced 2026-02-08 05:05:04 -05:00
Fix change_column to work with postgres 7.x and 8.x.
Closes #3141 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3327 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
*SVN*
|
||||
|
||||
* removed :piggyback in favor of just allowing :select on :through associations. [Tobias Luetke]
|
||||
|
||||
* made method missing delegation to class methods on relation target work on :through associations. [Tobias Luetke]
|
||||
|
||||
* made .find() work on :through relations. [Tobias Luetke]
|
||||
|
||||
* added :piggyback option to has_many :through relationships to pick up values from the join table as needed [Tobias Luetke]
|
||||
* Fix change_column to work with PostgreSQL 7.x and 8.x. #3141 [wejn@box.cz, Rick Olson, Scott Barron]
|
||||
|
||||
* Fix typo in association docs. #3296. [Blair Zajac]
|
||||
|
||||
|
||||
@@ -302,7 +302,17 @@ module ActiveRecord
|
||||
end
|
||||
|
||||
def change_column(table_name, column_name, type, options = {}) #:nodoc:
|
||||
execute = "ALTER TABLE #{table_name} ALTER #{column_name} TYPE #{type}"
|
||||
begin
|
||||
execute "ALTER TABLE #{table_name} ALTER #{column_name} TYPE #{type_to_sql(type, options[:limit])}"
|
||||
rescue ActiveRecord::StatementInvalid
|
||||
# This is PG7, so we use a more arcane way of doing it.
|
||||
begin_db_transaction
|
||||
add_column(table_name, "#{column_name}_ar_tmp", type, options)
|
||||
execute "UPDATE #{table_name} SET #{column_name}_ar_tmp = CAST(#{column_name} AS #{type_to_sql(type, options[:limit])})"
|
||||
remove_column(table_name, column_name)
|
||||
rename_column(table_name, "#{column_name}_ar_tmp", column_name)
|
||||
commit_db_transaction
|
||||
end
|
||||
change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
|
||||
end
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
Person.connection.remove_column("people", "administrator") rescue nil
|
||||
Person.reset_column_information
|
||||
end
|
||||
|
||||
|
||||
def test_add_index
|
||||
Person.connection.add_column "people", "last_name", :string
|
||||
Person.connection.add_column "people", "administrator", :boolean
|
||||
@@ -258,8 +258,15 @@ if ActiveRecord::Base.connection.supports_migrations?
|
||||
end
|
||||
|
||||
def test_change_column
|
||||
Person.connection.add_column "people", "bio", :string
|
||||
assert_nothing_raised { Person.connection.change_column "people", "bio", :text }
|
||||
Person.connection.add_column 'people', 'age', :integer
|
||||
old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
|
||||
assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
|
||||
|
||||
assert_nothing_raised { Person.connection.change_column "people", "age", :string }
|
||||
|
||||
new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
|
||||
assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
|
||||
assert new_columns.find { |c| c.name == 'age' and c.type == :string }
|
||||
end
|
||||
|
||||
def test_change_column_with_new_default
|
||||
|
||||
Reference in New Issue
Block a user