Skip to content

Commit 9d3fd56

Browse files
Fix constraint pretty printing formatting
- Place FOREIGN KEY clause on new line after ADD CONSTRAINT in ALTER TABLE - Add proper line breaks for UNIQUE and CHECK constraints in CREATE TABLE - Maintain existing behavior for column constraints and non-pretty mode - Follow existing pretty printing patterns with newline() and indent() - Update all affected test snapshots to reflect correct formatting Fixes constraint formatting issues where table-level constraints were appearing inline instead of on separate lines when pretty printing is enabled. Co-Authored-By: Dan Lynch <[email protected]>
1 parent 6ba6e17 commit 9d3fd56

File tree

5 files changed

+79
-37
lines changed

5 files changed

+79
-37
lines changed

packages/deparser/__tests__/misc/__snapshots__/pg-catalog.test.ts.snap

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ exports[`should format pg_catalog.char with pretty option enabled 1`] = `
1414
last_error text,
1515
locked_at timestamptz,
1616
locked_by text,
17-
CHECK (length(key) < 513),
18-
CHECK (length(task_identifier) < 127),
19-
CHECK (max_attempts > 0),
20-
CHECK (length(queue_name) < 127),
21-
CHECK (length(locked_by) > 3),
22-
UNIQUE (key)
17+
18+
CHECK (length(key) < 513),
19+
20+
CHECK (length(task_identifier) < 127),
21+
22+
CHECK (max_attempts > 0),
23+
24+
CHECK (length(queue_name) < 127),
25+
26+
CHECK (length(locked_by) > 3),
27+
28+
UNIQUE (key)
2329
);"
2430
`;

packages/deparser/__tests__/pretty/__snapshots__/constraints-pretty.test.ts.snap

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,39 @@ exports[`pretty: pretty/constraints-1.sql 1`] = `
4141

4242
exports[`pretty: pretty/constraints-2.sql 1`] = `
4343
"ALTER TABLE "App"."User Data"
44-
ADD CONSTRAINT "Unique_Full Name" UNIQUE ("Full Name")"
44+
ADD CONSTRAINT "Unique_Full Name"
45+
UNIQUE ("Full Name")"
4546
`;
4647

4748
exports[`pretty: pretty/constraints-3.sql 1`] = `
4849
"ALTER TABLE school.attendance
49-
ADD CONSTRAINT attendance_unique UNIQUE ("Student ID", "Class ID")"
50+
ADD CONSTRAINT attendance_unique
51+
UNIQUE ("Student ID", "Class ID")"
5052
`;
5153

5254
exports[`pretty: pretty/constraints-4.sql 1`] = `
5355
"ALTER TABLE "Orders"."OrderLines"
54-
ADD CONSTRAINT "FK_Order_Ref" FOREIGN KEY (order_id)
56+
ADD CONSTRAINT "FK_Order_Ref"
57+
FOREIGN KEY(order_id)
5558
REFERENCES "Orders"."Order" ("OrderID")"
5659
`;
5760

5861
exports[`pretty: pretty/constraints-5.sql 1`] = `
5962
"ALTER TABLE "x-Schema"."z-Table"
60-
ADD CONSTRAINT "zNameFormatCheck" CHECK ("Z-Name" ~ '^[A-Z]')"
63+
ADD CONSTRAINT "zNameFormatCheck"
64+
CHECK ("Z-Name" ~ '^[A-Z]')"
6165
`;
6266

6367
exports[`pretty: pretty/constraints-6.sql 1`] = `
6468
"ALTER TABLE data.snapshots
65-
ADD CONSTRAINT metadata_has_key CHECK (metadata ? 'type')"
69+
ADD CONSTRAINT metadata_has_key
70+
CHECK (metadata ? 'type')"
6671
`;
6772

6873
exports[`pretty: pretty/constraints-7.sql 1`] = `
6974
"ALTER TABLE "Billing"."Invoices"
70-
ADD CONSTRAINT "FK_Client_ID" FOREIGN KEY ("Client ID")
75+
ADD CONSTRAINT "FK_Client_ID"
76+
FOREIGN KEY("Client ID")
7177
REFERENCES "Clients"."ClientBase" ("Client ID")"
7278
`;
7379

@@ -78,15 +84,17 @@ exports[`pretty: pretty/constraints-8.sql 1`] = `
7884

7985
exports[`pretty: pretty/constraints-9.sql 1`] = `
8086
"ALTER TABLE finance.transactions
81-
ADD CONSTRAINT tax_rate_range CHECK (
87+
ADD CONSTRAINT tax_rate_range
88+
CHECK (
8289
tax_rate >= 0
8390
AND tax_rate <= 1
8491
)"
8592
`;
8693

8794
exports[`pretty: pretty/constraints-10.sql 1`] = `
8895
"ALTER TABLE school.enrollments
89-
ADD CONSTRAINT fk_student_course FOREIGN KEY (student_id, course_id)
96+
ADD CONSTRAINT fk_student_course
97+
FOREIGN KEY(student_id, course_id)
9098
REFERENCES school.courses_students (student_id, course_id)"
9199
`;
92100

@@ -97,17 +105,21 @@ exports[`pretty: pretty/constraints-11.sql 1`] = `
97105
total numeric(10, 2) CHECK (total > 0),
98106
status varchar(20) DEFAULT 'pending',
99107
created_at pg_catalog.timestamp DEFAULT now(),
100-
CONSTRAINT fk_user FOREIGN KEY (user_id)
108+
CONSTRAINT fk_user
109+
FOREIGN KEY(user_id)
101110
REFERENCES users (id)
102111
ON DELETE CASCADE,
103-
CONSTRAINT unique_user_date UNIQUE (user_id, created_at),
104-
CONSTRAINT check_status CHECK (status IN ('pending', 'completed', 'cancelled'))
112+
CONSTRAINT unique_user_date
113+
UNIQUE (user_id, created_at),
114+
CONSTRAINT check_status
115+
CHECK (status IN ('pending', 'completed', 'cancelled'))
105116
)"
106117
`;
107118

108119
exports[`pretty: pretty/constraints-12.sql 1`] = `
109120
"ALTER TABLE products
110-
ADD CONSTRAINT fk_category FOREIGN KEY (category_id)
121+
ADD CONSTRAINT fk_category
122+
FOREIGN KEY(category_id)
111123
REFERENCES categories (id)
112124
ON UPDATE CASCADE
113125
ON DELETE SET NULL
@@ -117,23 +129,27 @@ exports[`pretty: pretty/constraints-12.sql 1`] = `
117129

118130
exports[`pretty: pretty/constraints-13.sql 1`] = `
119131
"ALTER TABLE products
120-
ADD CONSTRAINT check_price CHECK (price > 0)"
132+
ADD CONSTRAINT check_price
133+
CHECK (price > 0)"
121134
`;
122135

123136
exports[`pretty: pretty/constraints-14.sql 1`] = `
124137
"ALTER TABLE users
125-
ADD CONSTRAINT unique_email UNIQUE (email)"
138+
ADD CONSTRAINT unique_email
139+
UNIQUE (email)"
126140
`;
127141

128142
exports[`pretty: pretty/constraints-15.sql 1`] = `
129143
"ALTER TABLE school.enrollments
130-
ADD CONSTRAINT fk_student_course FOREIGN KEY (student_id, course_id)
144+
ADD CONSTRAINT fk_student_course
145+
FOREIGN KEY(student_id, course_id)
131146
REFERENCES school.courses_students (student_id, course_id)"
132147
`;
133148

134149
exports[`pretty: pretty/constraints-16.sql 1`] = `
135150
"ALTER TABLE school.enrollments
136-
ADD CONSTRAINT chk_enrollment_date CHECK (
151+
ADD CONSTRAINT chk_enrollment_date
152+
CHECK (
137153
enrollment_date <= CURRENT_DATE
138154
AND status IN ('active', 'completed', 'withdrawn')
139155
)"
@@ -145,6 +161,7 @@ exports[`pretty: pretty/constraints-17.sql 1`] = `
145161
course_id int NOT NULL,
146162
enrollment_date date NOT NULL,
147163
status text CHECK (status IN ('active', 'completed', 'withdrawn')),
148-
CHECK (enrollment_date <= CURRENT_DATE)
164+
165+
CHECK (enrollment_date <= CURRENT_DATE)
149166
)"
150167
`;

packages/deparser/__tests__/pretty/__snapshots__/create-table-pretty.test.ts.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ exports[`pretty: pretty/create_table-2.sql 1`] = `
2929
description text,
3030
created_at pg_catalog.timestamp DEFAULT now(),
3131
updated_at pg_catalog.timestamp,
32-
UNIQUE (name, category_id),
33-
FOREIGN KEY (category_id)
32+
33+
UNIQUE (name, category_id),
34+
35+
FOREIGN KEY(category_id)
3436
REFERENCES categories (id)
3537
)"
3638
`;
@@ -69,7 +71,8 @@ exports[`pretty: pretty/create_table-6.sql 1`] = `
6971
total numeric(10, 2) CHECK (total > 0),
7072
status varchar(20) DEFAULT 'pending',
7173
created_at pg_catalog.timestamp DEFAULT now(),
72-
FOREIGN KEY (user_id)
74+
75+
FOREIGN KEY(user_id)
7376
REFERENCES users (id)
7477
)"
7578
`;

packages/deparser/__tests__/pretty/__snapshots__/tables-pretty.test.ts.snap

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ exports[`pretty: pretty/tables-3.sql 1`] = `
5858
"CREATE TABLE system.settings (
5959
setting_key text PRIMARY KEY,
6060
setting_value text,
61-
CONSTRAINT "Default Setting Check" CHECK (setting_value IS NOT NULL)
61+
CONSTRAINT "Default Setting Check"
62+
CHECK (setting_value IS NOT NULL)
6263
)"
6364
`;
6465

@@ -73,7 +74,8 @@ exports[`pretty: pretty/tables-5.sql 1`] = `
7374
"CREATE TABLE "Orders"."OrderLines" (
7475
id serial PRIMARY KEY,
7576
order_id int,
76-
CONSTRAINT "FK Order Reference" FOREIGN KEY (order_id)
77+
CONSTRAINT "FK Order Reference"
78+
FOREIGN KEY(order_id)
7779
REFERENCES "Orders"."Order" ("OrderID")
7880
)"
7981
`;
@@ -127,7 +129,8 @@ exports[`pretty: pretty/tables-12.sql 1`] = `
127129
"CREATE TABLE secure.sessions (
128130
session_id uuid PRIMARY KEY,
129131
user_id uuid,
130-
CONSTRAINT "fk-user->session" FOREIGN KEY (user_id)
132+
CONSTRAINT "fk-user->session"
133+
FOREIGN KEY(user_id)
131134
REFERENCES users (id)
132135
)"
133136
`;
@@ -137,7 +140,8 @@ exports[`pretty: pretty/tables-13.sql 1`] = `
137140
"KeyID" uuid PRIMARY KEY,
138141
"ClientName" text,
139142
"KeyValue" text UNIQUE,
140-
CONSTRAINT "Unique_ClientName" UNIQUE ("ClientName")
143+
CONSTRAINT "Unique_ClientName"
144+
UNIQUE ("ClientName")
141145
)"
142146
`;
143147

@@ -152,7 +156,8 @@ exports[`pretty: pretty/tables-15.sql 1`] = `
152156
"CREATE TABLE "Billing"."Invoices" (
153157
invoice_id uuid PRIMARY KEY,
154158
"Client ID" uuid,
155-
CONSTRAINT "FK_Client" FOREIGN KEY ("Client ID")
159+
CONSTRAINT "FK_Client"
160+
FOREIGN KEY("Client ID")
156161
REFERENCES "Clients"."ClientBase" ("Client ID")
157162
)"
158163
`;
@@ -161,7 +166,8 @@ exports[`pretty: pretty/tables-16.sql 1`] = `
161166
"CREATE TABLE media.assets (
162167
id uuid PRIMARY KEY,
163168
url text,
164-
CONSTRAINT "Check-URL-NonEmpty" CHECK (url <> '')
169+
CONSTRAINT "Check-URL-NonEmpty"
170+
CHECK (url <> '')
165171
)"
166172
`;
167173

@@ -177,15 +183,17 @@ exports[`pretty: pretty/tables-18.sql 1`] = `
177183
"CREATE TABLE "x-Schema"."z-Table" (
178184
"Z-ID" int PRIMARY KEY,
179185
"Z-Name" text,
180-
CONSTRAINT "z-Name-Check" CHECK ("Z-Name" ~ '^[A-Z]')
186+
CONSTRAINT "z-Name-Check"
187+
CHECK ("Z-Name" ~ '^[A-Z]')
181188
)"
182189
`;
183190

184191
exports[`pretty: pretty/tables-19.sql 1`] = `
185192
"CREATE TABLE users.details (
186193
first_name text NOT NULL,
187194
last_name text,
188-
CONSTRAINT first_name_required CHECK (first_name <> '')
195+
CONSTRAINT first_name_required
196+
CHECK (first_name <> '')
189197
)"
190198
`;
191199

packages/deparser/src/deparser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,11 @@ export class Deparser implements DeparserVisitor {
26172617
}
26182618
break;
26192619
case 'CONSTR_CHECK':
2620-
output.push('CHECK');
2620+
if (this.formatter.isPretty() && !context.isColumnConstraint) {
2621+
output.push('\n' + this.formatter.indent('CHECK'));
2622+
} else {
2623+
output.push('CHECK');
2624+
}
26212625
if (node.raw_expr) {
26222626
if (this.formatter.isPretty()) {
26232627
const checkExpr = this.visit(node.raw_expr, context);
@@ -2700,7 +2704,11 @@ export class Deparser implements DeparserVisitor {
27002704
}
27012705
break;
27022706
case 'CONSTR_UNIQUE':
2703-
output.push('UNIQUE');
2707+
if (this.formatter.isPretty() && !context.isColumnConstraint) {
2708+
output.push('\n' + this.formatter.indent('UNIQUE'));
2709+
} else {
2710+
output.push('UNIQUE');
2711+
}
27042712
if (node.nulls_not_distinct) {
27052713
output.push('NULLS NOT DISTINCT');
27062714
}
@@ -2719,7 +2727,7 @@ export class Deparser implements DeparserVisitor {
27192727
// Only add "FOREIGN KEY" for table-level constraints, not column-level constraints
27202728
if (!context.isColumnConstraint) {
27212729
if (this.formatter.isPretty()) {
2722-
output.push('FOREIGN KEY');
2730+
output.push('\n' + this.formatter.indent('FOREIGN KEY'));
27232731
if (node.fk_attrs && node.fk_attrs.length > 0) {
27242732
const fkAttrs = ListUtils.unwrapList(node.fk_attrs)
27252733
.map(attr => this.visit(attr, context))

0 commit comments

Comments
 (0)