Skip to content

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

chore: register schema edit plugin (#3385)

boojack5db4b1522899
diff --git a/plugin/parser/edit/edit.go b/plugin/parser/edit/edit.go
new file mode 100644
index 0000000000..12fae4a0b3
--- /dev/null
+++ b/plugin/parser/edit/edit.go
@@ -0,0 +1,47 @@
+// Package edit provides the schema edit plugin.
+package edit
+
+import (
+ "sync"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/plugin/parser"
+)
+
+// SchemaEditor is the interface for schema editor.
+type SchemaEditor interface {
+ DeparseDatabaseEdit(databaseEdit *api.DatabaseEdit) (string, error)
+}
+
+var (
+ editorMu sync.RWMutex
+ editors = make(map[parser.EngineType]SchemaEditor)
+)
+
+// Register makes a differ available by the provided id.
+// If Register is called twice with the same name or if differ is nil,
+// it panics.
+func Register(engineType parser.EngineType, se SchemaEditor) {
+ if se == nil {
+ panic("parser: Register parser is nil")
+ }
+ editorMu.Lock()
+ defer editorMu.Unlock()
+ if _, dup := editors[engineType]; dup {
+ panic("parser: Register called twice for differ " + engineType)
+ }
+ editors[engineType] = se
+}
+
+// DeparseDatabaseEdit returns the DDL statement from DatabaseEdit structure.
+func DeparseDatabaseEdit(engineType parser.EngineType, databaseEdit *api.DatabaseEdit) (string, error) {
+ editorMu.RLock()
+ se, ok := editors[engineType]
+ editorMu.RUnlock()
+ if !ok {
+ return "", errors.Errorf("engine: unknown engine type %v", engineType)
+ }
+ return se.DeparseDatabaseEdit(databaseEdit)
+}
diff --git a/plugin/parser/edit/mysql/schema.go b/plugin/parser/edit/mysql/edit.go
similarity index 90%
rename from plugin/parser/edit/mysql/schema.go
rename to plugin/parser/edit/mysql/edit.go
index d82f58c527..2e051973bb 100644
--- a/plugin/parser/edit/mysql/schema.go
+++ b/plugin/parser/edit/mysql/edit.go
@@ -13,12 +13,26 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/api"
+ bbparser "github.com/bytebase/bytebase/plugin/parser"
+ "github.com/bytebase/bytebase/plugin/parser/edit"
// Register pingcap parser driver.
_ "github.com/pingcap/tidb/types/parser_driver"
)
-func deparseDatabaseEdit(databaseEdit *api.DatabaseEdit) (string, error) {
+var (
+ _ edit.SchemaEditor = (*SchemaEditor)(nil)
+)
+
+func init() {
+ edit.Register(bbparser.MySQL, &SchemaEditor{})
+}
+
+// SchemaEditor it the editor for MySQL dialect.
+type SchemaEditor struct{}
+
+// DeparseDatabaseEdit deparses DatabaseEdit to DDL statement.
+func (*SchemaEditor) DeparseDatabaseEdit(databaseEdit *api.DatabaseEdit) (string, error) {
var stmtList []string
for _, createTableContext := range databaseEdit.CreateTableList {
createTableStmt := transformCreateTableContext(createTableContext)
diff --git a/plugin/parser/edit/mysql/schema_test.go b/plugin/parser/edit/mysql/edit_test.go
similarity index 95%
rename from plugin/parser/edit/mysql/schema_test.go
rename to plugin/parser/edit/mysql/edit_test.go
index f4a94e08f3..1060f026e5 100644
--- a/plugin/parser/edit/mysql/schema_test.go
+++ b/plugin/parser/edit/mysql/edit_test.go
@@ -96,8 +96,9 @@ func TestDeparseDatabaseEdit(t *testing.T) {
},
}
+ mysqlEditor := &SchemaEditor{}
for _, test := range tests {
- stmt, err := deparseDatabaseEdit(test.databaseEdit)
+ stmt, err := mysqlEditor.DeparseDatabaseEdit(test.databaseEdit)
assert.NoError(t, err)
assert.Equal(t, test.want, stmt)
}