Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename the primary key index when renaming a table in pg
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
activerecord/test/cases/adapters/postgresql/rename_table_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |