Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(schema-editor): support rename and drop schema (#4156)

boojack88bdade3cac2
diff --git a/api/database.go b/api/database.go
index e38bce6df6..87dcda513c 100644
--- a/api/database.go
+++ b/api/database.go
@@ -130,6 +130,8 @@ type DatabaseEdit struct {
DatabaseID int `json:"databaseId"`
CreateSchemaList []*CreateSchemaContext `json:"createSchemaList"`
+ RenameSchemaList []*RenameSchemaContext `json:"renameSchemaList"`
+ DropSchemaList []*DropSchemaContext `json:"dropSchemaList"`
CreateTableList []*CreateTableContext `json:"createTableList"`
AlterTableList []*AlterTableContext `json:"alterTableList"`
RenameTableList []*RenameTableContext `json:"renameTableList"`
@@ -141,6 +143,17 @@ type CreateSchemaContext struct {
Schema string `json:"schema"`
}
+// RenameSchemaContext is the edit database context to rename a schema.
+type RenameSchemaContext struct {
+ OldName string `json:"oldName"`
+ NewName string `json:"newName"`
+}
+
+// DropSchemaContext is the edit database context to drop a schema.
+type DropSchemaContext struct {
+ Schema string `json:"schema"`
+}
+
// CreateTableContext is the edit database context to create a table.
type CreateTableContext struct {
Schema string `json:"schema"`
diff --git a/plugin/parser/edit/pg/deparse.go b/plugin/parser/edit/pg/deparse.go
index 453c0cefd9..2c36ef3aec 100644
--- a/plugin/parser/edit/pg/deparse.go
+++ b/plugin/parser/edit/pg/deparse.go
@@ -30,6 +30,18 @@ func (*SchemaEditor) DeparseDatabaseEdit(databaseEdit *api.DatabaseEdit) (string
return "", err
}
}
+ for _, renameSchemaContext := range databaseEdit.RenameSchemaList {
+ err := transformRenameSchemaContext(ctx, renameSchemaContext)
+ if err != nil {
+ return "", err
+ }
+ }
+ for _, dropSchemaContext := range databaseEdit.DropSchemaList {
+ err := transformDropSchemaContext(ctx, dropSchemaContext)
+ if err != nil {
+ return "", err
+ }
+ }
for _, createTableContext := range databaseEdit.CreateTableList {
err := transformCreateTableContext(ctx, createTableContext)
if err != nil {
@@ -70,6 +82,23 @@ func transformCreateSchemaContext(ctx *DeparseContext, createSchemaContext *api.
return nil
}
+func transformRenameSchemaContext(ctx *DeparseContext, renameSchemaContext *api.RenameSchemaContext) error {
+ // TODO(steven): support rename schema with our pg parser.
+ alterSchemaStmt := fmt.Sprintf(`ALTER SCHEMA "%s" RENAME TO "%s";`, renameSchemaContext.OldName, renameSchemaContext.NewName)
+ ctx.StmtList = append(ctx.StmtList, alterSchemaStmt)
+ return nil
+}
+
+func transformDropSchemaContext(ctx *DeparseContext, dropSchemaContext *api.DropSchemaContext) error {
+ dropSchemaStmt := &ast.DropSchemaStmt{
+ SchemaList: []string{dropSchemaContext.Schema},
+ IfExists: true,
+ Behavior: ast.DropBehaviorNone,
+ }
+ ctx.NodeList = append(ctx.NodeList, dropSchemaStmt)
+ return nil
+}
+
func transformCreateTableContext(ctx *DeparseContext, createTableContext *api.CreateTableContext) error {
table := ast.TableDef{
Type: ast.TableTypeBaseTable,
diff --git a/plugin/parser/edit/pg/deparse_test.go b/plugin/parser/edit/pg/deparse_test.go
index 0fcb6f65f6..bc86ac87a7 100644
--- a/plugin/parser/edit/pg/deparse_test.go
+++ b/plugin/parser/edit/pg/deparse_test.go
@@ -11,6 +11,78 @@ import (
_ "github.com/bytebase/bytebase/plugin/parser/engine/pg"
)
+func TestDeparseRenameSchema(t *testing.T) {
+ tests := []struct {
+ name string
+ databaseEdit *api.DatabaseEdit
+ want string
+ }{
+ {
+ name: "renmae schema public",
+ databaseEdit: &api.DatabaseEdit{
+ DatabaseID: api.UnknownID,
+ RenameSchemaList: []*api.RenameSchemaContext{
+ {
+ OldName: "public",
+ NewName: "private",
+ },
+ },
+ },
+ want: "ALTER SCHEMA \"public\" RENAME TO \"private\";",
+ },
+ }
+
+ postgresEditor := &SchemaEditor{}
+ for _, test := range tests {
+ stmt, err := postgresEditor.DeparseDatabaseEdit(test.databaseEdit)
+ assert.NoError(t, err)
+ assert.Equal(t, test.want, stmt)
+ }
+}
+
+func TestDeparseDropSchema(t *testing.T) {
+ tests := []struct {
+ name string
+ databaseEdit *api.DatabaseEdit
+ want string
+ }{
+ {
+ name: "drop schema public",
+ databaseEdit: &api.DatabaseEdit{
+ DatabaseID: api.UnknownID,
+ DropSchemaList: []*api.DropSchemaContext{
+ {
+ Schema: "public",
+ },
+ },
+ },
+ want: "DROP SCHEMA IF EXISTS \"public\";",
+ },
+ {
+ name: "drop schema public and t1",
+ databaseEdit: &api.DatabaseEdit{
+ DatabaseID: api.UnknownID,
+ DropSchemaList: []*api.DropSchemaContext{
+ {
+ Schema: "public",
+ },
+ {
+ Schema: "t1",
+ },
+ },
+ },
+ want: "DROP SCHEMA IF EXISTS \"public\";\nDROP SCHEMA IF EXISTS \"t1\";",
+ },
+ }
+
+ postgresEditor := &SchemaEditor{}
+ for _, test := range tests {
+ stmt, err := postgresEditor.DeparseDatabaseEdit(test.databaseEdit)
+ assert.NoError(t, err)
+ assert.Equal(t, test.want, stmt)
+ }
+}
+
func TestDeparseCreateTable(t *testing.T) {
var defaultValue = "0"