Skip to content

Commit

Permalink
Merge branch 'main' into rs/drop-multi-column-constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade committed Dec 16, 2024
2 parents 74af490 + 694d935 commit 986c1f1
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
github.com/cloudflare/backoff v0.0.0-20240920015135-e46b80a3a7d0
github.com/lib/pq v1.10.9
github.com/oapi-codegen/nullable v1.1.0
github.com/pganalyze/pg_query_go/v6 v6.0.0
github.com/pterm/pterm v0.12.80
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/testcontainers/testcontainers-go v0.34.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0
github.com/xataio/pg_query_go/v6 v6.0.0-20241216080535-894186571799
golang.org/x/tools v0.28.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQ
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pganalyze/pg_query_go/v6 v6.0.0 h1:in6RkR/apfqlAtvqgDxd4Y4o87a5Pr8fkKDB4DrDo2c=
github.com/pganalyze/pg_query_go/v6 v6.0.0/go.mod h1:nvTHIuoud6e1SfrUaFwHqT0i4b5Nr+1rPWVds3B5+50=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -219,6 +217,8 @@ github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZ
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
github.com/xataio/pg_query_go/v6 v6.0.0-20241216080535-894186571799 h1:FUY7PHaOXMicK0kSh32BdLGwd6svrbNZrnup69jG0DM=
github.com/xataio/pg_query_go/v6 v6.0.0-20241216080535-894186571799/go.mod h1:GK6bpfAhPtZb7wG/IccqvnH+cz3cmvvRTkC+MosESGo=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
Expand Down
30 changes: 8 additions & 22 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ package sql2pgroll

import (
"fmt"
"strconv"

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

"github.com/xataio/pgroll/pkg/migrations"
)
Expand Down Expand Up @@ -274,29 +273,16 @@ func convertAlterTableSetColumnDefault(stmt *pgq.AlterTableStmt, cmd *pgq.AlterT
operation.Default = nullable.NewNullNullable[string]()
return operation, nil
}

// We have a constant
switch v := c.GetVal().(type) {
case *pgq.A_Const_Sval:
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(fmt.Sprintf("'%s'", v.Bsval.GetBsval()))
default:
return nil, fmt.Errorf("unknown constant type: %T", c.GetVal())
}

return operation, nil
}

// We're setting it to an expression
if cmd.GetDef() != nil {
// We're setting it to something other than a constant
return nil, nil
def, err := pgq.DeparseExpr(cmd.GetDef())
if err != nil {
return nil, fmt.Errorf("failed to deparse expression: %w", err)
}
operation.Default = nullable.NewNullableWithValue(def)
return operation, nil
}

// We're not setting it to anything, which is the case when we are dropping it
Expand Down
11 changes: 8 additions & 3 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func TestConvertAlterTableStatements(t *testing.T) {
sql: "ALTER TABLE foo ALTER COLUMN bar SET DEFAULT null",
expectedOp: expect.AlterColumnOp7,
},
{
sql: "ALTER TABLE foo ALTER COLUMN bar SET DEFAULT now()",
expectedOp: expect.AlterColumnOp11,
},
{
sql: "ALTER TABLE foo ALTER COLUMN bar SET DEFAULT (first_name || ' ' || last_name)",
expectedOp: expect.AlterColumnOp12,
},
{
sql: "ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a)",
expectedOp: expect.CreateConstraintOp1,
Expand Down Expand Up @@ -158,9 +166,6 @@ func TestUnconvertableAlterTableStatements(t *testing.T) {
"ALTER TABLE foo DROP COLUMN bar CASCADE",
"ALTER TABLE foo DROP COLUMN IF EXISTS bar",

// Non literal default values
"ALTER TABLE foo ALTER COLUMN bar SET DEFAULT now()",

// Unsupported foreign key statements
"ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE RESTRICT;",
"ALTER TABLE foo ADD CONSTRAINT fk_bar_cd FOREIGN KEY (a, b) REFERENCES bar (c, d) ON UPDATE CASCADE;",
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql2pgroll/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package sql2pgroll
import (
"fmt"

pgq "github.com/pganalyze/pg_query_go/v6"
pgq "github.com/xataio/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package sql2pgroll

import (
pgq "github.com/pganalyze/pg_query_go/v6"
pgq "github.com/xataio/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql2pgroll/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package sql2pgroll
import (
"strings"

pgq "github.com/pganalyze/pg_query_go/v6"
pgq "github.com/xataio/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)
Expand Down
18 changes: 17 additions & 1 deletion pkg/sql2pgroll/expect/alter_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,23 @@ var AlterColumnOp9 = &migrations.OpAlterColumn{
var AlterColumnOp10 = &migrations.OpAlterColumn{
Table: "foo",
Column: "bar",
Default: nullable.NewNullableWithValue("'b0101'"),
Default: nullable.NewNullableWithValue("b'0101'"),
Up: sql2pgroll.PlaceHolderSQL,
Down: sql2pgroll.PlaceHolderSQL,
}

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

var AlterColumnOp12 = &migrations.OpAlterColumn{
Table: "foo",
Column: "bar",
Default: nullable.NewNullableWithValue("(first_name || ' ') || last_name"),
Up: sql2pgroll.PlaceHolderSQL,
Down: sql2pgroll.PlaceHolderSQL,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql2pgroll/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package sql2pgroll

import (
pgq "github.com/pganalyze/pg_query_go/v6"
pgq "github.com/xataio/pg_query_go/v6"

"github.com/xataio/pgroll/pkg/migrations"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql2pgroll/typename.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

pgq "github.com/pganalyze/pg_query_go/v6"
pgq "github.com/xataio/pg_query_go/v6"
)

// convertTypeName converts a TypeName node to a string.
Expand Down

0 comments on commit 986c1f1

Please sign in to comment.