Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix renaming primary key index when renaming a PostgreSQL table having uuid primary key #49998

Merged

Conversation

fatkodima
Copy link
Member

Fixes #49994.

Rails uses gen_random_uuid() function as a default for uuid primary key for PostgreSQL, which is not correctly fetched from the pg_catalog.

Comment on lines 18 to 19
assert_changes(-> { num_indices_named("before_rename_pkey") }, from: 1, to: 0) do
assert_changes(-> { num_indices_named("after_rename_pkey") }, from: 0, to: 1) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about factoring out an assert_renames_index(from, to, &block) helper method?

Comment on lines 35 to 32
test "renaming a table with uuid primary key and default also renames the primary key index" do
@connection.create_table :before_rename, force: true, id: :uuid, default: -> { "gen_random_uuid()" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since gen_random_uuid is already the default function for PostgreSQL >= 9.4, this is exercising the same code path as the previous test. Perhaps both tests should specify a default, one for uuid_generate_v4 and one for gen_random_uuid?

@fatkodima fatkodima force-pushed the fix-pg-rename_table-with-uuid-pk branch from 54a78b9 to 7fba608 Compare November 10, 2023 19:21
@fatkodima
Copy link
Member Author

Implemented suggestions. Please take a look.

@jonathanhefner jonathanhefner merged commit 16607e3 into rails:main Nov 10, 2023
4 checks passed
@jonathanhefner
Copy link
Member

jonathanhefner commented Nov 10, 2023

Thank you, @fatkodima! 🔑

Backported to 7-1-stable in 3e8f1e0 and 7-0-stable in 34f10c9.

rafaelfranca pushed a commit that referenced this pull request Nov 10, 2023
…d-pk

Fix renaming primary key index when renaming a PostgreSQL table having uuid primary key
@fatkodima fatkodima deleted the fix-pg-rename_table-with-uuid-pk branch November 10, 2023 20:53
jonathanhefner added a commit that referenced this pull request Nov 10, 2023
…d-pk

Fix renaming primary key index when renaming a PostgreSQL table having uuid primary key

(cherry picked from commit 16607e3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Primary key index not renamed when renaming table in Postgres when using UUIDs as pkey
2 participants