Skip to content

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Jun 25, 2025

Previously, pgroll could not alter PK columns if they were included in a foreign key reference. The old column
could not be dropped because it was still referred by foreign key constraints. Now the foreign key constraints are
recreated so they refer to the new PK column.

Requires #930
Closes #375

@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 12:55 Inactive
@kvch kvch force-pushed the fix-change-column-type-when-fk branch from 3a3d6f1 to f8ee926 Compare June 25, 2025 13:28
@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 13:28 Inactive
@kvch kvch force-pushed the fix-change-column-type-when-fk branch from f8ee926 to ae55c6b Compare June 25, 2025 13:29
@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 13:29 Inactive
@kvch kvch marked this pull request as draft June 25, 2025 14:34
@kvch kvch force-pushed the fix-change-column-type-when-fk branch from ae55c6b to ce7ef05 Compare June 25, 2025 14:42
@github-actions github-actions bot temporarily deployed to Docs Preview June 25, 2025 14:43 Inactive
@kvch kvch marked this pull request as ready for review June 27, 2025 09:46
kvch added a commit that referenced this pull request Jul 1, 2025
…traints (#930)

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
@kvch kvch force-pushed the fix-change-column-type-when-fk branch from ce7ef05 to 8eb4435 Compare July 1, 2025 10:32
@github-actions github-actions bot temporarily deployed to Docs Preview July 1, 2025 10:32 Inactive
@kvch kvch force-pushed the fix-change-column-type-when-fk branch from 8eb4435 to 0224bfe Compare July 1, 2025 10:32
@github-actions github-actions bot temporarily deployed to Docs Preview July 1, 2025 10:32 Inactive
Copy link

github-actions bot commented Jul 1, 2025

Merging this branch will decrease overall coverage

Impacted Packages Coverage Δ 🤖
github.com/xataio/pgroll/pkg/migrations 75.07% (-0.58%) 👎

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/xataio/pgroll/pkg/migrations/dbactions.go 81.04% (-6.59%) 211 (+25) 171 (+8) 40 (+17) 👎
github.com/xataio/pgroll/pkg/migrations/op_alter_column.go 87.96% (ø) 108 95 13

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

@kvch kvch requested a review from andrew-farries July 1, 2025 10:35
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.

Is the problem more general than this? AIUI any column referenced by a FK constraint can't be modified, not just primary key columns.

Comment on lines 726 to 848
Name: "02_alter_column",
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "people",
Column: "id",
Type: ptr("bigint"),
Up: "CAST(id AS bigint)",
Down: "SELECT CASE WHEN id < 2147483647 THEN CAST(id AS int) ELSE 0 END",
},
},
},
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this test is incorrect; shouldn't we be testing that the primary key field on the referenced table can be modified, ie events, not people?

Comment on lines 895 to 899
rows, err := a.conn.QueryContext(ctx, fmt.Sprintf(`
SELECT conname, r.conrelid::regclass, pg_catalog.pg_get_constraintdef(r.oid, true) as condef
FROM pg_catalog.pg_constraint r
WHERE confrelid = %s::regclass AND r.contype = 'f'`,
pq.QuoteIdentifier(a.table),
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we need this information, I think it should be present in the schema returned by read_schema. If we are missing information there we should add it to read_schema rather than running ad-hoc queries during the migration.

@github-actions github-actions bot temporarily deployed to Docs Preview July 21, 2025 14:01 Inactive
@kvch
Copy link
Contributor Author

kvch commented Jul 21, 2025

Depends on #946

@kvch kvch changed the base branch from main to fix-duplicate-backfilled-column August 7, 2025 17:22
@kvch kvch marked this pull request as draft August 7, 2025 17:22
Base automatically changed from fix-duplicate-backfilled-column to main August 12, 2025 10:55
@kvch kvch force-pushed the fix-change-column-type-when-fk branch from 77665f3 to 3177e00 Compare August 12, 2025 12:12
@github-actions github-actions bot temporarily deployed to Docs Preview August 12, 2025 12:12 Inactive
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.

Change Column Type when FK
2 participants