diff --git a/translators/postgres.go b/translators/postgres.go index 60d5152..3d13ae1 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 afd44df..110e2d0 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';`