Skip to content

Commit 9c8e8d8

Browse files
fix: improve CHECK constraint and nested SELECT indentation
- Spawn context with increased indentLevel for CHECK constraint expressions - Spawn context with increased indentLevel for nested SELECT statements - Update snapshots to reflect proper indentation of logical scopes - Ensure tax_rate expressions in CHECK constraints are properly indented - Ensure nested SELECT FROM clauses are properly indented within subqueries Co-Authored-By: Dan Lynch <[email protected]>
1 parent c2f6de8 commit 9c8e8d8

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ exports[`pretty: pretty/constraints-9.sql 1`] = `
8787
ADD CONSTRAINT tax_rate_range
8888
CHECK (
8989
tax_rate >= 0
90-
AND tax_rate <= 1
90+
AND tax_rate <= 1
9191
)"
9292
`;
9393

@@ -151,7 +151,7 @@ exports[`pretty: pretty/constraints-16.sql 1`] = `
151151
ADD CONSTRAINT chk_enrollment_date
152152
CHECK (
153153
enrollment_date <= CURRENT_DATE
154-
AND status IN ('active', 'completed', 'withdrawn')
154+
AND status IN ('active', 'completed', 'withdrawn')
155155
)"
156156
`;
157157

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ CASE
9595
WHEN EXISTS (SELECT 1
9696
FROM reviews AS r
9797
WHERE
98-
r.product_id = p.id
99-
AND r.rating >= 4) THEN 'Popular'
98+
r.product_id = p.id
99+
AND r.rating >= 4) THEN 'Popular'
100100
ELSE 'Unrated'
101101
END AS status
102102
FROM products AS p
@@ -165,8 +165,8 @@ WHERE
165165
AND EXISTS (SELECT 1
166166
FROM order_notes AS n
167167
WHERE
168-
(n.order_id = o.id
169-
AND n.note ILIKE '%expedite%'))))
168+
(n.order_id = o.id
169+
AND n.note ILIKE '%expedite%'))))
170170
ORDER BY
171171
o.created_at DESC"
172172
`;
@@ -229,8 +229,8 @@ CASE
229229
WHEN EXISTS (SELECT 1
230230
FROM logins
231231
WHERE
232-
logins.user_id = users.user_id
233-
AND success = false) THEN 'risky'
232+
logins.user_id = users.user_id
233+
AND success = false) THEN 'risky'
234234
ELSE 'safe'
235235
END AS risk_status
236236
FROM users"
@@ -261,12 +261,12 @@ exports[`pretty: pretty/misc-12.sql 1`] = `
261261
id,
262262
(SELECT
263263
CASE
264-
WHEN count(*) > 5 THEN 'frequent'
265-
ELSE 'occasional'
264+
WHEN count(*) > 5 THEN 'frequent'
265+
ELSE 'occasional'
266266
END
267267
FROM purchases AS p
268268
WHERE
269-
p.user_id = u.id) AS purchase_freq
269+
p.user_id = u.id) AS purchase_freq
270270
FROM users AS u"
271271
`;
272272

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ WHERE
117117
id IN (SELECT user_id
118118
FROM orders
119119
WHERE
120-
total > 100)"
120+
total > 100)"
121121
`;
122122

123123
exports[`pretty: pretty/selects-14.sql 1`] = `

packages/deparser/src/deparser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,7 @@ export class Deparser implements DeparserVisitor {
26592659
}
26602660
if (node.raw_expr) {
26612661
if (context.isPretty()) {
2662-
const checkExpr = this.visit(node.raw_expr, context);
2662+
const checkExpr = this.visit(node.raw_expr, context.spawn('Constraint', { indentLevel: context.indentLevel + 1 }));
26632663
if (checkExpr.includes('\n')) {
26642664
output.push('(\n' + context.indent(checkExpr) + '\n)');
26652665
} else {
@@ -2973,7 +2973,7 @@ export class Deparser implements DeparserVisitor {
29732973
}
29742974

29752975
SubLink(node: t.SubLink, context: DeparserContext): string {
2976-
const subselect = context.parens(this.visit(node.subselect, context));
2976+
const subselect = context.parens(this.visit(node.subselect, context.spawn('SubLink', { indentLevel: context.indentLevel + 1 })));
29772977

29782978
switch (node.subLinkType) {
29792979
case 'ANY_SUBLINK':

0 commit comments

Comments
 (0)