aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-27 18:28:55 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-27 18:30:01 -0300
commit0aeb490dc48c39c1bfaf8b8ff772c266a9cde867 (patch)
treed40f83896d1cabb85651660cf36f8756acea10d5 /activerecord
parent3c19402fc3ba31c38df386ac328c3bccc717c9c3 (diff)
parentb58ec66f0e96ef7741d9ae7ae619683e6cb9d1f8 (diff)
downloadrails-0aeb490dc48c39c1bfaf8b8ff772c266a9cde867.tar.gz
rails-0aeb490dc48c39c1bfaf8b8ff772c266a9cde867.tar.bz2
rails-0aeb490dc48c39c1bfaf8b8ff772c266a9cde867.zip
Merge pull request #15934 from seuros/rename
rename primary key sequence only if it exists
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb6
-rw-r--r--activerecord/test/cases/migration/rename_table_test.rb10
3 files changed, 17 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index f94e8f21ca..7e40373163 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