Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change CREATE TABLE DEFAULT constraint handling #561

Merged
Merged
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
7 changes: 5 additions & 2 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql2pgroll/expect/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,14 @@ var CreateTableOp19 = &migrations.OpCreateTable{
},
},
}

var CreateTableOp20 = &migrations.OpCreateTable{
Name: "foo",
Columns: []migrations.Column{
{
Name: "a",
Type: "int",
Nullable: true,
},
},
}