Skip to content

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

refactor: move differ to parser

d-bytebasecab8cfa75263
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 7c18511765..97fcdb6194 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -33,7 +33,6 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db/util"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/transform"
"github.com/bytebase/bytebase/backend/runner/backuprun"
@@ -980,7 +979,7 @@ func (s *DatabaseService) DiffSchema(ctx context.Context, request *v1pb.DiffSche
return nil, status.Errorf(codes.Internal, "failed to get parser engine, error: %v", err)
}
- diff, err := differ.SchemaDiff(engine, source, target, false /* ignoreCaseSensitive */)
+ diff, err := base.SchemaDiff(engine, source, target, false /* ignoreCaseSensitive */)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to compute diff between source and target schemas, error: %v", err)
}
diff --git a/backend/api/v1/schema_design_service.go b/backend/api/v1/schema_design_service.go
index a26c2f1582..013fbd6009 100644
--- a/backend/api/v1/schema_design_service.go
+++ b/backend/api/v1/schema_design_service.go
@@ -14,7 +14,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
@@ -539,7 +539,7 @@ func (*SchemaDesignService) DiffMetadata(_ context.Context, request *v1pb.DiffMe
return nil, err
}
- diff, err := differ.SchemaDiff(convertEngine(request.Engine), sourceSchema, targetSchema, false /* ignoreCaseSensitive */)
+ diff, err := base.SchemaDiff(convertEngine(request.Engine), sourceSchema, targetSchema, false /* ignoreCaseSensitive */)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to compute diff between source and target schemas, error: %v", err)
}
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index 8f5e0fc58b..65f5332011 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -11,13 +11,14 @@ import (
)
var (
- mux sync.Mutex
- queryValidators = make(map[storepb.Engine]ValidateSQLForEditorFunc)
- fieldMaskers = make(map[storepb.Engine]GetMaskedFieldsFunc)
- changedResourcesGetter = make(map[storepb.Engine]ExtractChangedResourcesFunc)
- resourcesGetter = make(map[storepb.Engine]ExtractResourceListFunc)
- splitter = make(map[storepb.Engine]SplitMultiSQLFunc)
- databaseGetter = make(map[storepb.Engine]ExtractDatabaseListFunc)
+ mux sync.Mutex
+ queryValidators = make(map[storepb.Engine]ValidateSQLForEditorFunc)
+ fieldMaskers = make(map[storepb.Engine]GetMaskedFieldsFunc)
+ changedResourcesGetters = make(map[storepb.Engine]ExtractChangedResourcesFunc)
+ resourcesGetters = make(map[storepb.Engine]ExtractResourceListFunc)
+ splitters = make(map[storepb.Engine]SplitMultiSQLFunc)
+ databaseGetters = make(map[storepb.Engine]ExtractDatabaseListFunc)
+ schemaDiffers = make(map[storepb.Engine]SchemaDiffFunc)
)
type ValidateSQLForEditorFunc func(string) (bool, error)
@@ -26,6 +27,7 @@ type ExtractChangedResourcesFunc func(string, string, string) ([]SchemaResource,
type ExtractResourceListFunc func(string, string, string) ([]SchemaResource, error)
type SplitMultiSQLFunc func(string) ([]SingleSQL, error)
type ExtractDatabaseListFunc func(string, string) ([]string, error)
+type SchemaDiffFunc func(oldStmt, newStmt string, ignoreCaseSensitivity bool) (string, error)
func RegisterQueryValidator(engine storepb.Engine, f ValidateSQLForEditorFunc) {
mux.Lock()
@@ -73,15 +75,15 @@ func ExtractSensitiveField(engine storepb.Engine, statement string, currentDatab
func RegisterExtractChangedResourcesFunc(engine storepb.Engine, f ExtractChangedResourcesFunc) {
mux.Lock()
defer mux.Unlock()
- if _, dup := changedResourcesGetter[engine]; dup {
+ if _, dup := changedResourcesGetters[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- changedResourcesGetter[engine] = f
+ changedResourcesGetters[engine] = f
}
// ExtractChangedResources extracts the changed resources from the SQL.
func ExtractChangedResources(engine storepb.Engine, currentDatabase string, currentSchema string, sql string) ([]SchemaResource, error) {
- f, ok := changedResourcesGetter[engine]
+ f, ok := changedResourcesGetters[engine]
if !ok {
return nil, errors.Errorf("engine %s is not supported", engine)
}
@@ -91,14 +93,14 @@ func ExtractChangedResources(engine storepb.Engine, currentDatabase string, curr
func RegisterExtractResourceListFunc(engine storepb.Engine, f ExtractResourceListFunc) {
mux.Lock()
defer mux.Unlock()
- if _, dup := resourcesGetter[engine]; dup {
+ if _, dup := resourcesGetters[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- resourcesGetter[engine] = f
+ resourcesGetters[engine] = f
}
func ExtractResourceList(engine storepb.Engine, currentDatabase string, currentSchema string, sql string) ([]SchemaResource, error) {
- f, ok := resourcesGetter[engine]
+ f, ok := resourcesGetters[engine]
if !ok {
return nil, errors.Errorf("engine %s is not supported", engine)
}
@@ -108,15 +110,15 @@ func ExtractResourceList(engine storepb.Engine, currentDatabase string, currentS
func RegisterSplitterFunc(engine storepb.Engine, f SplitMultiSQLFunc) {
mux.Lock()
defer mux.Unlock()
- if _, dup := splitter[engine]; dup {
+ if _, dup := splitters[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- splitter[engine] = f
+ splitters[engine] = f
}
// SplitMultiSQL splits statement into a slice of the single SQL.
func SplitMultiSQL(engine storepb.Engine, statement string) ([]SingleSQL, error) {
- f, ok := splitter[engine]
+ f, ok := splitters[engine]
if !ok {
return nil, errors.Errorf("engine %s is not supported", engine)
}
@@ -126,17 +128,34 @@ func SplitMultiSQL(engine storepb.Engine, statement string) ([]SingleSQL, error)
func RegisterExtractDatabaseListFunc(engine storepb.Engine, f ExtractDatabaseListFunc) {
mux.Lock()
defer mux.Unlock()
- if _, dup := databaseGetter[engine]; dup {
+ if _, dup := databaseGetters[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- databaseGetter[engine] = f
+ databaseGetters[engine] = f
}
// ExtractDatabaseList extracts all databases from statement.
func ExtractDatabaseList(engine storepb.Engine, statement string, currentDatabase string) ([]string, error) {
- f, ok := databaseGetter[engine]
+ f, ok := databaseGetters[engine]
if !ok {
return nil, errors.Errorf("engine %s is not supported", engine)
}
return f(statement, currentDatabase)
}
+
+func RegisterSchemaDiffFunc(engine storepb.Engine, f SchemaDiffFunc) {
+ mux.Lock()
+ defer mux.Unlock()
+ if _, dup := schemaDiffers[engine]; dup {
+ panic(fmt.Sprintf("Register called twice %s", engine))
+ }
+ schemaDiffers[engine] = f
+}
+
+func SchemaDiff(engine storepb.Engine, oldStmt, newStmt string, ignoreCaseSensitivity bool) (string, error) {
+ f, ok := schemaDiffers[engine]
+ if !ok {
+ return "", errors.Errorf("engine %s is not supported", engine)
+ }
+ return f(oldStmt, newStmt, ignoreCaseSensitivity)
+}
diff --git a/backend/plugin/parser/sql/differ/mysql/differ.go b/backend/plugin/parser/mysql/differ.go
similarity index 98%
rename from backend/plugin/parser/sql/differ/mysql/differ.go
rename to backend/plugin/parser/mysql/differ.go
index 1847ebf251..e64b875b15 100644
--- a/backend/plugin/parser/sql/differ/mysql/differ.go
+++ b/backend/plugin/parser/mysql/differ.go
@@ -19,23 +19,17 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pkg/errors"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
-
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
// Register pingcap parser driver.
driver "github.com/pingcap/tidb/types/parser_driver"
)
-var (
- _ differ.SchemaDiffer = (*SchemaDiffer)(nil)
-)
-
func init() {
- differ.Register(storepb.Engine_MYSQL, &SchemaDiffer{})
- differ.Register(storepb.Engine_TIDB, &SchemaDiffer{})
- differ.Register(storepb.Engine_OCEANBASE, &SchemaDiffer{})
+ base.RegisterSchemaDiffFunc(storepb.Engine_MYSQL, SchemaDiff)
+ base.RegisterSchemaDiffFunc(storepb.Engine_TIDB, SchemaDiff)
+ base.RegisterSchemaDiffFunc(storepb.Engine_OCEANBASE, SchemaDiff)
}
const (
@@ -43,10 +37,6 @@ const (
enableFKCheckStmt string = "SET FOREIGN_KEY_CHECKS=1;\n"
)
-// SchemaDiffer it the parser for MySQL dialect.
-type SchemaDiffer struct {
-}
-
// diffNode defines different modification types as the safe change order.
// The safe change order means we can change them with no dependency conflicts as this order.
type diffNode struct {
@@ -568,7 +558,7 @@ type constraintMap map[string]*ast.Constraint
// SchemaDiff returns the schema diff.
// It only supports schema information from mysqldump.
-func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, ignoreCaseSensitive bool) (string, error) {
+func SchemaDiff(oldStmt, newStmt string, ignoreCaseSensitive bool) (string, error) {
// 1. Preprocessing Stage.
// TiDB parser doesn't support some statements like `CREATE EVENT`, so we need to extract them out and diff them based on string compare.
oldUnsupportedStmtList, oldSupportedStmt, err := classifyStatement(oldStmt)
@@ -594,13 +584,13 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, ignoreCaseSensitive boo
}
func classifyStatement(statement string) ([]string, string, error) {
- unsupported, supported, err := mysqlparser.ExtractTiDBUnsupportedStmts(statement)
+ unsupported, supported, err := ExtractTiDBUnsupportedStmts(statement)
if err != nil {
return nil, "", errors.Wrapf(err, "failed to extract TiDB unsupported statements from statements %q", statement)
}
var afterFilter []string
for _, stmt := range unsupported {
- if !mysqlparser.IsDelimiter(stmt) {
+ if !IsDelimiter(stmt) {
afterFilter = append(afterFilter, stmt)
}
}
diff --git a/backend/plugin/parser/sql/differ/mysql/differ_column_test.go b/backend/plugin/parser/mysql/differ_column_test.go
similarity index 100%
rename from backend/plugin/parser/sql/differ/mysql/differ_column_test.go
rename to backend/plugin/parser/mysql/differ_column_test.go
diff --git a/backend/plugin/parser/sql/differ/mysql/differ_constraint_test.go b/backend/plugin/parser/mysql/differ_constraint_test.go
similarity index 100%
rename from backend/plugin/parser/sql/differ/mysql/differ_constraint_test.go
rename to backend/plugin/parser/mysql/differ_constraint_test.go
diff --git a/backend/plugin/parser/sql/differ/mysql/differ_table_test.go b/backend/plugin/parser/mysql/differ_table_test.go
similarity index 100%
rename from backend/plugin/parser/sql/differ/mysql/differ_table_test.go
rename to backend/plugin/parser/mysql/differ_table_test.go
diff --git a/backend/plugin/parser/sql/differ/mysql/differ_test.go b/backend/plugin/parser/mysql/differ_test.go
similarity index 96%
rename from backend/plugin/parser/sql/differ/mysql/differ_test.go
rename to backend/plugin/parser/mysql/differ_test.go
index a92f0f44cc..c8a1722656 100644
--- a/backend/plugin/parser/sql/differ/mysql/differ_test.go
+++ b/backend/plugin/parser/mysql/differ_test.go
@@ -92,9 +92,8 @@ type testCase struct {
func testDiffWithoutDisableForeignKeyCheck(t *testing.T, testCases []testCase) {
a := require.New(t)
- mysqlDiffer := &SchemaDiffer{}
for _, test := range testCases {
- out, err := mysqlDiffer.SchemaDiff(test.old, test.new, true /* ignoreCaseSensitive */)
+ out, err := SchemaDiff(test.old, test.new, true /* ignoreCaseSensitive */)
a.NoError(err)
if len(out) > 0 {
a.Equal(disableFKCheckStmt, out[:len(disableFKCheckStmt)])
diff --git a/backend/plugin/parser/sql/differ/mysql/differ_utils.go b/backend/plugin/parser/mysql/differ_utils.go
similarity index 100%
rename from backend/plugin/parser/sql/differ/mysql/differ_utils.go
rename to backend/plugin/parser/mysql/differ_utils.go
diff --git a/backend/plugin/parser/sql/differ/mysql/differ_utils_test.go b/backend/plugin/parser/mysql/differ_utils_test.go
similarity index 100%
rename from backend/plugin/parser/sql/differ/mysql/differ_utils_test.go
rename to backend/plugin/parser/mysql/differ_utils_test.go
diff --git a/backend/plugin/parser/sql/differ/pg/differ.go b/backend/plugin/parser/pg/differ.go
similarity index 97%
rename from backend/plugin/parser/sql/differ/pg/differ.go
rename to backend/plugin/parser/pg/differ.go
index a885d4b09e..9460e61b94 100644
--- a/backend/plugin/parser/sql/differ/pg/differ.go
+++ b/backend/plugin/parser/pg/differ.go
@@ -9,23 +9,14 @@ import (
"github.com/pkg/errors"
- pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-var (
- _ differ.SchemaDiffer = (*SchemaDiffer)(nil)
-)
-
func init() {
- differ.Register(storepb.Engine_POSTGRES, &SchemaDiffer{})
-}
-
-// SchemaDiffer it the parser for PostgreSQL dialect.
-type SchemaDiffer struct {
+ base.RegisterSchemaDiffFunc(storepb.Engine_POSTGRES, SchemaDiff)
}
// diffNode defines different modification types as the safe change order.
@@ -233,7 +224,7 @@ func newTypeInfo(id int, createType *ast.CreateTypeStmt) *typeInfo {
}
func (m schemaMap) addTable(id int, table *ast.CreateTableStmt) error {
- if pgparser.IsSystemSchema(table.Name.Schema) {
+ if IsSystemSchema(table.Name.Schema) {
return nil
}
schema, exists := m[table.Name.Schema]
@@ -253,7 +244,7 @@ func (m schemaMap) getTable(schemaName string, tableName string) *tableInfo {
}
func (m schemaMap) addConstraint(id int, addConstraint *ast.AddConstraintStmt) error {
- if pgparser.IsSystemSchema(addConstraint.Table.Schema) {
+ if IsSystemSchema(addConstraint.Table.Schema) {
return nil
}
schema, exists := m[addConstraint.Table.Schema]
@@ -285,7 +276,7 @@ func (m schemaMap) getConstraint(schemaName string, tableName string, constraint
}
func (m schemaMap) addExtension(id int, extension *ast.CreateExtensionStmt) error {
- if pgparser.IsSystemSchema(extension.Schema) {
+ if IsSystemSchema(extension.Schema) {
return nil
}
schema, exists := m[extension.Schema]
@@ -305,7 +296,7 @@ func (m schemaMap) getExtension(schemaName string, extensionName string) *extens
}
func (m schemaMap) addFunction(id int, function *ast.CreateFunctionStmt) error {
- if pgparser.IsSystemSchema(function.Function.Schema) {
+ if IsSystemSchema(function.Function.Schema) {
return nil
}
schema, exists := m[function.Function.Schema]
@@ -329,7 +320,7 @@ func (m schemaMap) getFunction(schemaName string, signature string) *functionInf
}
func (m schemaMap) addIndex(id int, index *ast.CreateIndexStmt) error {
- if pgparser.IsSystemSchema(index.Index.Table.Schema) {
+ if IsSystemSchema(index.Index.Table.Schema) {
return nil
}
schema, exists := m[index.Index.Table.Schema]
@@ -349,7 +340,7 @@ func (m schemaMap) getIndex(schemaName string, indexName string) *indexInfo {
}
func (m schemaMap) addSequence(id int, sequence *ast.CreateSequenceStmt) error {
- if pgparser.IsSystemSchema(sequence.SequenceDef.SequenceName.Schema) {
+ if IsSystemSchema(sequence.SequenceDef.SequenceName.Schema) {
return nil
}
schema, exists := m[sequence.SequenceDef.SequenceName.Schema]
@@ -369,7 +360,7 @@ func (m schemaMap) getSequence(schemaName string, sequenceName string) *sequence
}
func (m schemaMap) addTrigger(id int, trigger *ast.CreateTriggerStmt) error {
- if pgparser.IsSystemSchema(trigger.Trigger.Table.Schema) {
+ if IsSystemSchema(trigger.Trigger.Table.Schema) {
return nil
}
schema, exists := m[trigger.Trigger.Table.Schema]
@@ -397,7 +388,7 @@ func (m schemaMap) getTrigger(schemaName string, tableName string, triggerName s
}
func (m schemaMap) addType(id int, createType *ast.CreateTypeStmt) error {
- if pgparser.IsSystemSchema(createType.Type.TypeName().Schema) {
+ if IsSystemSchema(createType.Type.TypeName().Schema) {
return nil
}
schema, exists := m[createType.Type.TypeName().Schema]
@@ -438,7 +429,7 @@ func (m schemaMap) addSequenceOwnedBy(id int, alterStmt *ast.AlterSequenceStmt)
return errors.Errorf("expect OwnedBy only, but found %v", alterStmt)
}
- if pgparser.IsSystemSchema(alterStmt.Name.Schema) {
+ if IsSystemSchema(alterStmt.Name.Schema) {
return nil
}
schema, exists := m[alterStmt.Name.Schema]
@@ -538,7 +529,7 @@ func mergeDefaultIntoColumn(nodeList []ast.Node) ([]ast.Node, error) {
}
// SchemaDiff computes the schema differences between old and new schema.
-func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error) {
+func SchemaDiff(oldStmt, newStmt string, _ bool) (string, error) {
oldNodes, oldPartitions, err := parseAndPreprocessStatement(oldStmt)
if err != nil {
return "", errors.Wrapf(err, "failed to parse and preprocess old statements %q", oldStmt)
@@ -639,7 +630,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Name.Schema, stmt.Name.Name)]; exists {
continue
}
- if pgparser.IsSystemSchema(stmt.Name.Schema) {
+ if IsSystemSchema(stmt.Name.Schema) {
continue
}
oldTable := oldSchemaMap.getTable(stmt.Name.Schema, stmt.Name.Name)
@@ -654,7 +645,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateSchemaStmt:
- if pgparser.IsSystemSchema(stmt.Name) {
+ if IsSystemSchema(stmt.Name) {
continue
}
schema, hasSchema := oldSchemaMap[stmt.Name]
@@ -668,7 +659,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Table.Schema, stmt.Table.Name)]; exists {
continue
}
- if pgparser.IsSystemSchema(stmt.Table.Schema) {
+ if IsSystemSchema(stmt.Table.Schema) {
continue
}
for _, alterItem := range stmt.AlterItemList {
@@ -708,7 +699,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if _, exists := newPartitionMap[fmt.Sprintf("%s.%s", stmt.Index.Table.Schema, stmt.Index.Table.Name)]; exists {
continue
}
- if pgparser.IsSystemSchema(stmt.Index.Table.Schema) {
+ if IsSystemSchema(stmt.Index.Table.Schema) {
continue
}
oldIndex := oldSchemaMap.getIndex(stmt.Index.Table.Schema, stmt.Index.Name)
@@ -723,7 +714,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateSequenceStmt:
- if pgparser.IsSystemSchema(stmt.SequenceDef.SequenceName.Schema) {
+ if IsSystemSchema(stmt.SequenceDef.SequenceName.Schema) {
continue
}
oldSequence := oldSchemaMap.getSequence(stmt.SequenceDef.SequenceName.Schema, stmt.SequenceDef.SequenceName.Name)
@@ -741,7 +732,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
if !onlySetOwnedBy(stmt) {
return "", errors.Errorf("expect OwnedBy only, but found %v", stmt)
}
- if pgparser.IsSystemSchema(stmt.Name.Schema) {
+ if IsSystemSchema(stmt.Name.Schema) {
continue
}
oldSequence := oldSchemaMap.getSequence(stmt.Name.Schema, stmt.Name.Name)
@@ -755,7 +746,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateExtensionStmt:
- if pgparser.IsSystemSchema(stmt.Schema) {
+ if IsSystemSchema(stmt.Schema) {
continue
}
oldExtension := oldSchemaMap.getExtension(stmt.Schema, stmt.Name)
@@ -770,7 +761,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateFunctionStmt:
- if pgparser.IsSystemSchema(stmt.Function.Schema) {
+ if IsSystemSchema(stmt.Function.Schema) {
continue
}
signature, err := functionSignature(stmt.Function)
@@ -789,7 +780,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateTriggerStmt:
- if pgparser.IsSystemSchema(stmt.Trigger.Table.Schema) {
+ if IsSystemSchema(stmt.Trigger.Table.Schema) {
continue
}
oldTrigger := oldSchemaMap.getTrigger(stmt.Trigger.Table.Schema, stmt.Trigger.Table.Name, stmt.Trigger.Name)
@@ -804,7 +795,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error)
return "", err
}
case *ast.CreateTypeStmt:
- if pgparser.IsSystemSchema(stmt.Type.TypeName().Schema) {
+ if IsSystemSchema(stmt.Type.TypeName().Schema) {
continue
}
oldType := oldSchemaMap.getType(stmt.Type.TypeName().Schema, stmt.Type.TypeName().Name)
diff --git a/backend/plugin/parser/sql/differ/pg/differ_test.go b/backend/plugin/parser/pg/differ_test.go
similarity index 91%
rename from backend/plugin/parser/sql/differ/pg/differ_test.go
rename to backend/plugin/parser/pg/differ_test.go
index 20d4feeb84..161c60c959 100644
--- a/backend/plugin/parser/sql/differ/pg/differ_test.go
+++ b/backend/plugin/parser/pg/differ_test.go
@@ -20,8 +20,6 @@ type DifferTestData struct {
}
func runDifferTest(t *testing.T, file string, record bool) {
- pgDiffer := &SchemaDiffer{}
-
var tests []DifferTestData
filepath := filepath.Join("test-data", file)
yamlFile, err := os.Open(filepath)
@@ -34,7 +32,7 @@ func runDifferTest(t *testing.T, file string, record bool) {
require.NoError(t, err)
for i, test := range tests {
- diff, err := pgDiffer.SchemaDiff(test.OldSchema, test.NewSchema, false /* ignoreCaseSensitive */)
+ diff, err := SchemaDiff(test.OldSchema, test.NewSchema, false /* ignoreCaseSensitive */)
require.NoError(t, err)
if record {
tests[i].Diff = diff
diff --git a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_constraint.yaml b/backend/plugin/parser/pg/test-data/test_differ_constraint.yaml
similarity index 100%
rename from backend/plugin/parser/sql/differ/pg/test-data/test_differ_constraint.yaml
rename to backend/plugin/parser/pg/test-data/test_differ_constraint.yaml
diff --git a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_data.yaml b/backend/plugin/parser/pg/test-data/test_differ_data.yaml
similarity index 100%
rename from backend/plugin/parser/sql/differ/pg/test-data/test_differ_data.yaml
rename to backend/plugin/parser/pg/test-data/test_differ_data.yaml
diff --git a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_merge.yaml b/backend/plugin/parser/pg/test-data/test_differ_merge.yaml
similarity index 100%
rename from backend/plugin/parser/sql/differ/pg/test-data/test_differ_merge.yaml
rename to backend/plugin/parser/pg/test-data/test_differ_merge.yaml
diff --git a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_schema.yaml b/backend/plugin/parser/pg/test-data/test_differ_schema.yaml
similarity index 100%
rename from backend/plugin/parser/sql/differ/pg/test-data/test_differ_schema.yaml
rename to backend/plugin/parser/pg/test-data/test_differ_schema.yaml
diff --git a/backend/plugin/parser/sql/differ/pg/test-data/test_differ_sequence.yaml b/backend/plugin/parser/pg/test-data/test_differ_sequence.yaml
similarity index 100%
rename from backend/plugin/parser/sql/differ/pg/test-data/test_differ_sequence.yaml
rename to backend/plugin/parser/pg/test-data/test_differ_sequence.yaml
diff --git a/backend/plugin/parser/sql/differ/plsql/differ.go b/backend/plugin/parser/plsql/differ.go
similarity index 93%
rename from backend/plugin/parser/sql/differ/plsql/differ.go
rename to backend/plugin/parser/plsql/differ.go
index 52116716eb..b2e072d21d 100644
--- a/backend/plugin/parser/sql/differ/plsql/differ.go
+++ b/backend/plugin/parser/plsql/differ.go
@@ -9,24 +9,14 @@ import (
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
-
plsql "github.com/bytebase/plsql-parser"
- plsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/plsql"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-var (
- _ differ.SchemaDiffer = (*SchemaDiffer)(nil)
-)
-
func init() {
- differ.Register(storepb.Engine_ORACLE, &SchemaDiffer{})
-}
-
-// SchemaDiffer is the schema differ for plsql.
-type SchemaDiffer struct {
+ base.RegisterSchemaDiffFunc(storepb.Engine_ORACLE, SchemaDiff)
}
type diffNode struct {
@@ -120,7 +110,7 @@ func (diff *diffNode) String() (string, error) {
}
// SchemaDiff implements the differ.SchemaDiffer interface.
-func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, _ bool) (string, error) {
+func SchemaDiff(oldStmt, newStmt string, _ bool) (string, error) {
oldSchemaInfo, err := buildSchemaInfo(oldStmt)
if err != nil {
return "", errors.Wrapf(err, "failed to build schema info for old statement")
@@ -228,7 +218,7 @@ func buildConstraintMap(table *tableInfo) map[string]plsql.IRelational_propertyC
if constraint.Constraint_name() == nil {
continue
}
- _, constraintName := plsqlparser.NormalizeConstraintName(constraint.Constraint_name())
+ _, constraintName := NormalizeConstraintName(constraint.Constraint_name())
if constraintName == "" {
continue
}
@@ -238,7 +228,7 @@ func buildConstraintMap(table *tableInfo) map[string]plsql.IRelational_propertyC
if constraint.Constraint_name() == nil {
continue
}
- _, constraintName := plsqlparser.NormalizeConstraintName(constraint.Constraint_name())
+ _, constraintName := NormalizeConstraintName(constraint.Constraint_name())
if constraintName == "" {
continue
}
@@ -264,7 +254,7 @@ func (diff *diffNode) diffConstraint(oldTable, newTable *tableInfo) error {
if constraint.Constraint_name() == nil {
continue
}
- _, constraintName := plsqlparser.NormalizeConstraintName(constraint.Constraint_name())
+ _, constraintName := NormalizeConstraintName(constraint.Constraint_name())
if constraintName == "" {
continue
}
@@ -284,7 +274,7 @@ func (diff *diffNode) diffConstraint(oldTable, newTable *tableInfo) error {
if constraint.Constraint_name() == nil {
continue
}
- _, constraintName := plsqlparser.NormalizeConstraintName(constraint.Constraint_name())
+ _, constraintName := NormalizeConstraintName(constraint.Constraint_name())
if constraintName == "" {
continue
}
@@ -404,7 +394,7 @@ func buildColumnMap(table *tableInfo) map[string]plsql.IColumn_definitionContext
if item.Column_definition() == nil {
continue
}
- columnName := plsqlparser.NormalizeIdentifierContext(item.Column_definition().Column_name().Identifier())
+ columnName := NormalizeIdentifierContext(item.Column_definition().Column_name().Identifier())
columnMap[columnName] = item.Column_definition()
}
return columnMap
@@ -425,7 +415,7 @@ func (diff *diffNode) diffColumn(oldTable, newTable *tableInfo) error {
continue
}
- newColumnName := plsqlparser.NormalizeIdentifierContext(item.Column_definition().Column_name().Identifier())
+ newColumnName := NormalizeIdentifierContext(item.Column_definition().Column_name().Identifier())
oldColumn, ok := oldColumnMap[newColumnName]
if !ok {
addColumns = append(addColumns, item.Column_definition())
@@ -439,7 +429,7 @@ func (diff *diffNode) diffColumn(oldTable, newTable *tableInfo) error {
delete(oldColumnMap, newColumnName)
}
for _, column := range oldColumnMap {
- dropColumns = append(dropColumns, plsqlparser.NormalizeIdentifierContext(column.Column_name().Identifier()))
+ dropColumns = append(dropColumns, NormalizeIdentifierContext(column.Column_name().Identifier()))
}
sort.Slice(dropColumns, func(i, j int) bool {
@@ -590,7 +580,7 @@ func isColumnEqual(oldColumn, newColumn plsql.IColumn_definitionContext) bool {
}
func buildSchemaInfo(statement string) (*schemaInfo, error) {
- node, _, err := plsqlparser.ParsePLSQL(statement)
+ node, _, err := ParsePLSQL(statement)
if err != nil {
return nil, err
}
@@ -626,7 +616,7 @@ func (l *buildSchemaInfoListener) EnterCreate_table(ctx *plsql.Create_tableConte
}
if ctx.Schema_name() != nil {
- schemaName := plsqlparser.NormalizeIdentifierContext(ctx.Schema_name().Identifier())
+ schemaName := NormalizeIdentifierContext(ctx.Schema_name().Identifier())
if l.schemaInfo.name == "" {
l.schemaInfo.name = schemaName
}
@@ -635,7 +625,7 @@ func (l *buildSchemaInfoListener) EnterCreate_table(ctx *plsql.Create_tableConte
return
}
}
- tableName := plsqlparser.NormalizeIdentifierContext(ctx.Table_name().Identifier())
+ tableName := NormalizeIdentifierContext(ctx.Table_name().Identifier())
l.schemaInfo.tableMap[tableName] = &tableInfo{
id: len(l.schemaInfo.tableMap),
name: tableName,
@@ -655,7 +645,7 @@ func (l *buildSchemaInfoListener) EnterCreate_index(ctx *plsql.Create_indexConte
return
}
- schema, index := plsqlparser.NormalizeIndexName(ctx.Index_name())
+ schema, index := NormalizeIndexName(ctx.Index_name())
if schema != "" && l.schemaInfo.name == "" {
l.schemaInfo.name = schema
}
diff --git a/backend/plugin/parser/sql/differ/plsql/differ_test.go b/backend/plugin/parser/plsql/differ_test.go
similarity index 90%
rename from backend/plugin/parser/sql/differ/plsql/differ_test.go
rename to backend/plugin/parser/plsql/differ_test.go
index a7db8c3763..b988162ced 100644
--- a/backend/plugin/parser/sql/differ/plsql/differ_test.go
+++ b/backend/plugin/parser/plsql/differ_test.go
@@ -18,8 +18,6 @@ type DifferTestData struct {
}
func runDifferTest(t *testing.T, file string, record bool) {
- plsqlDiffer := &SchemaDiffer{}
-
var tests []DifferTestData
filepath := filepath.Join("test-data", file)
yamlFile, err := os.Open(filepath)
@@ -32,7 +30,7 @@ func runDifferTest(t *testing.T, file string, record bool) {
require.NoError(t, err)
for i, test := range tests {
- diff, err := plsqlDiffer.SchemaDiff(test.OldSchema, test.NewSchema, false /* ignoreCaseSensitive */)
+ diff, err := SchemaDiff(test.OldSchema, test.NewSchema, false /* ignoreCaseSensitive */)
require.NoError(t, err)
if record {
tests[i].Diff = diff
diff --git a/backend/plugin/parser/sql/differ/plsql/test-data/test_differ_data.yaml b/backend/plugin/parser/plsql/test-data/test_differ_data.yaml
similarity index 100%
rename from backend/plugin/parser/sql/differ/plsql/test-data/test_differ_data.yaml
rename to backend/plugin/parser/plsql/test-data/test_differ_data.yaml
diff --git a/backend/plugin/parser/sql/differ/differ.go b/backend/plugin/parser/sql/differ/differ.go
deleted file mode 100644
index a33b450320..0000000000
--- a/backend/plugin/parser/sql/differ/differ.go
+++ /dev/null
@@ -1,47 +0,0 @@
-// Package differ provides the schema differ plugin.
-package differ
-
-import (
- "fmt"
- "sync"
-
- "github.com/pkg/errors"
-
- storepb "github.com/bytebase/bytebase/proto/generated-go/store"
-)
-
-// SchemaDiffer is the interface for schema differ.
-type SchemaDiffer interface {
- SchemaDiff(oldStmt, newStmt string, ignoreCaseSensitivity bool) (string, error)
-}
-
-var (
- differMu sync.RWMutex
- differs = make(map[storepb.Engine]SchemaDiffer)
-)
-
-// 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 storepb.Engine, d SchemaDiffer) {
- if d == nil {
- panic("parser: Register parser is nil")
- }
- differMu.Lock()
- defer differMu.Unlock()
- if _, dup := differs[engineType]; dup {
- panic(fmt.Sprintf("parser: Register called twice for differ %s", engineType))
- }
- differs[engineType] = d
-}
-
-// SchemaDiff returns the schema diff between old and new statements.
-func SchemaDiff(engineType storepb.Engine, oldStmt, newStmt string, ignoreCaseSensitive bool) (string, error) {
- differMu.RLock()
- p, ok := differs[engineType]
- differMu.RUnlock()
- if !ok {
- return "", errors.Errorf("engine: unknown engine type %v", engineType)
- }
- return p.SchemaDiff(oldStmt, newStmt, ignoreCaseSensitive)
-}
diff --git a/backend/runner/utils/sdl_utils.go b/backend/runner/utils/sdl_utils.go
index 81125968db..9f3e3c86c4 100644
--- a/backend/runner/utils/sdl_utils.go
+++ b/backend/runner/utils/sdl_utils.go
@@ -9,7 +9,7 @@ import (
"github.com/bytebase/bytebase/backend/component/dbfactory"
- "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/transform"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -47,7 +47,7 @@ func ComputeDatabaseSchemaDiff(ctx context.Context, instance *store.InstanceMess
if err != nil {
return "", errors.Wrapf(err, "failed to transform SDL format")
}
- diff, err := differ.SchemaDiff(engine, sdlFormat, newSchema, store.IgnoreDatabaseAndTableCaseSensitive(instance))
+ diff, err := base.SchemaDiff(engine, sdlFormat, newSchema, store.IgnoreDatabaseAndTableCaseSensitive(instance))
if err != nil {
return "", errors.Wrapf(err, "compute schema diff")
}
diff --git a/backend/server/minimal.go b/backend/server/minimal.go
index 3c85236df0..b84865aaa6 100644
--- a/backend/server/minimal.go
+++ b/backend/server/minimal.go
@@ -18,10 +18,6 @@ import (
_ "github.com/bytebase/bytebase/backend/plugin/advisor/mysql"
_ "github.com/bytebase/bytebase/backend/plugin/advisor/pg"
- // Differs.
- _ "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ/mysql"
- _ "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ/pg"
-
// Editors.
_ "github.com/bytebase/bytebase/backend/plugin/parser/sql/edit/mysql"
_ "github.com/bytebase/bytebase/backend/plugin/parser/sql/edit/pg"
diff --git a/backend/server/ultimate.go b/backend/server/ultimate.go
index fdac05bcac..dc79778e28 100644
--- a/backend/server/ultimate.go
+++ b/backend/server/ultimate.go
@@ -24,7 +24,4 @@ import (
_ "github.com/bytebase/bytebase/backend/plugin/advisor/mssql"
_ "github.com/bytebase/bytebase/backend/plugin/advisor/oracle"
_ "github.com/bytebase/bytebase/backend/plugin/advisor/snowflake"
-
- // Differs.
- _ "github.com/bytebase/bytebase/backend/plugin/parser/sql/differ/plsql"
)