aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-07-11 02:40:22 -0700
committerPiotr Sarnacki <drogus@gmail.com>2012-07-11 02:40:22 -0700
commite72d8768878efd13b817ad91a12f872f53efd809 (patch)
treeefe70a001cbb118000d37c2085a954994355e7d9
parent3b1de4a29cb8dd463c2fe12f981ca56559f4e5bb (diff)
parent8d3eafe7906f6d6cf22f7d1ff038c887a32d9a6b (diff)
downloadrails-e72d8768878efd13b817ad91a12f872f53efd809.tar.gz
rails-e72d8768878efd13b817ad91a12f872f53efd809.tar.bz2
rails-e72d8768878efd13b817ad91a12f872f53efd809.zip
Merge pull request #7031 from robbkidd/rename_sequences_too_backport_to_3-2
Back-port #6874 to 3.2: psql adapter should rename a default pk sequence during rename_table
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb7
-rw-r--r--activerecord/test/cases/migration_test.rb20
2 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 822f8f73de..cd565787b3 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1005,12 +1005,19 @@ module ActiveRecord
end
# Renames a table.
+ # Also renames a table's primary key sequence if the sequence name matches the
+ # Active Record default.
#
# Example:
# rename_table('octopuses', 'octopi')
def rename_table(name, new_name)
clear_cache!
execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}"
+ pk, seq = pk_and_sequence_for(new_name)
+ if seq == "#{name}_#{pk}_seq"
+ new_seq = "#{new_name}_#{pk}_seq"
+ execute "ALTER TABLE #{quote_table_name(seq)} RENAME TO #{quote_table_name(new_seq)}"
+ end
end
# Adds a new column to the named table.
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index bedae76e28..fb5eb2633b 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -949,6 +949,26 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ if current_adapter?(:PostgreSQLAdapter)
+ def test_rename_table_for_postgresql_should_also_rename_default_sequence
+ begin
+ ActiveRecord::Base.connection.create_table :octopuses do |t|
+ t.column :url, :string
+ end
+ ActiveRecord::Base.connection.rename_table :octopuses, :octopi
+
+ pk, seq = ActiveRecord::Base.connection.pk_and_sequence_for('octopi')
+
+ assert_equal "octopi_#{pk}_seq", seq
+
+ ensure
+ ActiveRecord::Base.connection.drop_table :octopuses rescue nil
+ ActiveRecord::Base.connection.drop_table :octopi rescue nil
+ end
+ end
+ end
+
+
def test_change_column_nullability
Person.delete_all
Person.connection.add_column "people", "funny", :boolean