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
3 changes: 3 additions & 0 deletions __fixtures__/generated/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -22009,6 +22009,9 @@
"misc/booleans-cast-1.sql": "SELECT * FROM myschema.mytable WHERE a = TRUE",
"misc/booleans-cast-2.sql": "SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)",
"misc/booleans-cast-3.sql": "SELECT * FROM myschema.mytable WHERE a = 't'::boolean",
"misc/alter-trigger-rename-1.sql": "ALTER TRIGGER p ON t RENAME TO q",
"misc/alter-trigger-rename-2.sql": "ALTER TRIGGER \"Mixed Trig\" ON \"My Schema\".\"My Table\" RENAME TO \"New Trig\"",
"misc/alter-trigger-rename-3.sql": "ALTER TRIGGER old_trigger ON public.t RENAME TO new_trigger",
"latest/postgres/create_view-1.sql": "CREATE FUNCTION interpt_pp(path, path)\n RETURNS point\n AS 'regresslib'\n LANGUAGE C STRICT",
"latest/postgres/create_view-2.sql": "CREATE TABLE real_city (\n\tpop\t\t\tint4,\n\tcname\t\ttext,\n\toutline \tpath\n)",
"latest/postgres/create_view-3.sql": "COPY real_city FROM 'filename'",
Expand Down
3 changes: 3 additions & 0 deletions __fixtures__/kitchen-sink/misc/alter-trigger-rename.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TRIGGER p ON t RENAME TO q;
ALTER TRIGGER "Mixed Trig" ON "My Schema"."My Table" RENAME TO "New Trig";
ALTER TRIGGER old_trigger ON public.t RENAME TO new_trigger;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { FixtureTestUtils } from '../../test-utils';
const fixtures = new FixtureTestUtils();

it('misc-alter-trigger-rename', async () => {
await fixtures.runFixtureTests([
"misc/alter-trigger-rename-1.sql",
"misc/alter-trigger-rename-2.sql",
"misc/alter-trigger-rename-3.sql"
]);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`non-pretty: misc/alter-trigger-rename-1.sql 1`] = `"ALTER TRIGGER p ON t RENAME TO q"`;

exports[`non-pretty: misc/alter-trigger-rename-2.sql 1`] = `"ALTER TRIGGER "Mixed Trig" ON "My Schema"."My Table" RENAME TO "New Trig""`;

exports[`non-pretty: misc/alter-trigger-rename-3.sql 1`] = `"ALTER TRIGGER old_trigger ON public.t RENAME TO new_trigger"`;

exports[`pretty: misc/alter-trigger-rename-1.sql 1`] = `"ALTER TRIGGER p ON t RENAME TO q"`;

exports[`pretty: misc/alter-trigger-rename-2.sql 1`] = `"ALTER TRIGGER "Mixed Trig" ON "My Schema"."My Table" RENAME TO "New Trig""`;
Comment on lines +5 to +11

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

Snapshot file has invalid unescaped quotes

The snapshot entries for misc/alter-trigger-rename-2.sql are invalid JavaScript: raw double quotes appear inside the double-quoted string literal (packages/deparser/tests/pretty/snapshots/alter-trigger-rename.test.ts.snap:5). The same defect appears at line 11 for the pretty: variant. Jest writes inner quotes escaped, so requiring this .snap file throws a SyntaxError, failing every test in alter-trigger-rename.test.ts and potentially aborting the whole Jest run.

📋 Prompt for AI Agents

In packages/deparser/tests/pretty/snapshots/alter-trigger-rename.test.ts.snap, lines 5 and 11 contain raw unescaped double quotes inside double-quoted string literals, which is a JavaScript syntax error that breaks the Jest test module at load. Fix by deleting the file and regenerating with jest -u, so Jest writes the correctly escaped serialization (inner quotes as \").

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a bug: the snapshot strings are backtick template literals, so inner double quotes are valid. Deleting and regenerating it with Jest gives byte-identical output, and it loads fine.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The hunk doesn't support that: the backticks only wrap the snapshot key (e.g. exports[non-pretty: ... 1]), which is standard Jest. The value after = is delimited by double quotes — = "ALTER TRIGGER "Mixed Trig" ON "My Schema"..." — with raw unescaped " inside, which is a SyntaxError at require time. Jest's pretty-format serializer writes inner double quotes as \", so the committed bytes here were not produced by Jest. If you believe the file on disk differs from this diff, please push the regenerated version (delete + jest -u) so we can verify; as shown, lines 5 and 11 are still invalid.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The value is a backtick literal too: each line is exports[key] = "ALTER TRIGGER "Mixed Trig" ...";, so the double quotes sit inside a template literal, which is Jest's standard format. I checked the file on main by require()-ing it, and it loads without errors.


exports[`pretty: misc/alter-trigger-rename-3.sql 1`] = `"ALTER TRIGGER old_trigger ON public.t RENAME TO new_trigger"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PrettyTest } from '../../test-utils/PrettyTest';

const prettyTest = new PrettyTest([
'misc/alter-trigger-rename-1.sql',
'misc/alter-trigger-rename-2.sql',
'misc/alter-trigger-rename-3.sql'
]);
prettyTest.generateTests();
7 changes: 5 additions & 2 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8477,6 +8477,9 @@ export class Deparser implements DeparserVisitor {
break;
case 'OBJECT_TRIGGER':
output.push('TRIGGER');
if (node.subname) {
output.push(QuoteUtils.quoteIdentifier(node.subname));
}
Comment on lines +8480 to +8482

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 · high

IF EXISTS emitted after trigger name

The new OBJECT_TRIGGER case pushes the quoted trigger name immediately after the TRIGGER keyword (packages/deparser/src/deparser.ts:8480-8482), but the missing_ok check emits IF EXISTS later, at packages/deparser/src/deparser.ts:8516-8518. A trigger rename with missing_ok set therefore deparses to ALTER TRIGGER "trg" IF EXISTS ON t RENAME TO x, which PostgreSQL's grammar (ALTER TRIGGER [IF EXISTS] name ON table RENAME TO ...) rejects as a syntax error, breaking round-trips of statements the parser accepts.

📋 Prompt for AI Agents

In packages/deparser/src/deparser.ts, RenameStmt handler: remove the if (node.subname) push from the case 'OBJECT_TRIGGER': block (lines ~8480-8482) and instead emit QuoteUtils.quoteIdentifier(node.subname) after the missing_ok / IF EXISTS block (after line ~8518), before the ON <relation> handling, so the order is ALTER TRIGGER [IF EXISTS] <name> ON <table> per the PostgreSQL grammar. Mirror the existing OBJECT_RULE handling at lines 8520-8524. The same defect exists for OBJECT_POLICY at lines 8459-8461 and should be fixed in the same pass. Add a fixture for ALTER TRIGGER IF EXISTS p ON t RENAME TO q.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostgreSQL doesn't accept IF EXISTS on ALTER TRIGGER ... RENAME: ALTER TRIGGER IF EXISTS p ON t RENAME TO q gives syntax error at or near "EXISTS", so missing_ok can't be set for triggers. No change needed.

break;
case 'OBJECT_TSCONFIGURATION':
output.push('TEXT SEARCH CONFIGURATION');
Expand Down Expand Up @@ -8522,8 +8525,8 @@ export class Deparser implements DeparserVisitor {
} else if (node.relation) {
const rangeVarContext = context.spawn('RenameStmt', { objtype: node.relationType });

// Add ON keyword for policy operations
if (node.renameType === 'OBJECT_POLICY') {
// Add ON keyword for policy and trigger operations
if (node.renameType === 'OBJECT_POLICY' || node.renameType === 'OBJECT_TRIGGER') {
output.push('ON');
}

Expand Down
Loading