diff --git a/pkg/sql2pgroll/create_table.go b/pkg/sql2pgroll/create_table.go index 42be400c..6994ebbd 100644 --- a/pkg/sql2pgroll/create_table.go +++ b/pkg/sql2pgroll/create_table.go @@ -137,11 +137,14 @@ func convertColumnDef(tableName string, col *pgq.ColumnDef) (*migrations.Column, if isConstraintNamed(c.GetConstraint()) { return nil, nil } - d, err := pgq.DeparseExpr(c.GetConstraint().GetRawExpr()) + d, err := extractDefault(c.GetConstraint().GetRawExpr()) if err != nil { return nil, fmt.Errorf("error deparsing default value: %w", err) } - defaultValue = &d + if !d.IsNull() { + v := d.MustGet() + defaultValue = &v + } case pgq.ConstrType_CONSTR_FOREIGN: foreignKey, err = convertInlineForeignKeyConstraint(tableName, col.GetColname(), c.GetConstraint()) if err != nil { diff --git a/pkg/sql2pgroll/create_table_test.go b/pkg/sql2pgroll/create_table_test.go index 5538ed8b..d37f41e8 100644 --- a/pkg/sql2pgroll/create_table_test.go +++ b/pkg/sql2pgroll/create_table_test.go @@ -72,6 +72,10 @@ func TestConvertCreateTableStatements(t *testing.T) { sql: "CREATE TABLE foo(a timestamptz DEFAULT now())", expectedOp: expect.CreateTableOp11, }, + { + sql: "CREATE TABLE foo(a int DEFAULT NULL)", + expectedOp: expect.CreateTableOp20, + }, { sql: "CREATE TABLE foo(a int CONSTRAINT my_fk REFERENCES bar(b))", expectedOp: expect.CreateTableOp19, diff --git a/pkg/sql2pgroll/expect/create_table.go b/pkg/sql2pgroll/expect/create_table.go index b46adaa5..7243b608 100644 --- a/pkg/sql2pgroll/expect/create_table.go +++ b/pkg/sql2pgroll/expect/create_table.go @@ -257,3 +257,14 @@ var CreateTableOp19 = &migrations.OpCreateTable{ }, }, } + +var CreateTableOp20 = &migrations.OpCreateTable{ + Name: "foo", + Columns: []migrations.Column{ + { + Name: "a", + Type: "timestamptz", + Nullable: true, + }, + }, +}