Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update psql adapter to rename a default pkey sequence when renaming a…

… table.
  • Loading branch information...
commit d735abf401c722a2efb9302f5aa6683b047572f8 1 parent 4a20fcf
@robbkidd robbkidd authored
View
7 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1208,12 +1208,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.
View
13 activerecord/test/cases/migration/rename_table_test.rb
@@ -67,6 +67,19 @@ def test_rename_table_with_an_index
rename_table :octopi, :test_models
end
+
+ def test_rename_table_for_postgresql_should_also_rename_default_sequence
+ skip 'not supported' unless current_adapter?(:PostgreSQLAdapter)
+
+ rename_table :test_models, :octopi
+
+ con = ActiveRecord::Base.connection
+ pk, seq = con.pk_and_sequence_for('octopi')
+
+ assert_equal "octopi_#{pk}_seq", seq
+
+ rename_table :octopi, :test_models
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.