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
11 changes: 9 additions & 2 deletions internal/plan/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,16 @@ func generateRewrite(d diff.Diff, newlyCreatedTables map[string]bool, newlyCreat
case diff.DiffTypeTableColumn:
if d.Operation == diff.DiffOperationAlter {
if columnDiff, ok := d.Source.(*diff.ColumnDiff); ok {
// Check if this is a NOT NULL addition
// Check if this is a NOT NULL addition AND this specific statement is for SET NOT NULL
// Multiple statements can be generated from the same ColumnDiff (e.g., SET NOT NULL + SET DEFAULT),
// so we must only rewrite the statement that actually contains SET NOT NULL
if columnDiff.Old.IsNullable && !columnDiff.New.IsNullable {
return generateColumnNotNullRewrite(columnDiff, d.Path)
// Verify this diff's SQL actually contains SET NOT NULL
for _, stmt := range d.Statements {
if strings.Contains(stmt.SQL, "SET NOT NULL") {
return generateColumnNotNullRewrite(columnDiff, d.Path)
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions testdata/diff/create_table/add_default_not_null/diff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE people ALTER COLUMN created_at SET NOT NULL;
ALTER TABLE people ALTER COLUMN created_at SET DEFAULT now();
4 changes: 4 additions & 0 deletions testdata/diff/create_table/add_default_not_null/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE public.people (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
created_at timestamp with time zone DEFAULT NOW() NOT NULL
);
4 changes: 4 additions & 0 deletions testdata/diff/create_table/add_default_not_null/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE public.people (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
created_at timestamp with time zone
);
44 changes: 44 additions & 0 deletions testdata/diff/create_table/add_default_not_null/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"version": "1.0.0",
"pgschema_version": "1.6.2",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "a5614a0f5b8d53ba131811b4a89cd3eddb434b6bd0398b7ec180199c8fba880d"
},
"groups": [
{
"steps": [
{
"sql": "ALTER TABLE people ADD CONSTRAINT created_at_not_null CHECK (created_at IS NOT NULL) NOT VALID;",
"type": "table.column",
"operation": "alter",
"path": "public.people.created_at"
},
{
"sql": "ALTER TABLE people VALIDATE CONSTRAINT created_at_not_null;",
"type": "table.column",
"operation": "alter",
"path": "public.people.created_at"
},
{
"sql": "ALTER TABLE people ALTER COLUMN created_at SET NOT NULL;",
"type": "table.column",
"operation": "alter",
"path": "public.people.created_at"
},
{
"sql": "ALTER TABLE people DROP CONSTRAINT created_at_not_null;",
"type": "table.column",
"operation": "alter",
"path": "public.people.created_at"
},
{
"sql": "ALTER TABLE people ALTER COLUMN created_at SET DEFAULT now();",
"type": "table.column",
"operation": "alter",
"path": "public.people.created_at"
}
]
}
]
}
9 changes: 9 additions & 0 deletions testdata/diff/create_table/add_default_not_null/plan.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ALTER TABLE people ADD CONSTRAINT created_at_not_null CHECK (created_at IS NOT NULL) NOT VALID;

ALTER TABLE people VALIDATE CONSTRAINT created_at_not_null;

ALTER TABLE people ALTER COLUMN created_at SET NOT NULL;

ALTER TABLE people DROP CONSTRAINT created_at_not_null;

ALTER TABLE people ALTER COLUMN created_at SET DEFAULT now();
21 changes: 21 additions & 0 deletions testdata/diff/create_table/add_default_not_null/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ people
~ created_at (column)

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

ALTER TABLE people ADD CONSTRAINT created_at_not_null CHECK (created_at IS NOT NULL) NOT VALID;

ALTER TABLE people VALIDATE CONSTRAINT created_at_not_null;

ALTER TABLE people ALTER COLUMN created_at SET NOT NULL;

ALTER TABLE people DROP CONSTRAINT created_at_not_null;

ALTER TABLE people ALTER COLUMN created_at SET DEFAULT now();
Loading