Skip to content

Commit 2de4ac1

Browse files
committed
Fixed migration generator adding a null default value to generated columns
1 parent 14ba085 commit 2de4ac1

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

IHP/IDE/CodeGen/MigrationGenerator.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ normalizeColumn table Column { name, columnType, defaultValue, notNull, isUnique
407407

408408
normalizedDefaultValue = case defaultValue of
409409
Just defaultValue -> Just (normalizeExpression defaultValue)
410-
Nothing -> if notNull
410+
Nothing -> if notNull || isJust generator
411411
then Nothing
412412
else Just (VarExpression "null") -- pg_dump columns don't have an explicit default null value
413413

Test/IDE/CodeGeneration/MigrationGenerator.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,34 @@ tests = do
865865

866866
diffSchemas targetSchema actualSchema `shouldBe` migration
867867

868+
it "should run not generate a default value for a generated column" do
869+
let targetSchema = sql [i|
870+
CREATE TABLE products (
871+
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
872+
name TEXT NOT NULL,
873+
description TEXT NOT NULL,
874+
sku TEXT NOT NULL,
875+
text_search TSVECTOR GENERATED ALWAYS AS
876+
( setweight(to_tsvector('english', sku), 'A') ||
877+
setweight(to_tsvector('english', name), 'B') ||
878+
setweight(to_tsvector('english', description), 'C')
879+
) STORED
880+
);
881+
|]
882+
let actualSchema = sql [i|
883+
CREATE TABLE products (
884+
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
885+
name TEXT NOT NULL,
886+
description TEXT NOT NULL,
887+
sku TEXT NOT NULL
888+
);
889+
|]
890+
let migration = sql [i|
891+
ALTER TABLE products ADD COLUMN text_search TSVECTOR GENERATED ALWAYS AS (setweight(to_tsvector('english', sku), 'A') || setweight(to_tsvector('english', name), 'B') || setweight(to_tsvector('english', description), 'C')) STORED;
892+
|]
893+
894+
diffSchemas targetSchema actualSchema `shouldBe` migration
895+
868896

869897
sql :: Text -> [Statement]
870898
sql code = case Megaparsec.runParser Parser.parseDDL "" code of

0 commit comments

Comments
 (0)