diff --git a/config/versions.json b/config/versions.json index ce39f3c3..4e41edf6 100644 --- a/config/versions.json +++ b/config/versions.json @@ -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" }, diff --git a/packages/deparser/scripts/generate-version-deparsers.ts b/packages/deparser/scripts/generate-version-deparsers.ts index ec9b6101..80e08a4b 100644 --- a/packages/deparser/scripts/generate-version-deparsers.ts +++ b/packages/deparser/scripts/generate-version-deparsers.ts @@ -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 /, ''); + } +`; + 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 */ @@ -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 => @@ -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 { return Deparser.deparse(query, opts); diff --git a/packages/transform-ast/src/transformers/v13-to-v14.ts b/packages/transform-ast/src/transformers/v13-to-v14.ts index ecfbb091..2e5fa6ff 100644 --- a/packages/transform-ast/src/transformers/v13-to-v14.ts +++ b/packages/transform-ast/src/transformers/v13-to-v14.ts @@ -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) { @@ -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); @@ -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 = {}; @@ -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 }; } } diff --git a/packages/transform-ast/src/transformers/v14-to-v15.ts b/packages/transform-ast/src/transformers/v14-to-v15.ts index 3e0ff952..10e2c29d 100644 --- a/packages/transform-ast/src/transformers/v14-to-v15.ts +++ b/packages/transform-ast/src/transformers/v14-to-v15.ts @@ -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 }; } @@ -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 = { + 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; + return { DefElem: { ...option.DefElem, arg: { Boolean: { boolval: ival.ival === 1 } } } }; + }); + } return { AlterSubscriptionStmt: result }; } diff --git a/packages/transform-ast/src/transformers/v15-to-v16.ts b/packages/transform-ast/src/transformers/v15-to-v16.ts index 454f5ae2..25c2450f 100644 --- a/packages/transform-ast/src/transformers/v15-to-v16.ts +++ b/packages/transform-ast/src/transformers/v15-to-v16.ts @@ -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); } @@ -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)) @@ -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 }; } diff --git a/packages/transform-ast/src/transformers/v16-to-v17.ts b/packages/transform-ast/src/transformers/v16-to-v17.ts index f252712d..c943ab74 100644 --- a/packages/transform-ast/src/transformers/v16-to-v17.ts +++ b/packages/transform-ast/src/transformers/v16-to-v17.ts @@ -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); }