diff options
author | Abdelkader Boudih <terminale@gmail.com> | 2014-06-27 08:46:07 +0000 |
---|---|---|
committer | Abdelkader Boudih <terminale@gmail.com> | 2014-06-27 19:43:54 +0000 |
commit | b58ec66f0e96ef7741d9ae7ae619683e6cb9d1f8 (patch) | |
tree | e7798d3c07957d60e7be4b7d2cbb88870d981a8a | |
parent | 30b56084fb67d99fa3284d34c9bb914ebee378b6 (diff) | |
download | rails-b58ec66f0e96ef7741d9ae7ae619683e6cb9d1f8.tar.gz rails-b58ec66f0e96ef7741d9ae7ae619683e6cb9d1f8.tar.bz2 rails-b58ec66f0e96ef7741d9ae7ae619683e6cb9d1f8.zip |
rename sequence only if it exists
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/migration/rename_table_test.rb | 10 |
3 files changed, 17 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f94e8f21ca..b17ca22544 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 30df98be1b..596ebca5d6 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -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 diff --git a/activerecord/test/cases/migration/rename_table_test.rb b/activerecord/test/cases/migration/rename_table_test.rb index a52b58c4ac..ba39fb1dec 100644 --- a/activerecord/test/cases/migration/rename_table_test.rb +++ b/activerecord/test/cases/migration/rename_table_test.rb @@ -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 |