Repository navigation
fix: make the pg13β17 lanes round-trip every statement valid for their major - #381
Conversation
β¦r major transform-ast: keep fields the upgrade chain was dropping (column COMPRESSION/STORAGE, FK SET NULL/DEFAULT column lists, GRANTED BY, REINDEX CONCURRENTLY, publication tables, ALTER SUBSCRIPTION kind/enabled, ALTER PROCEDURE name). generate-version-deparsers: the generated pg13-16 packages override the few spellings the PG18 deparser prints that an older server cannot parse back. packages/deparser/src is untouched, so PG18 output does not change. config: pgsql-deparser 16.2.1 (16.2.0 is already published without these fixes).
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
Review complete. π‘ 2 medium π¬ Inline comments (2)
π§Ή Nitpicks (1) β π’ 1 low
The change regenerates per-PG-version deparsers with targeted post-processing overrides and fixes several cross-version AST shape mismatches so PG13-14 SQL upgrades and re-deparses correctly for newer servers.
Two medium-severity defects remain: the Reviewed commit: a9c9fc0 |
There was a problem hiding this comment.
This PR adds version-specific deparser monkey-patches and PG13-17 AST transformer fixes (subscription enable/disable, publication object lists, REINDEX CONCURRENTLY, GRANT ADMIN OPTION, bare json/json_object) plus a pgsql-deparser 16.2.1 version bump.
Key findings
- π‘
ALTER SUBSCRIPTION ... DISABLEskips Boolean conversion β v14-to-v15.ts:1050 - π‘ Publication table-list flag breaks interleaved object kinds β generate-version-deparsers.ts:128
| if (result.kind === 'ALTER_SUBSCRIPTION_ENABLED' && Array.isArray(result.options)) { | ||
| result.options = result.options.map((option: any) => { | ||
| const ival = option?.DefElem?.arg?.Integer; | ||
| if (option?.DefElem?.defname !== 'enabled' || !ival) return option; |
There was a problem hiding this comment.
π‘ bug Β· medium
ALTER SUBSCRIPTION ... DISABLE skips Boolean conversion
In packages/transform-ast/src/transformers/v14-to-v15.ts:1050, the guard !ival treats an Integer node with ival === 0 as absent, so ALTER SUBSCRIPTION ... DISABLE (enabled=0) keeps a raw Integer arg in the PG15 output where a Boolean node is required. Deparsers and consumers expecting the PG15 shape receive an unexpected node for the most common disable case.
π Prompt for AI Agents
In packages/transform-ast/src/transformers/v14-to-v15.ts at line 1050, inside AlterSubscriptionStmt, replace the guard if (option?.DefElem?.defname !== 'enabled' || !ival) return option; with a check that does not treat 0 as missing, e.g. if (option?.DefElem?.defname !== 'enabled' || typeof ival?.ival !== 'number') return option; so Integer { ival: 0 } maps to Boolean { boolval: false } as intended for DISABLE.
| PublicationObjSpec(node: any, context: any): string { | ||
| const sql = super.PublicationObjSpec(node, context); | ||
| if (node.pubobjtype !== 'PUBLICATIONOBJ_TABLE') return sql; | ||
| const first = !this.publicationTableListed; | ||
| this.publicationTableListed = true; | ||
| return first ? sql : sql.replace(/^TABLE /, ''); | ||
| } |
There was a problem hiding this comment.
π‘ bug Β· medium
Publication table-list flag breaks interleaved object kinds
The generated PublicationObjSpec override strips the leading TABLE from every object after the first PUBLICATIONOBJ_TABLE, keyed on a state flag that is only reset at CreatePublicationStmt/AlterPublicationStmt entry (packages/deparser/scripts/generate-version-deparsers.ts:128-132). For a publication whose objects interleave kinds, FOR TABLE a, TABLES IN SCHEMA s, TABLE b, the flag set by the first object is never reset, so the third is deparsed as bare b. PG13/14 grammar only accepts TABLE <list> or TABLES IN SCHEMA ... per object, so the round-trip silently emits SQL those servers reject.
π Prompt for AI Agents
In packages/deparser/scripts/generate-version-deparsers.ts, in the PUBLICATION_TABLE_LIST template (lines 115-135), inside PublicationObjSpec, when node.pubobjtype !== 'PUBLICATIONOBJ_TABLE', set this.publicationTableListed = false before returning sql. This ensures a bare table name is emitted only when the previous object in the list was also a TABLE object; otherwise PG13/14 output like FOR TABLE a, TABLES IN SCHEMA s, b is unparseable.
Summary
Follow-up to #379 and #380. The generated pg13β17 packages (old AST β
@pgsql/transform-astβ PG18 AST β PG18 deparser) still failed parse β deparse β re-parse on some kitchen-sink statements. With this PR every statement that the major's own parser accepts round-trips, on all five lanes.packages/deparser/srcandpackages/parser/srcare not touched: PG18 output does not change.1.
transform-ast: fields the upgrade chain was droppingThese are plain bugs in the upgrade step; each fix copies or maps the missing field.
ReindexStmtconcurrent(now aconcurrentlyparam)AlterFunctionStmtfunc.objnameturned into{ "0": β¦ }when there are no args (ALTER PROCEDURE f() VOLATILE)AlterSubscriptionStmtALTER_SUBSCRIPTION_PUBLICATIONnot renamed toALTER_SUBSCRIPTION_SET_PUBLICATIONCreatePublicationStmt/AlterPublicationStmttables/tableActionnot converted topubobjects/actionAlterSubscriptionStmtenabledstayed anInteger, soDISABLEprinted asENABLEColumnDef,Constraint,GrantRoleStmtcompression,fk_del_set_cols,grantorColumnDefstorage_name,compressionThis code only runs when upgrading a 13β17 AST; a PG18 AST never passes through it.
2.
generate-version-deparsers.ts: old-server spellings, in the generated packages onlyThe PG18 deparser prints a few forms an older server cannot parse back to the same AST. Rather than change the PG18 deparser, the generated
index.tsfor the affected majors overrides them:GRANT role β¦ WITH ADMIN OPTIONinstead ofWITH ADMIN TRUEALTER OPERATOR β¦ SET (restrict = NONE)instead ofSET (restrict)jsontype bare (the 16β17 upgrade qualifies it aspg_catalog.json)json_object()bare (the 15β16 upgrade qualifies it)TABLE a, bin publications instead ofTABLE a, TABLE bREINDEX TABLE CONCURRENTLY xinstead ofREINDEX (CONCURRENTLY) TABLE xsubstring()always printed in its SQL form, the one spelling that re-parses topg_catalog.substringThe json /
substringoverrides wrap methods of the per-step transformers held by the lane'sPGnnToPG18Transformer(reached through its privatev16to17/v15to16/v13to14fields), so they depend on those field names.3.
config/versions.jsonpgsql-deparserfor 16 β16.2.1;16.2.0is already on npm without these fixes. The 17 lane's output is unchanged, so17.19.0stands.Results
Clean regeneration and build of all lanes from this branch; ASTs compared ignoring
location,stmt_location,stmt_len. Kitchen-sink is 24,021 statements, each run with pretty off and on.packages/deparser(316 suites, 424 snapshots) andpackages/transform-ast(1,443 tests) pass unchanged.Review notes
prepare-versions, so the numbers above come from a local harness.pgsql-deparserat the local lane builds, since 13.20.0 / 14.2.0 / 15.2.0 / 16.2.1 are not on npm yet.Link to Devin session: https://app.devin.ai/sessions/20f34d49f2df465b8a7c94defbd6ac9a
Open in Devin Desktop: https://app.devin.ai/desktop/session/20f34d49f2df465b8a7c94defbd6ac9a?variant=devin
Requested by: @pyramation
π’ Devin Review found nothing to fix Β· View findings Β· Follow Devin's work