Skip to content
Open
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
2 changes: 1 addition & 1 deletion translators/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
18 changes: 18 additions & 0 deletions translators/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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';`
Expand Down