Schema Diff: ignore defseqrelid to avoid false SERIAL/BIGSERIAL diffs and invalid ALTER SQL (#10236) - #10248
Open
agu2347 wants to merge 1 commit into
Open
Conversation
…pgadmin-org#10236) get_formatted_columns() reprojects a column as SERIAL/BIGSERIAL/SMALLSERIAL when it owns (pg_depend, deptype='a' -> seqrelid) the same sequence referenced by its own nextval() DEFAULT (pg_attrdef -> defseqrelid, pgadmin-org#9896/pgadmin-org#10100/pgadmin-org#10101). Both seqrelid and defseqrelid survive into the column dict that Schema Diff compares, but they are raw sequence OIDs that are meaningless to compare across two independently-created databases -- two databases can have byte-for-byte identical BIGSERIAL columns while their owned sequences get assigned completely different OIDs. column_keys_to_ignore already excluded seqrelid, but not its sibling defseqrelid. That gap caused: * SchemaDiffTableCompare.compare() / are_dictionaries_identical() to report a table as different purely because of a benign OID mismatch, even when every user-visible column property (cltype included) was identical. * compare_target_cols() to add the column to the 'changed' diff list, which downstream generates SQL via the column update.sql template. Since data.cltype == o_data.cltype ('bigserial' on both sides), this produced the invalid statement reported in the issue: `ALTER COLUMN adl_id TYPE bigserial;` Adding 'defseqrelid' to column_keys_to_ignore fixes both symptoms at the source: the column is no longer considered different at all, so neither the false-positive diff nor the invalid ALTER COLUMN statement is generated. Added a unit test (test_schema_diff_serial_column_ignore.py, mirroring the existing test_serial_detection_unit.py convention) covering: * two BIGSERIAL columns differing only by defseqrelid are not flagged * a genuine difference on an otherwise-identical BIGSERIAL column is still detected (proves the fix doesn't mask real diffs) Fixes pgadmin-org#10236
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
WalkthroughThe schema diff now ignores ChangesSerial schema-diff handling
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Schema Diff reports a false-positive difference (and generates invalid SQL) for structurally identical SERIAL/BIGSERIAL/SMALLSERIAL columns across two databases.
Fixes #10236
Root cause
get_formatted_columns()reprojects a column as SERIAL/BIGSERIAL/SMALLSERIAL when it owns (pg_depend,deptype='a'->seqrelid) the same sequence referenced by its ownnextval()DEFAULT (pg_attrdef->defseqrelid; see #9896/#10100/#10101). Bothseqrelidanddefseqrelidsurvive into the column dict that Schema Diff compares, but they are raw sequence OIDs -- meaningless to compare across two independently-created databases. Two databases can have byte-for-byte identical BIGSERIAL columns while their owned sequences get assigned completely different OIDs.SchemaDiffTableCompare.column_keys_to_ignorealready excludedseqrelid, but not its siblingdefseqrelid. That gap caused two symptoms from the same root cause:compare()/are_dictionaries_identical()reports a table as different purely because of the benigndefseqrelidmismatch, even when every user-visible column property (includingcltype) is identical.compare_target_cols()adds the column to thechangeddiff list. Downstream SQL generation (columns/sql/*/update.sql) then rendersALTER COLUMN ... TYPE {cltype}for that column -- even thoughdata.cltype == o_data.cltype('bigserial'on both sides) -- producing the invalid statement reported in the issue:ALTER COLUMN adl_id TYPE bigserial;.Fix
Add
'defseqrelid'toSchemaDiffTableCompare.column_keys_to_ignore, alongside its existing sibling'seqrelid'. This fixes both symptoms at the source: the column is no longer considered different at all, so neither the false-positive diff nor the invalidALTER COLUMNstatement is generated.Testing
Added
tests/test_schema_diff_serial_column_ignore.py, mirroring the existingcolumns/tests/test_serial_detection_unit.pyunit-test convention (BaseTestGenerator+ mock dict fixtures, no live DB needed for the logic under test):defseqrelidare not flagged as added/changed.NOT NULLtoggled) on an otherwise-identical BIGSERIAL column is still detected, proving the fix doesn't mask real diffs.Since standing up the full Postgres-backed regression harness in my sandbox was impractical for this change, I additionally verified the actual patched
SchemaDiffTableCompare.compare_target_cols()logic in isolation (loading the real class source directly, bypassing only the unrelated Flask/DB-heavy base-class import) with 6 checks, including a check that reverting the fix reproduces the exact false positive described in the issue (proving this is a genuine regression guard, not a tautological test). All passed.pycodestyle --config=.pycodestyle(the project's actual CI style gate, confirmed via.github/workflows/check-python-style.yml) is clean on both changed files.Summary by CodeRabbit
NOT NULLdifferences, continue to be detected.