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
4 changes: 2 additions & 2 deletions internal/diff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,8 @@ func buildColumnClauses(column *ir.Column, isPartOfAnyPK bool, tableSchema strin
parts = append(parts, fmt.Sprintf("GENERATED ALWAYS AS (%s) STORED", *column.GeneratedExpr))
}

// 4. NOT NULL (skip for PK including multi-column PKs, identity, SERIAL, or generated columns)
if !column.IsNullable && column.Identity == nil && !isSerialColumn(column) && !isPartOfAnyPK && !column.IsGenerated {
// 4. NOT NULL (skip for PK including multi-column PKs, identity, and SERIAL)
if !column.IsNullable && column.Identity == nil && !isSerialColumn(column) && !isPartOfAnyPK {
parts = append(parts, "NOT NULL")
}

Expand Down
4 changes: 3 additions & 1 deletion testdata/diff/create_table/add_column_generated/diff.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ALTER TABLE merge_request
ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY;

ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED;
ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED;

ALTER TABLE merge_request ADD COLUMN cleaned_title varchar(255) GENERATED ALWAYS AS (lower(data ->> 'title')) STORED NOT NULL;
1 change: 1 addition & 0 deletions testdata/diff/create_table/add_column_generated/new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ CREATE TABLE public.merge_request (
data jsonb NOT NULL,
iid integer GENERATED ALWAYS AS ((data ->> 'iid')::integer) STORED,
title text GENERATED ALWAYS AS (data ->> 'title') STORED,
cleaned_title varchar(255) GENERATED ALWAYS AS (lower(data ->> 'title')) STORED NOT NULL,
CONSTRAINT pk_merge_request_iid PRIMARY KEY (iid)
);
6 changes: 6 additions & 0 deletions testdata/diff/create_table/add_column_generated/plan.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"type": "table.column",
"operation": "create",
"path": "public.merge_request.title"
},
{
"sql": "ALTER TABLE merge_request ADD COLUMN cleaned_title varchar(255) GENERATED ALWAYS AS (lower(data ->> 'title')) STORED NOT NULL;",
"type": "table.column",
"operation": "create",
"path": "public.merge_request.cleaned_title"
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions testdata/diff/create_table/add_column_generated/plan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ ALTER TABLE merge_request
ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY;

ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED;

ALTER TABLE merge_request ADD COLUMN cleaned_title varchar(255) GENERATED ALWAYS AS (lower(data ->> 'title')) STORED NOT NULL;
3 changes: 3 additions & 0 deletions testdata/diff/create_table/add_column_generated/plan.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Summary by type:

Tables:
~ merge_request
+ cleaned_title (column)
+ iid (column)
+ title (column)

Expand All @@ -15,3 +16,5 @@ ALTER TABLE merge_request
ADD COLUMN iid integer GENERATED ALWAYS AS (CAST(data ->> 'iid' AS int)) STORED CONSTRAINT pk_merge_request_iid PRIMARY KEY;

ALTER TABLE merge_request ADD COLUMN title text GENERATED ALWAYS AS (data ->> 'title') STORED;

ALTER TABLE merge_request ADD COLUMN cleaned_title varchar(255) GENERATED ALWAYS AS (lower(data ->> 'title')) STORED NOT NULL;