Skip to content

Commit

Permalink
Fix ALTER TABLE ADD COLUMN default nullability (#558)
Browse files Browse the repository at this point in the history
Columns added by `ALTER TABLE ADD COLUMN` are nullable by default.

For the `pgroll` `Column` type, these means we need to set `Nullable` to
`true` by default when converting such SQL statements with `sql2pgroll`.
  • Loading branch information
andrew-farries authored Jan 2, 2025
1 parent e04850e commit 783d282
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
8 changes: 6 additions & 2 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ func convertAlterTableAddColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd

operation := &migrations.OpAddColumn{
Column: migrations.Column{
Name: columnDef.GetColname(),
Type: columnType,
Name: columnDef.GetColname(),
Type: columnType,
Nullable: true,
},
Table: getQualifiedRelationName(stmt.GetRelation()),
Up: PlaceHolderSQL,
Expand All @@ -430,8 +431,11 @@ func convertAlterTableAddColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd
switch constraint.GetConstraint().GetContype() {
case pgq.ConstrType_CONSTR_NULL:
operation.Column.Nullable = true
case pgq.ConstrType_CONSTR_NOTNULL:
operation.Column.Nullable = false
case pgq.ConstrType_CONSTR_PRIMARY:
operation.Column.Pk = true
operation.Column.Nullable = false
case pgq.ConstrType_CONSTR_UNIQUE:
operation.Column.Unique = true
case pgq.ConstrType_CONSTR_CHECK:
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestConvertAlterTableStatements(t *testing.T) {
},
{
sql: "ALTER TABLE foo ADD COLUMN bar int NOT NULL",
expectedOp: expect.AddColumnOp1,
expectedOp: expect.AddColumnOp8,
},
{
sql: "ALTER TABLE schema.foo ADD COLUMN bar int",
Expand Down
49 changes: 33 additions & 16 deletions pkg/sql2pgroll/expect/add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ var AddColumnOp1 = &migrations.OpAddColumn{
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Name: "bar",
Type: "int",
Nullable: true,
},
}

var AddColumnOp2 = &migrations.OpAddColumn{
Table: "schema.foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Name: "bar",
Type: "int",
Nullable: true,
},
}

Expand All @@ -30,9 +32,10 @@ func AddColumnOp1WithDefault(def *string) *migrations.OpAddColumn {
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Default: def,
Name: "bar",
Type: "int",
Default: def,
Nullable: true,
},
}
}
Expand All @@ -51,9 +54,10 @@ var AddColumnOp4 = &migrations.OpAddColumn{
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Unique: true,
Name: "bar",
Type: "int",
Unique: true,
Nullable: true,
},
}

Expand All @@ -71,8 +75,9 @@ var AddColumnOp6 = &migrations.OpAddColumn{
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Name: "bar",
Type: "int",
Nullable: true,
Check: &migrations.CheckConstraint{
Constraint: "bar > 0",
Name: "",
Expand All @@ -84,22 +89,34 @@ var AddColumnOp7 = &migrations.OpAddColumn{
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Name: "bar",
Type: "int",
Nullable: true,
Check: &migrations.CheckConstraint{
Constraint: "bar > 0",
Name: "check_bar",
},
},
}

var AddColumnOp8 = &migrations.OpAddColumn{
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Nullable: false,
},
}

func AddColumnOp8WithOnDeleteAction(action migrations.ForeignKeyReferenceOnDelete) *migrations.OpAddColumn {
return &migrations.OpAddColumn{
Table: "foo",
Up: sql2pgroll.PlaceHolderSQL,
Column: migrations.Column{
Name: "bar",
Type: "int",
Name: "bar",
Type: "int",
Nullable: true,
References: &migrations.ForeignKeyReference{
Column: "bar",
Name: "fk_baz",
Expand Down

0 comments on commit 783d282

Please sign in to comment.