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
6 changes: 4 additions & 2 deletions internal/plan/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,19 @@ func generateColumnNotNullRewrite(_ *diff.ColumnDiff, path string) []RewriteStep
tableName := getTableNameWithSchema(schema, table)
constraintName := fmt.Sprintf("%s_not_null", column)

quotedColumn := ir.QuoteIdentifier(column)

// Step 1: Add CHECK constraint with NOT VALID
addConstraintSQL := fmt.Sprintf("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s IS NOT NULL) NOT VALID;",
tableName, ir.QuoteIdentifier(constraintName), column)
tableName, ir.QuoteIdentifier(constraintName), quotedColumn)

// Step 2: Validate the constraint
validateConstraintSQL := fmt.Sprintf("ALTER TABLE %s VALIDATE CONSTRAINT %s;",
tableName, ir.QuoteIdentifier(constraintName))

// Step 3: Set column to NOT NULL
setNotNullSQL := fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;",
tableName, column)
tableName, quotedColumn)

// Step 4: Drop CHECK constraint
dropConstraintSQL := fmt.Sprintf("ALTER TABLE %s DROP CONSTRAINT %s;",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "Planning" ALTER COLUMN "offersValidUntil" SET NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE public."Planning" (
id integer NOT NULL,
"offersValidUntil" timestamp with time zone NOT NULL,
CONSTRAINT "Planning_pkey" PRIMARY KEY (id)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE public."Planning" (
id integer NOT NULL,
"offersValidUntil" timestamp with time zone,
CONSTRAINT "Planning_pkey" PRIMARY KEY (id)
);
38 changes: 38 additions & 0 deletions testdata/diff/online/issue_313_camelcase_column_not_null/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "1.0.0",
"pgschema_version": "1.7.2",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "59ecc7fdab924e513eb6acdc93543167379bf64b4c6b5e1fe1d8e98fa48fdb9f"
},
"groups": [
{
"steps": [
{
"sql": "ALTER TABLE \"Planning\" ADD CONSTRAINT \"offersValidUntil_not_null\" CHECK (\"offersValidUntil\" IS NOT NULL) NOT VALID;",
"type": "table.column",
"operation": "alter",
"path": "public.Planning.offersValidUntil"
},
{
"sql": "ALTER TABLE \"Planning\" VALIDATE CONSTRAINT \"offersValidUntil_not_null\";",
"type": "table.column",
"operation": "alter",
"path": "public.Planning.offersValidUntil"
},
{
"sql": "ALTER TABLE \"Planning\" ALTER COLUMN \"offersValidUntil\" SET NOT NULL;",
"type": "table.column",
"operation": "alter",
"path": "public.Planning.offersValidUntil"
},
{
"sql": "ALTER TABLE \"Planning\" DROP CONSTRAINT \"offersValidUntil_not_null\";",
"type": "table.column",
"operation": "alter",
"path": "public.Planning.offersValidUntil"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE "Planning" ADD CONSTRAINT "offersValidUntil_not_null" CHECK ("offersValidUntil" IS NOT NULL) NOT VALID;

ALTER TABLE "Planning" VALIDATE CONSTRAINT "offersValidUntil_not_null";

ALTER TABLE "Planning" ALTER COLUMN "offersValidUntil" SET NOT NULL;

ALTER TABLE "Planning" DROP CONSTRAINT "offersValidUntil_not_null";
19 changes: 19 additions & 0 deletions testdata/diff/online/issue_313_camelcase_column_not_null/plan.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Plan: 1 to modify.

Summary by type:
tables: 1 to modify

Tables:
~ Planning
~ offersValidUntil (column)

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

ALTER TABLE "Planning" ADD CONSTRAINT "offersValidUntil_not_null" CHECK ("offersValidUntil" IS NOT NULL) NOT VALID;

ALTER TABLE "Planning" VALIDATE CONSTRAINT "offersValidUntil_not_null";

ALTER TABLE "Planning" ALTER COLUMN "offersValidUntil" SET NOT NULL;

ALTER TABLE "Planning" DROP CONSTRAINT "offersValidUntil_not_null";