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

Convert DROP COLUMN SQL to pgroll operation #521

Merged
merged 7 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 29 additions & 0 deletions pkg/sql2pgroll/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

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

"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down Expand Up @@ -35,6 +36,8 @@ func convertAlterTableStmt(stmt *pgq.AlterTableStmt) (migrations.Operations, err
op, err = convertAlterTableAlterColumnType(stmt, alterTableCmd)
case pgq.AlterTableType_AT_AddConstraint:
op, err = convertAlterTableAddConstraint(stmt, alterTableCmd)
case pgq.AlterTableType_AT_DropColumn:
op, err = convertAlterTableDropColumn(stmt, alterTableCmd)
}

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

// convertAlterTableDropColumn converts SQL statements like:
//
// `ALTER TABLE foo DROP COLUMN bar
//
// to an OpDropColumn operation.
func convertAlterTableDropColumn(stmt *pgq.AlterTableStmt, cmd *pgq.AlterTableCmd) (migrations.Operation, error) {
switch cmd.Behavior {
case pgq.DropBehavior_DROP_RESTRICT, pgq.DropBehavior_DROP_BEHAVIOR_UNDEFINED:
// Supported
case pgq.DropBehavior_DROP_CASCADE:
// Fall back to SQL
return nil, nil
}

// IF EXISTS not supported
if cmd.MissingOk {
return nil, nil
}
andrew-farries marked this conversation as resolved.
Show resolved Hide resolved

return &migrations.OpDropColumn{
Table: stmt.GetRelation().GetRelname(),
Column: cmd.GetName(),
Down: PlaceHolderSQL,
}, nil
}

// canConvertUniqueConstraint checks if the unique constraint `constraint` can
// be faithfully converted to an OpCreateConstraint operation without losing
// information.
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql2pgroll/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
Expand Down Expand Up @@ -43,6 +44,14 @@ func TestConvertAlterTableStatements(t *testing.T) {
sql: "ALTER TABLE foo ADD CONSTRAINT bar UNIQUE (a, b)",
expectedOp: expect.CreateConstraintOp2,
},
{
sql: "ALTER TABLE foo DROP COLUMN bar",
expectedOp: expect.DropColumnOp1,
},
{
sql: "ALTER TABLE foo DROP COLUMN bar RESTRICT ",
expectedOp: expect.DropColumnOp1,
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -72,6 +81,10 @@ func TestUnconvertableAlterTableAddConstraintStatements(t *testing.T) {
// operations when changing data type.
`ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text COLLATE "en_US"`,
"ALTER TABLE foo ALTER COLUMN a SET DATA TYPE text USING 'foo'",

// CASCADE and IF EXISTS clauses are not represented by OpDropColumn
"ALTER TABLE foo DROP COLUMN bar CASCADE",
"ALTER TABLE foo DROP COLUMN IF EXISTS bar",
andrew-farries marked this conversation as resolved.
Show resolved Hide resolved
}

for _, sql := range tests {
Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

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

andrew-farries marked this conversation as resolved.
Show resolved Hide resolved
"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sql2pgroll

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

"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql2pgroll/expect/drop_column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package expect

import (
"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
)

var DropColumnOp1 = &migrations.OpDropColumn{
Table: "foo",
Column: "bar",
Down: sql2pgroll.PlaceHolderSQL,
}
1 change: 1 addition & 0 deletions pkg/sql2pgroll/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sql2pgroll

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

"github.com/xataio/pgroll/pkg/migrations"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/sql2pgroll/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xataio/pgroll/pkg/migrations"
"github.com/xataio/pgroll/pkg/sql2pgroll"
"github.com/xataio/pgroll/pkg/sql2pgroll/expect"
Expand Down
Loading