Skip to content

Support altering primary key columns that are not included in FK constraints #930

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

Merged
merged 1 commit into from
Jul 1, 2025

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Jun 23, 2025

In the past pgroll could not preserve primary key constraint on altered columns. Instead the primary key constraint was down-graded to a unique, not null constraint. Now the PK constraint is not removed during completing the migration.

Futhermore, if the table had rows, pgroll could not backfill rows. pgroll "updates" the primary keys of the backfilled table to trigger backfilling existing columns. Previously, finding the identity columns returned the duplicated column __pgroll_new_id instead of the correct id. In this case we need the current PK, so we can copy the correct values to the new column.

I opened follow-up issue to support altering mutiple columns of the same composite key in the same migration: #930

Support for alter PK columns that are part of FK constraints is in a follow-up PR: #935

Closes #888

@github-actions github-actions bot temporarily deployed to Docs Preview June 23, 2025 14:36 Inactive
@kvch kvch changed the title Change lock to FOR UPDATE so unique columns can be backfilled as well Change lock to FOR UPDATE so unique columns can be backfilled Jun 23, 2025
@kvch kvch force-pushed the fix-backfill-unique branch from 15d03a4 to cb7a074 Compare June 24, 2025 13:31
@github-actions github-actions bot temporarily deployed to Docs Preview June 24, 2025 13:31 Inactive
@kvch kvch changed the title Change lock to FOR UPDATE so unique columns can be backfilled Support altering primary key columns Jun 24, 2025
@kvch kvch force-pushed the fix-backfill-unique branch from cb7a074 to 0614e53 Compare June 24, 2025 13:41
@github-actions github-actions bot temporarily deployed to Docs Preview June 24, 2025 13:41 Inactive
@kvch kvch requested a review from andrew-farries June 24, 2025 13:43
@kvch kvch marked this pull request as ready for review June 24, 2025 13:45
@kvch kvch force-pushed the fix-backfill-unique branch from 0614e53 to dcc5a75 Compare June 25, 2025 13:01
@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 13:01 Inactive
@kvch kvch force-pushed the fix-backfill-unique branch from dcc5a75 to 0d5eed0 Compare June 25, 2025 13:03
@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 13:03 Inactive
@kvch kvch changed the title Support altering primary key columns Support altering primary key columns that are not included in FK constraints Jun 25, 2025
@kvch kvch force-pushed the fix-backfill-unique branch from 0d5eed0 to aa744fd Compare June 25, 2025 13:13
@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 13:13 Inactive
Copy link

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/xataio/pgroll/internal/testutils 0.00% (ø)
github.com/xataio/pgroll/pkg/backfill 0.00% (ø)
github.com/xataio/pgroll/pkg/migrations 75.41% (+0.12%) 👍

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/xataio/pgroll/internal/testutils/error_codes.go 0.00% (ø) 0 0 0
github.com/xataio/pgroll/pkg/backfill/backfill.go 0.00% (ø) 71 (-4) 0 71 (-4)
github.com/xataio/pgroll/pkg/migrations/rename.go 81.25% (-0.15%) 48 (+5) 39 (+4) 9 (+1) 👎

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/xataio/pgroll/pkg/migrations/op_alter_column_test.go

Copy link
Collaborator

@andrew-farries andrew-farries left a comment

Choose a reason for hiding this comment

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

Looks good 👍

@kvch kvch merged commit be1a7cf into main Jul 1, 2025
30 checks passed
@kvch kvch deleted the fix-backfill-unique branch July 1, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve primary key constraint on altered columns
2 participants