Skip to content

Commit

Permalink
Fix sql2pgroll conversion of string DEFAULT expressions (#532)
Browse files Browse the repository at this point in the history
Column `DEFAULT` expressions for strings and bitstrings need to quoted,
otherwise they are interpreted as identifiers.

Update the test expectations and the conversion code to quote the
values.
  • Loading branch information
andrew-farries authored Dec 13, 2024
1 parent cde22df commit 3c1a5fa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,15 @@ func convertAlterTableSetColumnDefault(stmt *pgq.AlterTableStmt, cmd *pgq.AlterT
// We have a constant
switch v := c.GetVal().(type) {
case *pgq.A_Const_Sval:
operation.Default = nullable.NewNullableWithValue(v.Sval.GetSval())
operation.Default = nullable.NewNullableWithValue(fmt.Sprintf("'%s'", v.Sval.GetSval()))
case *pgq.A_Const_Ival:
operation.Default = nullable.NewNullableWithValue(strconv.FormatInt(int64(v.Ival.Ival), 10))
case *pgq.A_Const_Fval:
operation.Default = nullable.NewNullableWithValue(v.Fval.Fval)
case *pgq.A_Const_Boolval:
operation.Default = nullable.NewNullableWithValue(strconv.FormatBool(v.Boolval.Boolval))
case *pgq.A_Const_Bsval:
operation.Default = nullable.NewNullableWithValue(v.Bsval.Bsval)
operation.Default = nullable.NewNullableWithValue(fmt.Sprintf("'%s'", v.Bsval.GetBsval()))
default:
return nil, fmt.Errorf("unknown constant type: %T", c.GetVal())
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql2pgroll/expect/alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var AlterColumnOp4 = &migrations.OpAlterColumn{
var AlterColumnOp5 = &migrations.OpAlterColumn{
Table: "foo",
Column: "bar",
Default: nullable.NewNullableWithValue("baz"),
Default: nullable.NewNullableWithValue("'baz'"),
Up: sql2pgroll.PlaceHolderSQL,
Down: sql2pgroll.PlaceHolderSQL,
}
Expand Down Expand Up @@ -82,7 +82,7 @@ var AlterColumnOp9 = &migrations.OpAlterColumn{
var AlterColumnOp10 = &migrations.OpAlterColumn{
Table: "foo",
Column: "bar",
Default: nullable.NewNullableWithValue("b0101"),
Default: nullable.NewNullableWithValue("'b0101'"),
Up: sql2pgroll.PlaceHolderSQL,
Down: sql2pgroll.PlaceHolderSQL,
}
Expand Down

0 comments on commit 3c1a5fa

Please sign in to comment.