From 3c1eb9472c796cc6a708a40bd799f7f08dc04d6b Mon Sep 17 00:00:00 2001 From: Charlie Tonneslan Date: Mon, 18 May 2026 14:21:50 -0400 Subject: [PATCH] translators/postgres: respect null: false in change_column change_column with {null: false} emitted DROP NOT NULL instead of SET NOT NULL because the postgres builder only checked whether the "null" key was present, not its value. Match the behavior the caller asked for: nil or false means NOT NULL, anything else means nullable. Fixes #62 Signed-off-by: Charlie Tonneslan --- translators/postgres.go | 2 +- translators/postgres_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/translators/postgres.go b/translators/postgres.go index 60d5152f..3d13ae14 100644 --- a/translators/postgres.go +++ b/translators/postgres.go @@ -191,7 +191,7 @@ func (p *Postgres) buildChangeColumn(c fizz.Column) string { s := fmt.Sprintf("\"%s\" TYPE %s", c.Name, p.colType(c)) var sets []string - if c.Options["null"] == nil { + if c.Options["null"] == nil || c.Options["null"] == false { sets = append(sets, fmt.Sprintf("ALTER COLUMN \"%s\" SET NOT NULL", c.Name)) } else { sets = append(sets, fmt.Sprintf("ALTER COLUMN \"%s\" DROP NOT NULL", c.Name)) diff --git a/translators/postgres_test.go b/translators/postgres_test.go index afd44dff..110e2d04 100644 --- a/translators/postgres_test.go +++ b/translators/postgres_test.go @@ -207,6 +207,24 @@ func (p *PostgreSQLSuite) Test_Postgres_ChangeColumn() { r.Equal(ddl, res) } +func (p *PostgreSQLSuite) Test_Postgres_ChangeColumn_NullFalse() { + r := p.Require() + ddl := `ALTER TABLE "posts" ALTER COLUMN "destination_id" TYPE integer, ALTER COLUMN "destination_id" SET NOT NULL;` + + res, _ := fizz.AString(`change_column("posts", "destination_id", "integer", {"null": false})`, pgt) + + r.Equal(ddl, res) +} + +func (p *PostgreSQLSuite) Test_Postgres_ChangeColumn_NullTrue() { + r := p.Require() + ddl := `ALTER TABLE "posts" ALTER COLUMN "destination_id" TYPE integer, ALTER COLUMN "destination_id" DROP NOT NULL;` + + res, _ := fizz.AString(`change_column("posts", "destination_id", "integer", {"null": true})`, pgt) + + r.Equal(ddl, res) +} + func (p *PostgreSQLSuite) Test_Postgres_AddColumn() { r := p.Require() ddl := `ALTER TABLE "mytable" ADD COLUMN "mycolumn" VARCHAR (50) NOT NULL DEFAULT 'foo';`