From 51b2641396a551dc611cc0e2a44fa95edfff6011 Mon Sep 17 00:00:00 2001 From: Ryan Slade Date: Thu, 12 Dec 2024 14:21:08 +0100 Subject: [PATCH] Convert DROP TABLE SQL to pgroll operation (#529) 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 https://github.com/xataio/pgroll/issues/504 --- pkg/sql2pgroll/drop.go | 30 +++++++++++++++++++- pkg/sql2pgroll/drop_test.go | 43 ++++++++++++++++++++++++++++- pkg/sql2pgroll/expect/drop_table.go | 15 ++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 pkg/sql2pgroll/expect/drop_table.go diff --git a/pkg/sql2pgroll/drop.go b/pkg/sql2pgroll/drop.go index ed6596b2..e6303f96 100644 --- a/pkg/sql2pgroll/drop.go +++ b/pkg/sql2pgroll/drop.go @@ -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 } @@ -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 +} diff --git a/pkg/sql2pgroll/drop_test.go b/pkg/sql2pgroll/drop_test.go index 52c415eb..7a6c1ba5 100644 --- a/pkg/sql2pgroll/drop_test.go +++ b/pkg/sql2pgroll/drop_test.go @@ -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 { diff --git a/pkg/sql2pgroll/expect/drop_table.go b/pkg/sql2pgroll/expect/drop_table.go new file mode 100644 index 00000000..90d6a25e --- /dev/null +++ b/pkg/sql2pgroll/expect/drop_table.go @@ -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", +}