Skip to content

Commit

Permalink
sql2pgroll: Support set and drop column defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade committed Dec 11, 2024
1 parent 11b6454 commit da2efe4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sql2pgroll
import (
"fmt"

"github.com/oapi-codegen/nullable"
pgq "github.com/pganalyze/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
Expand Down Expand Up @@ -38,6 +39,8 @@ func convertAlterTableStmt(stmt *pgq.AlterTableStmt) (migrations.Operations, err
op, err = convertAlterTableAddConstraint(stmt, alterTableCmd)
case pgq.AlterTableType_AT_DropColumn:
op, err = convertAlterTableDropColumn(stmt, alterTableCmd)
case pgq.AlterTableType_AT_ColumnDefault:
op, err = convertAlterTableSetColumnDefault(stmt, alterTableCmd)
}

if err != nil {
Expand Down Expand Up @@ -158,11 +161,27 @@ func convertAlterTableAddUniqueConstraint(stmt *pgq.AlterTableStmt, constraint *
}, nil
}

// convertAlterTableDropColumn converts SQL statements like:
// convertAlterTableSetColumnDefault converts SQL statements like:
//
// `ALTER TABLE foo DROP COLUMN bar
// `ALTER TABLE foo COLUMN bar SET DEFAULT 'foo'
// `ALTER TABLE foo COLUMN bar SET DEFAULT null
// `ALTER TABLE foo COLUMN bar DROP DEFAULT
//
// to an OpDropColumn operation.
func convertAlterTableSetColumnDefault(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd) (migrations.Operation, error) {
def := nullable.NewNullNullable[string]()
if val := cmd.GetDef().GetAConst().GetSval(); val != nil {
def.Set(val.Sval)
}
return &migrations.OpAlterColumn{
Table: stmt.GetRelation().GetRelname(),
Column: cmd.GetName(),
Default: def,
Down: PlaceHolderSQL,
Up: PlaceHolderSQL,
}, nil
}

func convertAlterTableDropColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd) (migrations.Operation, error) {
if !canConvertDropColumn(cmd) {
return nil, nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ func TestConvertAlterTableStatements(t *testing.T) {
sql: "ALTER TABLE foo ALTER COLUMN a TYPE text",
expectedOp: expect.AlterColumnOp3,
},
{
sql: "ALTER TABLE foo ALTER COLUMN bar SET DEFAULT 'baz'",
expectedOp: expect.AlterColumnOp5,
},
{
sql: "ALTER TABLE foo ALTER COLUMN bar DROP DEFAULT",
expectedOp: expect.AlterColumnOp6,
},
{
sql: "ALTER TABLE foo ALTER COLUMN bar SET DEFAULT null",
expectedOp: expect.AlterColumnOp6,
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a)",
expectedOp: expect.CreateConstraintOp1,
Expand Down
18 changes: 18 additions & 0 deletions pkg/sql2pgroll/expect/alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package expect

import (
"github.com/oapi-codegen/nullable"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
)
Expand Down Expand Up @@ -37,6 +39,22 @@ var AlterColumnOp4 = &migrations.OpAlterColumn{
Name: ptr("b"),
}

var AlterColumnOp5 = &migrations.OpAlterColumn{
Table: "foo",
Column: "bar",
Default: nullable.NewNullableWithValue("baz"),
Up: sql2pgroll.PlaceHolderSQL,
Down: sql2pgroll.PlaceHolderSQL,
}

var AlterColumnOp6 = &migrations.OpAlterColumn{
Table: "foo",
Column: "bar",
Default: nullable.NewNullNullable[string](),
Up: sql2pgroll.PlaceHolderSQL,
Down: sql2pgroll.PlaceHolderSQL,
}

func ptr[T any](v T) *T {
return &v
}

0 comments on commit da2efe4

Please sign in to comment.