From dcc143cd702bfbfef9cb92e1fde75171d088a5ac Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sat, 22 Nov 2014 13:16:14 -0700 Subject: Rename the primary key index when renaming a table in pg Also checked to make sure this does not affect foreign key constraints. (It doesn't). Fixes #12856 Closes #14088 --- activerecord/CHANGELOG.md | 6 ++++ .../postgresql/schema_statements.rb | 3 ++ .../cases/adapters/postgresql/rename_table_test.rb | 34 ++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 activerecord/test/cases/adapters/postgresql/rename_table_test.rb diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fcc6523d6e..16f776df58 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Renaming a table in pg also renames the primary key index. + + Fixes #12856 + + *Sean Griffin* + * Make it possible to access fixtures excluded by a `default_scope`. *Yves Senn* 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 b4b066c06e..515ff2dd44 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -411,7 +411,10 @@ module ActiveRecord pk, seq = pk_and_sequence_for(new_name) if seq && seq.identifier == "#{table_name}_#{pk}_seq" new_seq = "#{new_name}_#{pk}_seq" + idx = "#{table_name}_pkey" + new_idx = "#{new_name}_pkey" execute "ALTER TABLE #{quote_table_name(seq)} RENAME TO #{quote_table_name(new_seq)}" + execute "ALTER INDEX #{quote_table_name(idx)} RENAME TO #{quote_table_name(new_idx)}" end rename_table_indexes(table_name, new_name) diff --git a/activerecord/test/cases/adapters/postgresql/rename_table_test.rb b/activerecord/test/cases/adapters/postgresql/rename_table_test.rb new file mode 100644 index 0000000000..056a035622 --- /dev/null +++ b/activerecord/test/cases/adapters/postgresql/rename_table_test.rb @@ -0,0 +1,34 @@ +require "cases/helper" + +class PostgresqlRenameTableTest < ActiveRecord::TestCase + def setup + @connection = ActiveRecord::Base.connection + @connection.create_table :before_rename, force: true + end + + def teardown + @connection.execute 'DROP TABLE IF EXISTS "before_rename"' + @connection.execute 'DROP TABLE IF EXISTS "after_rename"' + end + + test "renaming a table also renames the primary key index" do + # sanity check + assert_equal 1, num_indices_named("before_rename_pkey") + assert_equal 0, num_indices_named("after_rename_pkey") + + @connection.rename_table :before_rename, :after_rename + + assert_equal 0, num_indices_named("before_rename_pkey") + assert_equal 1, num_indices_named("after_rename_pkey") + end + + private + + def num_indices_named(name) + @connection.execute(<<-SQL).values.length + SELECT 1 FROM "pg_index" + JOIN "pg_class" ON "pg_index"."indexrelid" = "pg_class"."oid" + WHERE "pg_class"."relname" = '#{name}' + SQL + end +end -- cgit v1.2.3