Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions internal/ir/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,9 +849,15 @@ func normalizeCheckClause(checkClause string) string {
clause = after
}

// Remove outer parentheses if present (may be multiple layers)
for strings.HasPrefix(clause, "(") && strings.HasSuffix(clause, ")") {
clause = strings.TrimSpace(clause[1 : len(clause)-1])
// Remove ONLY outer parentheses if they are balanced and not part of complex expression
clause = strings.TrimSpace(clause)
for len(clause) > 0 && clause[0] == '(' && clause[len(clause)-1] == ')' {
// Check if parentheses are balanced
if isBalancedParentheses(clause[1 : len(clause)-1]) {
clause = strings.TrimSpace(clause[1 : len(clause)-1])
} else {
break
}
}

// First apply legacy conversions to handle PostgreSQL-specific patterns
Expand Down
2 changes: 2 additions & 0 deletions testdata/diff/create_table/add_check/diff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE code
ADD CONSTRAINT code_check CHECK (code > 0 AND code < 255);
3 changes: 3 additions & 0 deletions testdata/diff/create_table/add_check/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE public.code (
code integer PRIMARY KEY CONSTRAINT code_check CHECK (code > 0 AND code < 255)
);
3 changes: 3 additions & 0 deletions testdata/diff/create_table/add_check/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE IF NOT EXISTS code (
code integer PRIMARY KEY
);
26 changes: 26 additions & 0 deletions testdata/diff/create_table/add_check/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "1.0.0",
"pgschema_version": "1.1.0",
"created_at": "2025-09-23T18:49:35+03:00",
"source_fingerprint": {
"hash": "a9d472cd406e27485b570b72906ad224b8282469bd50699bddd6da04acdabaef"
},
"groups": [
{
"steps": [
{
"sql": "ALTER TABLE code\nADD CONSTRAINT code_check CHECK (code > 0 AND code < 255) NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.code.code_check"
},
{
"sql": "ALTER TABLE code VALIDATE CONSTRAINT code_check;",
"type": "table.constraint",
"operation": "create",
"path": "public.code.code_check"
}
]
}
]
}
4 changes: 4 additions & 0 deletions testdata/diff/create_table/add_check/plan.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE code
ADD CONSTRAINT code_check CHECK (code > 0 AND code < 255) NOT VALID;

ALTER TABLE code VALIDATE CONSTRAINT code_check;
16 changes: 16 additions & 0 deletions testdata/diff/create_table/add_check/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ code
+ code_check (constraint)

DDL to be executed:
--------------------------------------------------

ALTER TABLE code
ADD CONSTRAINT code_check CHECK (code > 0 AND code < 255) NOT VALID;

ALTER TABLE code VALIDATE CONSTRAINT code_check;