Skip to content

Commit

Permalink
Convert DROP TABLE SQL to pgroll operation (#529)
Browse files Browse the repository at this point in the history
Converts `DROP TABLE` statements in these forms:

```sql
DROP TABLE foo
DROP TABLE foo RESTRICT
DROP TABLE foo.bar
DROP TABLE IF EXISTS foo
```

These forms fall back to raw SQL:

```sql
DROP TABLE foo CASCADE
```

Part of #504
  • Loading branch information
ryanslade authored Dec 12, 2024
1 parent ef07810 commit 51b2641
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
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",
}

0 comments on commit 51b2641

Please sign in to comment.