Fix dropped parentheses on column-level CHECK constraints#14
Conversation
A column-level CHECK constraint requires its expression to be parenthesized. format_col_constraint_elem emitted the expression unparenthesized (`CHECK a > 0`), producing invalid SQL. Re-formatting that output mis-parsed and corrupted the table (gmr/pgfmt#11). - Wrap the CHECK expression in parentheses, gated on has_check so DEFAULT and other column expressions are untouched. - Add is_wrapped_in_parens so an already-parenthesized expression (e.g. pg_dump's `CHECK ((x))`) collapses to a single pair instead of doubling up; this keeps the create_domain fixture stable. - Stop padding the type column of a trailing constraint-less column, which previously left trailing whitespace on the line. Extract the shared render_aligned_column helper used by the CREATE TABLE and CREATE FOREIGN TABLE river renderers. - Add a regression test built from the issue's example covering paren preservation, idempotency, and no trailing whitespace. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR preserves column-level CHECK parentheses during repeated formatting, adds shared river-style column alignment, and extends regression coverage for CREATE TABLE and CREATE FOREIGN TABLE output. ChangesCHECK constraint parentheses and column alignment fix
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/test_parens.rs (1)
29-61: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider adding a CREATE FOREIGN TABLE case.
Per the cohort description,
render_aligned_columnis also applied to CREATE FOREIGN TABLE rendering, but this regression test only exercises CREATE TABLE. A parallel case would close the gap on that code path.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/test_parens.rs` around lines 29 - 61, Add a regression case for CREATE FOREIGN TABLE alongside preserve_col_check_parens_and_idempotent, since render_aligned_column is used there too and the current test only covers CREATE TABLE. Extend the test in tests/test_parens.rs (or add a sibling test) to format a CREATE FOREIGN TABLE statement with a column-level CHECK and verify the parentheses are preserved, reformatting stays idempotent, and no trailing whitespace is introduced, using format(...) with Style::Aweber as in the existing test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/test_parens.rs`:
- Around line 29-61: Add a regression case for CREATE FOREIGN TABLE alongside
preserve_col_check_parens_and_idempotent, since render_aligned_column is used
there too and the current test only covers CREATE TABLE. Extend the test in
tests/test_parens.rs (or add a sibling test) to format a CREATE FOREIGN TABLE
statement with a column-level CHECK and verify the parentheses are preserved,
reformatting stays idempotent, and no trailing whitespace is introduced, using
format(...) with Style::Aweber as in the existing test.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 43c6d664-6d90-4f6f-940b-930d35f734f3
📒 Files selected for processing (2)
src/formatter/stmt.rstests/test_parens.rs
render_aligned_column also drives CREATE FOREIGN TABLE rendering, but the issue #11 regression test only exercised CREATE TABLE. Adds a parallel case asserting column-level CHECK parentheses are preserved, reformatting is idempotent, and no trailing whitespace is emitted. Addresses a CodeRabbit review nitpick on PR #14. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
🤖 This comment was posted by Claude on behalf of @gmr Thanks @coderabbitai — good catch. Addressed the nitpick in 042e7cc by adding |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
cd /tmp && rm -rf repo && git clone --quiet https://github.com/gmr/libpgfmt.git repo && cd repo && git log --oneline -5 && git show 042e7cc --statLength of output: 1301 Verified — commit 042e7cc adds
(っ◕‿◕)っ |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/test_parens.rs (1)
62-91: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueLGTM overall — solid regression coverage for the foreign-table path.
Correctly mirrors the CREATE TABLE test, verifying paren preservation, idempotence, and no trailing whitespace against
render_aligned_column/is_wrapped_in_parens.One minor nit: this test and
preserve_col_check_parens_and_idempotent(lines 29-61) duplicate the same three-part assertion pattern (parens check → idempotence → trailing-whitespace loop). Could extract a small helper (e.g.assert_idempotent_and_clean(sql, expected_substr)) to cut duplication, but not blocking.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/test_parens.rs` around lines 62 - 91, The new foreign-table regression test duplicates the same assertion flow already used in preserve_col_check_parens_and_idempotent, so factor the repeated parens/idempotence/trailing-whitespace checks into a small shared helper in tests/test_parens.rs. Update preserve_foreign_col_check_parens_and_idempotent to call that helper with the SQL string and expected CHECK substring, keeping the behavior identical while reducing duplication.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/test_parens.rs`:
- Around line 62-91: The new foreign-table regression test duplicates the same
assertion flow already used in preserve_col_check_parens_and_idempotent, so
factor the repeated parens/idempotence/trailing-whitespace checks into a small
shared helper in tests/test_parens.rs. Update
preserve_foreign_col_check_parens_and_idempotent to call that helper with the
SQL string and expected CHECK substring, keeping the behavior identical while
reducing duplication.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 88b654e6-d4c1-49de-a6bb-d60a9673552f
📒 Files selected for processing (1)
tests/test_parens.rs
Factors the repeated parens/idempotence/trailing-whitespace assertion flow out of the CREATE TABLE and CREATE FOREIGN TABLE regression tests into assert_idempotent_and_clean, keeping behavior identical. Addresses a CodeRabbit review nitpick on PR #14. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
🤖 This comment was posted by Claude on behalf of @gmr Thanks @coderabbitai — addressed the duplication nitpick in b0e4875 by extracting the shared |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
cd /tmp && rm -rf repo && git clone --quiet https://github.com/gmr/libpgfmt.git repo && cd repo && git show b0e4875 --stat && echo "---" && git show b0e4875 -- tests/test_parens.rs | head -150Length of output: 5096
(っ◕‿◕)っ |
Summary
Fixes gmr/pgfmt#11. A column-level
CHECKconstraint requires its expression to be parenthesized.format_col_constraint_elememitted it unparenthesized, soCHECK (a > 0)formatted toCHECK a > 0— invalid PostgreSQL. Re-running the formatter on that output mis-parsed the broken SQL and cascaded into a corrupted table (columns split apart).Problem
Solution
has_checksoDEFAULTand other column expressions are left alone.is_wrapped_in_parensso an already-parenthesized expression (e.g. pg_dump's redundantCHECK ((x))) collapses to a single pair rather than doubling up. This keeps thecreate_domainfixture stable.render_aligned_columnhelper used by both theCREATE TABLEandCREATE FOREIGN TABLEriver renderers.Testing
cargo test— full suite passes (including the new regression test and all fixtures).cargo clippy --all-targets -- -D warnings— clean.🤖 Generated with Claude Code
Summary by CodeRabbit
CREATE TABLEandCREATE FOREIGN TABLEcolumn definitions, with consistent alignment of column names and types.CHECKformatting to preserve required parentheses and avoid incorrect wrapping.CHECKparentheses are preserved and formatting is idempotent across repeated runs, covering both table and foreign table cases.