Merge pull request #15934 from seuros/rename

rename primary key sequence only if it exists
This commit is contained in:
Rafael Mendonça França 2014-06-27 18:28:55 -03:00
commit 0aeb490dc4
3 changed files with 17 additions and 3 deletions

View File

@ -1,3 +1,7 @@
* PostgreSQL renaming table doesn't attempt to rename non existent sequences.
*Abdelkader Boudih*
* Move 'dependent: :destroy' handling for 'belongs_to'
from 'before_destroy' to 'after_destroy' callback chain

View File

@ -375,8 +375,8 @@ module ActiveRecord
end
# Renames a table.
# Also renames a table's primary key sequence if the sequence name matches the
# Active Record default.
# Also renames a table's primary key sequence if the sequence name exists and
# matches the Active Record default.
#
# Example:
# rename_table('octopuses', 'octopi')
@ -384,7 +384,7 @@ module ActiveRecord
clear_cache!
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
pk, seq = pk_and_sequence_for(new_name)
if seq.identifier == "#{table_name}_#{pk}_seq"
if seq && seq.identifier == "#{table_name}_#{pk}_seq"
new_seq = "#{new_name}_#{pk}_seq"
execute "ALTER TABLE #{quote_table_name(seq)} RENAME TO #{quote_table_name(new_seq)}"
end

View File

@ -76,6 +76,16 @@ module ActiveRecord
assert_equal ConnectionAdapters::PostgreSQL::Name.new("public", "octopi_#{pk}_seq"), seq
end
def test_renaming_table_doesnt_attempt_to_rename_non_existent_sequences
enable_uuid_ossp!(connection)
connection.create_table :cats, id: :uuid
assert_nothing_raised { rename_table :cats, :felines }
assert connection.table_exists? :felines
ensure
connection.drop_table :cats if connection.table_exists? :cats
connection.drop_table :felines if connection.table_exists? :felines
end
end
end
end