Skip to content

fix(pretty_print): indent the condition of a check constraint - #871

Merged
psteinroe merged 1 commit into
mainfrom
fix/format-check-constraint-body
Oct 19, 2026
Merged

psteinroe merged 1 commit into
mainfrom
fix/format-check-constraint-body

Conversation

@psteinroe

@psteinroe psteinroe commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

CHECK conditions printed check ( + condition + ) with no indent and no break after (, so a broken condition continued at the constraint line's indent. This happened with the default fit layout once a condition was too long. With layout: expanded it happened for every and/or, even in a short check (a or b).

CHECK conditions now use the parenthesized-condition layout that policies got in #857. A condition stays on one line when it fits. Otherwise the line breaks after (, the condition is indented one level, and ) goes back to the constraint's indent, followed by no inherit / not valid. This applies everywhere a CHECK is printed: table and column constraints in CREATE TABLE, ALTER TABLE ... ADD, CREATE DOMAIN and ALTER DOMAIN ... ADD CONSTRAINT.

Expanded layout

A constraint is not a clause, so its condition follows the line width even with layout: expanded, like select-list-only subqueries in #857. Once a constraint breaks, expanded layout puts each operand of the top-level and/or chain on its own line. The chain is emitted in the parentheses' group rather than a group of its own, so both break together. Nested groups such as (a and b) still stay on one line when they fit.

Before (layout: expanded, logicalOperatorPlacement: leading, lineWidth: 120):

  constraint t_at_least_one_permission check (can_view
  or can_edit),
  constraint accounts_contact_present check (email is not null
  or phone is not null
  or deleted_at is not null
  or kind = 'internal'),

After:

  constraint t_at_least_one_permission check (can_view or can_edit),
  constraint accounts_contact_present check (
    email is not null
    or phone is not null
    or deleted_at is not null
    or kind = 'internal'
  ),

Changed snapshots

New check_constraint_body fixtures cover the issue's statements plus a long table CHECK, a column-level CHECK, a CHECK with no inherit, ALTER TABLE ... not valid, and CREATE/ALTER DOMAIN, under the default, expanded, and expanded + compact + leading (width 120) settings. All existing snapshot changes are CHECK conditions that previously didn't fit:

  • Boolean chains (merge, create_stmt_cast_check_0): operands now sit indented inside check ( … ) instead of at the constraint's indent.
  • between and not (...) conditions (alter_table, constraints): break after (, and ) not valid follows the closing parenthesis.
  • Domain CHECKs with a long regex (email_address_type): the comparison is indented inside the parentheses.
  • JSON query functions in CHECKs (sqljson_queryfuncs): the function call is indented inside the parentheses. At width 100 some now fit on one line. The JSON functions' own argument layout and their double space before returning are unchanged; both already show up elsewhere on main.

Fixes #866

A CHECK condition now uses the parenthesized-condition layout of a policy's
using (...): it stays on one line when it fits, and otherwise breaks after
the opening parenthesis, indents the condition and puts the closing
parenthesis back at the constraint's indent.

A constraint is not a clause, so its condition follows the line width in
expanded layout too. Once it breaks, expanded layout puts each operand of
its and/or chain on its own line.
@psteinroe
psteinroe merged commit d338a23 into main Oct 19, 2026
9 checks passed
@psteinroe
psteinroe deleted the fix/format-check-constraint-body branch October 19, 2026 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter (expanded layout): boolean expression in CHECK constraint breaks without indentation

1 participant