Skip to content

Commit 904248c

Browse files
authored
Merge pull request #331 from constructive-io/fix/named-exclude-constraint
fix(deparser): keep the constraint name on EXCLUDE constraints
2 parents 08cdc15 + b3f5c56 commit 904248c

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎__fixtures__/generated/generated.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21372,6 +21372,8 @@
2137221372
"misc/issues-18.sql": "SELECT (- (-10 - -12))::numeric AS delta",
2137321373
"misc/issues-19.sql": "SELECT (- (a.actual_eur - a.budget_eur))::numeric AS delta_eur FROM accounts a",
2137421374
"misc/issues-20.sql": "CREATE TABLE test_exclude_where (\n id uuid PRIMARY KEY,\n database_id uuid NOT NULL,\n status text NOT NULL DEFAULT 'pending',\n EXCLUDE USING btree (database_id WITH =)\n WHERE (status = 'pending')\n)",
21375+
"misc/issues-21.sql": "CREATE TABLE test_named_exclude (\n id uuid PRIMARY KEY,\n database_id uuid NOT NULL,\n status text NOT NULL DEFAULT 'pending',\n CONSTRAINT one_pending_per_database\n EXCLUDE USING btree (database_id WITH =)\n WHERE (status = 'pending')\n)",
21376+
"misc/issues-22.sql": "ALTER TABLE test_named_exclude ADD CONSTRAINT no_overlap EXCLUDE USING gist (room WITH =, during WITH &&)",
2137521377
"misc/inflection-1.sql": "CREATE SCHEMA inflection",
2137621378
"misc/inflection-2.sql": "GRANT USAGE ON SCHEMA inflection TO PUBLIC",
2137721379
"misc/inflection-3.sql": "ALTER DEFAULT PRIVILEGES IN SCHEMA inflection \n GRANT EXECUTE ON FUNCTIONS TO PUBLIC",

‎__fixtures__/kitchen-sink/misc/issues.sql‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ CREATE TABLE test_exclude_where (
9292
EXCLUDE USING btree (database_id WITH =)
9393
WHERE (status = 'pending')
9494
);
95+
96+
-- Named EXCLUDE constraint: the deparser dropped `CONSTRAINT <name>` for CONSTR_EXCLUSION
97+
CREATE TABLE test_named_exclude (
98+
id uuid PRIMARY KEY,
99+
database_id uuid NOT NULL,
100+
status text NOT NULL DEFAULT 'pending',
101+
CONSTRAINT one_pending_per_database
102+
EXCLUDE USING btree (database_id WITH =)
103+
WHERE (status = 'pending')
104+
);
105+
ALTER TABLE test_named_exclude ADD CONSTRAINT no_overlap EXCLUDE USING gist (room WITH =, during WITH &&);

‎packages/deparser/__tests__/kitchen-sink/misc-issues.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ it('misc-issues', async () => {
2323
"misc/issues-17.sql",
2424
"misc/issues-18.sql",
2525
"misc/issues-19.sql",
26-
"misc/issues-20.sql"
26+
"misc/issues-20.sql",
27+
"misc/issues-21.sql",
28+
"misc/issues-22.sql"
2729
]);
2830
});

‎packages/deparser/src/deparser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,7 @@ export class Deparser implements DeparserVisitor {
28082808
const output: string[] = [];
28092809

28102810
// Handle constraint name if present
2811-
if (node.conname && (node.contype === 'CONSTR_CHECK' || node.contype === 'CONSTR_UNIQUE' || node.contype === 'CONSTR_PRIMARY' || node.contype === 'CONSTR_FOREIGN' || node.contype === 'CONSTR_NOTNULL')) {
2811+
if (node.conname && (node.contype === 'CONSTR_CHECK' || node.contype === 'CONSTR_UNIQUE' || node.contype === 'CONSTR_PRIMARY' || node.contype === 'CONSTR_FOREIGN' || node.contype === 'CONSTR_NOTNULL' || node.contype === 'CONSTR_EXCLUSION')) {
28122812
output.push('CONSTRAINT');
28132813
output.push(QuoteUtils.quoteIdentifier(node.conname));
28142814
}

0 commit comments

Comments
 (0)