Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Back-port #6874 to 3.2: psql adapter should rename a default pk sequence during rename_table #7031

Merged
merged 1 commit into from Jul 11, 2012
Jump to file or symbol
Failed to load files and symbols.
+27 −0
Split
@@ -1005,12 +1005,19 @@ def primary_key(table)
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.
@@ -949,6 +949,26 @@ def test_rename_table
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