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 TABLE SQL to pgroll operation #529

Merged
merged 3 commits into from
Dec 12, 2024
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
30 changes: 29 additions & 1 deletion pkg/sql2pgroll/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import (

// convertDropStatement converts supported drop statements to pgroll operations
func convertDropStatement(stmt *pgq.DropStmt) (migrations.Operations, error) {
if stmt.RemoveType == pgq.ObjectType_OBJECT_INDEX {
switch stmt.RemoveType {
case pgq.ObjectType_OBJECT_INDEX:
return convertDropIndexStatement(stmt)
case pgq.ObjectType_OBJECT_TABLE:
return convertDropTableStatement(stmt)

}
return nil, nil
}
Expand Down Expand Up @@ -46,3 +50,27 @@ func canConvertDropIndex(stmt *pgq.DropStmt) bool {
}
return true
}

// convertDropTableStatement converts simple DROP TABLE statements to pgroll operations
func convertDropTableStatement(stmt *pgq.DropStmt) (migrations.Operations, error) {
if !canConvertDropTable(stmt) {
return nil, nil
}

items := stmt.GetObjects()[0].GetList().GetItems()
parts := make([]string, len(items))
for i, item := range items {
parts[i] = item.GetString_().GetSval()
}

return migrations.Operations{
&migrations.OpDropTable{
Name: strings.Join(parts, "."),
},
}, nil
}

// canConvertDropTable checks whether we can convert the statement without losing any information.
func canConvertDropTable(stmt *pgq.DropStmt) bool {
return stmt.Behavior != pgq.DropBehavior_DROP_CASCADE
}
43 changes: 42 additions & 1 deletion pkg/sql2pgroll/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,52 @@ func TestDropIndexStatements(t *testing.T) {
}
}

func TestUnconvertableDropIndexStatements(t *testing.T) {
func TestDropTableStatements(t *testing.T) {
t.Parallel()

tests := []struct {
sql string
expectedOp migrations.Operation
}{
{
sql: "DROP TABLE foo",
expectedOp: expect.DropTableOp1,
},
{
sql: "DROP TABLE foo RESTRICT",
expectedOp: expect.DropTableOp1,
},
{
sql: "DROP TABLE IF EXISTS foo",
expectedOp: expect.DropTableOp1,
},
{
sql: "DROP TABLE foo.bar",
expectedOp: expect.DropTableOp2,
},
}

for _, tc := range tests {
t.Run(tc.sql, func(t *testing.T) {
ops, err := sql2pgroll.Convert(tc.sql)
require.NoError(t, err)

require.Len(t, ops, 1)

assert.Equal(t, tc.expectedOp, ops[0])
})
}
}

func TestUnconvertableDropStatements(t *testing.T) {
t.Parallel()

tests := []string{
// Drop index
"DROP INDEX foo CASCADE",

// Drop table
"DROP TABLE foo CASCADE",
}

for _, sql := range tests {
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql2pgroll/expect/drop_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache-2.0

package expect

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

var DropTableOp1 = &migrations.OpDropTable{
Name: "foo",
}

var DropTableOp2 = &migrations.OpDropTable{
Name: "foo.bar",
}