Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"16": {
"libpg-query": "16.8.3",
"pgsql-parser": "16.2.0",
"pgsql-deparser": "16.2.0",
"pgsql-deparser": "16.2.1",
"@pgsql/types": "16.1.2",
"npmTag": "pg16"
},
Expand Down
135 changes: 132 additions & 3 deletions packages/deparser/scripts/generate-version-deparsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,138 @@ const versionConfigs: VersionConfig[] = [13, 14, 15, 16, 17].map(version => ({
directTransformerFile: `./v${version}-to-v18-direct`
}));

// Spellings the PG18 deparser prints that an older server cannot parse back to the same AST.
// They are corrected here, in the generated package only, so the PG18 output is left alone.

// PG13-16: the 16->17 upgrade qualifies a bare json type as pg_catalog.json, as the PG17 parser does
const KEEP_BARE_JSON_TYPE = `
const bare = (names: any, name: string): boolean =>
Array.isArray(names) && names.length === 1 && (names[0]?.String?.sval ?? names[0]?.String?.str) === name;

const v16to17 = (tx as any).v16to17;
const upgradeTypeName = v16to17.TypeName.bind(v16to17);
v16to17.TypeName = (node: any, context: any) => {
const out = upgradeTypeName(node, context);
if (bare(node.names, 'json')) out.TypeName.names = [{ String: { sval: 'json' } }];
return out;
};
const upgradeTypeCast = v16to17.TypeCast.bind(v16to17);
v16to17.TypeCast = (node: any, context: any) => {
const out = upgradeTypeCast(node, context);
const typeName = out.TypeCast.typeName?.TypeName ?? out.TypeCast.typeName;
if (typeName && bare((node.typeName?.TypeName ?? node.typeName)?.names, 'json')) {
typeName.names = [{ String: { sval: 'json' } }];
}
return out;
};
`;

// PG13-15: the 15->16 upgrade qualifies json_object() as pg_catalog.json_object(), as the PG16 parser does
const KEEP_BARE_JSON_OBJECT = `
const v15to16 = (tx as any).v15to16;
const upgradeFuncCall = v15to16.FuncCall.bind(v15to16);
v15to16.FuncCall = (node: any, context: any) => {
const out = upgradeFuncCall(node, context);
if (out.FuncCall && bare(node.funcname, 'json_object')) {
out.FuncCall.funcname = [{ String: { sval: 'json_object' } }];
}
return out;
};
`;

// PG13-16: ALTER OPERATOR ... SET (RESTRICT = NONE) needs the explicit NONE before PG17
const OPERATOR_OPTION_NONE = `
DefElem(node: any, context: any): string {
if (!node.arg && node.defaction !== 'DEFELEM_DROP' && context.parentNodeTypes.includes('AlterOperatorStmt')) {
return \`\${node.defname} = NONE\`;
}
return super.DefElem(node, context);
}
`;

// PG13-15: GRANT role ... WITH ADMIN OPTION is the only spelling before PG16
const GRANT_ROLE_ADMIN_OPTION = `
GrantRoleStmt(node: any, context: any): string {
const sql = super.GrantRoleStmt(node, context);
const opts = (Array.isArray(node.opt) ? node.opt : []).map((opt: any) => opt.DefElem);
const arg = opts[0]?.arg;
const off = arg?.Boolean ? arg.Boolean.boolval !== true : arg?.String?.sval === 'false';
if (!node.is_grant || opts.length !== 1 || opts[0]?.defname !== 'admin' || off) return sql;
// locate the option by what the statement prints without GRANTED BY, so a role name can never match
const head = node.grantor ? super.GrantRoleStmt({ ...node, grantor: undefined }, context) : sql;
const option = ' WITH ADMIN TRUE';
if (!head.endsWith(option) || !sql.startsWith(head)) return sql;
return head.slice(0, -option.length) + ' WITH ADMIN OPTION' + sql.slice(head.length);
}
`;

// PG13: every substring() spelling parses to pg_catalog.substring(), which only the SQL form prints back
const KEEP_CATALOG_SUBSTRING = `
const v13to14 = (tx as any).v13to14;
const upgradeSubstring = v13to14.FuncCall.bind(v13to14);
v13to14.FuncCall = (node: any, context: any) => {
const out = upgradeSubstring(node, context);
const [schema, name] = (node.funcname ?? []).map((part: any) => part?.String?.str ?? part?.String?.sval);
const argCount = node.args?.length;
if (out.FuncCall && node.funcname?.length === 2 && schema === 'pg_catalog' && name === 'substring' && (argCount === 2 || argCount === 3)) {
out.FuncCall.funcname = [{ String: { sval: 'pg_catalog' } }, { String: { sval: 'substring' } }];
out.FuncCall.funcformat = 'COERCE_SQL_SYNTAX';
}
return out;
};
`;

// PG13: REINDEX takes CONCURRENTLY after the object kind, not in the option list
const REINDEX_CONCURRENTLY = `
ReindexStmt(node: any, context: any): string {
const params = Array.isArray(node.params) ? node.params : [];
const others = params.filter((param: any) => param?.DefElem?.defname !== 'concurrently');
if (others.length === params.length) return super.ReindexStmt(node, context);
return super
.ReindexStmt({ ...node, params: others }, context)
.replace(/^REINDEX( \\([^)]*\\))? (INDEX|TABLE|SCHEMA|SYSTEM|DATABASE)/, match => \`\${match} CONCURRENTLY\`);
}
`;

// PG13-14: a publication lists tables as TABLE a, b; repeating TABLE per object is PG15 syntax
const PUBLICATION_TABLE_LIST = `
private publicationTableListed = false;

CreatePublicationStmt(node: any, context: any): string {
this.publicationTableListed = false;
return super.CreatePublicationStmt(node, context);
}

AlterPublicationStmt(node: any, context: any): string {
this.publicationTableListed = false;
return super.AlterPublicationStmt(node, context);
}

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 /, '');
}
Comment on lines +128 to +134

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

`;

function generateDeparserIndex(config: VersionConfig): string {
const { version } = config;
const upgradePatches = [
version <= 16 ? KEEP_BARE_JSON_TYPE : '',
version <= 15 ? KEEP_BARE_JSON_OBJECT : '',
version <= 13 ? KEEP_CATALOG_SUBSTRING : ''
].join('');
const deparserOverrides = [
version <= 16 ? OPERATOR_OPTION_NONE : '',
version <= 15 ? GRANT_ROLE_ADMIN_OPTION : '',
version <= 14 ? PUBLICATION_TABLE_LIST : '',
version <= 13 ? REINDEX_CONCURRENTLY : ''
].join('');

return `/**
* Deparser for PostgreSQL version ${config.version}
* Deparser for PostgreSQL version ${version}
* Auto-generated by generate-version-deparsers.ts
*/

Expand All @@ -32,7 +161,7 @@ import { Deparser as Deparser18, DeparserOptions } from './deparser';
import { ${config.directTransformerClass} } from '${config.directTransformerFile}';

const tx = new ${config.directTransformerClass}();

${upgradePatches}
type Query = Node | Node[] | ParseResult;

const toAst18 = (query: Query): any =>
Expand All @@ -46,7 +175,7 @@ export class Deparser extends Deparser18 {
static deparse(query: Query, opts: DeparserOptions = {}): string {
return new Deparser(query, opts).deparseQuery();
}
}
${deparserOverrides}}

export async function deparse(query: Query, opts?: DeparserOptions): Promise<string> {
return Deparser.deparse(query, opts);
Expand Down
34 changes: 31 additions & 3 deletions packages/transform-ast/src/transformers/v13-to-v14.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,11 +1255,14 @@ export class V13ToV14Transformer {

if (node.func !== undefined) {
// Handle plain object func (not wrapped in ObjectWithArgs)
if (typeof node.func === 'object' && !('ObjectWithArgs' in node.func) && 'objargs' in node.func) {
if (typeof node.func === 'object' && !('ObjectWithArgs' in node.func) && ('objargs' in node.func || 'objname' in node.func)) {
const funcResult: any = {};

if ((node.func as any).objname !== undefined) {
funcResult.objname = this.transform((node.func as any).objname, childContext);
const objname = (node.func as any).objname;
if (objname !== undefined) {
funcResult.objname = Array.isArray(objname)
? objname.map((item: any) => this.transform(item, childContext))
: this.transform(objname, childContext);
}

if ((node.func as any).objargs !== undefined) {
Expand All @@ -1270,6 +1273,10 @@ export class V13ToV14Transformer {
: [this.createFunctionParameterFromTypeName((node.func as any).objargs, childContext, 0)];
}

if ((node.func as any).args_unspecified !== undefined) {
funcResult.args_unspecified = (node.func as any).args_unspecified;
}

result.func = funcResult;
} else {
const funcResult = this.transform(node.func as any, childContext);
Expand Down Expand Up @@ -2898,6 +2905,15 @@ export class V13ToV14Transformer {
return true;
}

AlterSubscriptionStmt(node: PG13.AlterSubscriptionStmt, context: TransformerContext): { AlterSubscriptionStmt: PG14.AlterSubscriptionStmt } {
const result: any = this.transformGenericNode(node, context);
// PG14 added ADD / DROP PUBLICATION and renamed the existing SET PUBLICATION kind
if (result.kind === 'ALTER_SUBSCRIPTION_PUBLICATION') {
result.kind = 'ALTER_SUBSCRIPTION_SET_PUBLICATION';
}
return { AlterSubscriptionStmt: result };
}

AlterSeqStmt(node: any, context: TransformerContext): { AlterSeqStmt: PG14.AlterSeqStmt } {
const result: any = {};

Expand Down Expand Up @@ -3402,6 +3418,18 @@ export class V13ToV14Transformer {
result.params = this.transform(nodeAny.params, context);
}

if (nodeAny.concurrent === true) {
result.params = [
...(result.params || []),
{
DefElem: {
defname: 'concurrently',
defaction: 'DEFELEM_UNSPEC'
}
}
];
}

return { ReindexStmt: result };
}
}
33 changes: 33 additions & 0 deletions packages/transform-ast/src/transformers/v14-to-v15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,21 @@ export class V14ToV15Transformer {
return { StatsElem: result };
}

// PG15 replaced the publication's list of tables with a list of PublicationObjSpec
private publicationTablesToObjects(result: any): void {
if (result.tables === undefined) return;
result.pubobjects = (Array.isArray(result.tables) ? result.tables : [result.tables]).map((table: any) => ({
PublicationObjSpec: {
pubobjtype: 'PUBLICATIONOBJ_TABLE',
pubtable: { relation: table.RangeVar ?? table }
}
}));
delete result.tables;
}

CreatePublicationStmt(node: PG14.CreatePublicationStmt, context: TransformerContext): { CreatePublicationStmt: PG15.CreatePublicationStmt } {
const result = this.transformGenericNode(node, context);
this.publicationTablesToObjects(result);
return { CreatePublicationStmt: result };
}

Expand All @@ -1013,11 +1026,31 @@ export class V14ToV15Transformer {

AlterPublicationStmt(node: PG14.AlterPublicationStmt, context: TransformerContext): { AlterPublicationStmt: PG15.AlterPublicationStmt } {
const result = this.transformGenericNode(node, context);
this.publicationTablesToObjects(result);
if (result.tableAction !== undefined) {
const actions: Record<string, string> = {
DEFELEM_ADD: 'AP_AddObjects',
DEFELEM_DROP: 'AP_DropObjects',
DEFELEM_SET: 'AP_SetObjects'
};
if (actions[result.tableAction]) {
result.action = actions[result.tableAction];
}
delete result.tableAction;
}
return { AlterPublicationStmt: result };
}

AlterSubscriptionStmt(node: PG14.AlterSubscriptionStmt, context: TransformerContext): { AlterSubscriptionStmt: PG15.AlterSubscriptionStmt } {
const result = this.transformGenericNode(node, context);
// ENABLE / DISABLE carried an Integer before PG15 introduced the Boolean node
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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

return { DefElem: { ...option.DefElem, arg: { Boolean: { boolval: ival.ival === 1 } } } };
});
}
return { AlterSubscriptionStmt: result };
}

Expand Down
14 changes: 14 additions & 0 deletions packages/transform-ast/src/transformers/v15-to-v16.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,10 @@ export class V15ToV16Transformer {
result.storage = node.storage;
}

if (node.compression !== undefined) {
result.compression = node.compression;
}

if (node.raw_default !== undefined) {
result.raw_default = this.transform(node.raw_default as any, context);
}
Expand Down Expand Up @@ -1242,6 +1246,12 @@ export class V15ToV16Transformer {
result.fk_del_action = node.fk_del_action;
}

if (node.fk_del_set_cols !== undefined) {
result.fk_del_set_cols = Array.isArray(node.fk_del_set_cols)
? node.fk_del_set_cols.map((item: any) => this.transform(item as any, context))
: this.transform(node.fk_del_set_cols as any, context);
}

if (node.old_conpfeqop !== undefined) {
result.old_conpfeqop = Array.isArray(node.old_conpfeqop)
? node.old_conpfeqop.map((item: any) => this.transform(item as any, context))
Expand Down Expand Up @@ -3336,6 +3346,10 @@ export class V15ToV16Transformer {
: this.transform(nodeAny.opt as any, context);
}

if (node.grantor !== undefined) {
result.grantor = node.grantor;
}

return { GrantRoleStmt: result };
}

Expand Down
6 changes: 6 additions & 0 deletions packages/transform-ast/src/transformers/v16-to-v17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,12 @@ export class V16ToV17Transformer {
if (node.storage !== undefined) {
result.storage = node.storage;
}
if (node.storage_name !== undefined) {
result.storage_name = node.storage_name;
}
if (node.compression !== undefined) {
result.compression = node.compression;
}
if (node.raw_default !== undefined) {
result.raw_default = this.transform(node.raw_default as any, context);
}
Expand Down
Loading