Repository navigation
Fix Schema Diff omitting DROP CONSTRAINT for primary key and unique constraints when a column is dropped. - #10814
Conversation
…key and unique constraints when a column is dropped. pgadmin-org#10813 Fixes pgadmin-org#10813 In _get_sql_to_delete_constraints(), checking whether a deleted primary key or unique constraint shared a column with columns_to_be_dropped used map(lambda x, y: x['column'] in y, c['columns'], data['columns_to_be_dropped']) and checked len(del_cols) == 0. Because map() returns a list of booleans whose length is non-zero whenever any column is dropped, DROP CONSTRAINT was omitted even when an unrelated column was dropped. In addition, two-argument map() paired lists positionally and performed substring matching, and INCLUDE columns were not checked. Check set intersection between the dropped columns and the constraint's key and INCLUDE columns instead, and add unit tests covering these cases.
|
No actionable comments were generated in the recent review. WalkthroughThe index-constraint deletion path now checks dropped columns against both key and INCLUDE columns. Unit tests cover when deletion SQL is generated and verify DROP-before-ADD ordering for recreated constraints. ChangesIndex constraint deletion
Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested reviewers: Merge Risk:
|
dpage
left a comment
There was a problem hiding this comment.
Thanks for this, and for the clear write-up in #10813.
The old check in _get_sql_to_delete_constraints() was broken in exactly the way you describe: map() always produced a non-empty list once any column was dropped, so DROP CONSTRAINT was skipped for every deleted primary key or unique constraint, whether or not it touched the dropped column. Replacing it with a set intersection against the key and INCLUDE columns is the right fix, and it also corrects the same misbehaviour in the table properties dialog, which goes through the same code path. The column names compare like for like (unquote_ident() on the key columns, raw attname for INCLUDE columns, raw names for the dropped columns), and since delete.sql uses DROP CONSTRAINT IF EXISTS and the column drops are emitted first, the skip is safe either way.
I ran the index_constraint test package against PostgreSQL 18 (58 passed) and confirmed that five of the new scenarios fail against the old code, so the tests do cover the bug. pycodestyle is clean.
Suggestion
-
test_index_constraint_delete_sql_unit.py: the first two scenarios ofTestIndexConstraintDeleteSqlUnitdiffer only in the constraint name, and_get_sql_to_delete_constraints()never sees the constraint type, so the unique constraint case duplicates the primary key one. The type-specific path is already covered byTestIndexConstraintDropAndAddSqlUnit, so you could drop the second scenario. Not a blocker. -
Correctness
-
Security
-
Error handling
-
Tests
-
Documentation (none needed)
Fixes #10813
In
_get_sql_to_delete_constraints()(web/pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/utils.py), dropping a primary key or unique constraint was skipped whenevercolumns_to_be_droppedwas non-empty:This had three issues:
map()produces a list of booleans (such as[False]), solen(del_cols) == 0wasFalsewhenever any column on the table was dropped—even a column unrelated to the constraint. As a result,DROP CONSTRAINTwas omitted whileADD CONSTRAINTwas still emitted, failing withmultiple primary keys for table ... are not allowed(or leaving stale unique constraints behind).map()zippedc['columns']anddata['columns_to_be_dropped']positionally and checkedx['column'] in y(a substring check against a single column name) rather than set membership against all dropped columns.INCLUDEcolumns (c.get('include')), which also cause PostgreSQL to drop the constraint automatically when dropped, were not checked.Changes
map()/len(del_cols) == 0check in_get_sql_to_delete_constraints()with a set intersection betweendata.get('columns_to_be_dropped')and the constraint's key +INCLUDEcolumns.test_index_constraint_delete_sql_unit.pycovering:DROP CONSTRAINT(primary key and unique constraint)DROP CONSTRAINTDROP CONSTRAINTINCLUDEcolumn skipsDROP CONSTRAINTDROP CONSTRAINTVerification
pycodestyle --config=.pycodestyle web/index_constraint/schema_difftest suites locally.Summary by CodeRabbit