Skip to content

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

chore(schema system): refine compatibility advisor (#1390)

rebelice026c4581906b
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility.go b/plugin/advisor/mysql/advisor_migration_compatibility.go
index b7928a3793..2f1104d0c2 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility.go
@@ -30,7 +30,12 @@ func (adv *CompatibilityAdvisor) Check(ctx advisor.Context, statement string) ([
return errAdvice, nil
}
- c := &compatibilityChecker{}
+ level, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ c := &compatibilityChecker{level: level}
for _, stmtNode := range root {
(stmtNode).Accept(c)
}
@@ -40,13 +45,15 @@ func (adv *CompatibilityAdvisor) Check(ctx advisor.Context, statement string) ([
Status: advisor.Success,
Code: common.Ok,
Title: "OK",
- Content: "Migration is backward compatible"})
+ Content: "",
+ })
}
return c.adviceList, nil
}
type compatibilityChecker struct {
adviceList []advisor.Advice
+ level advisor.Status
}
// Enter implements the ast.Visitor interface
@@ -139,7 +146,7 @@ func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
if code != common.Ok {
v.adviceList = append(v.adviceList, advisor.Advice{
- Status: advisor.Warn,
+ Status: v.level,
Code: code,
Title: "Potential incompatible migration",
Content: fmt.Sprintf("%q may cause incompatibility with the existing data and code", in.Text()),
diff --git a/plugin/advisor/mysql/advisor_migration_compatibility_test.go b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
index 8d9be30cc3..701b909943 100644
--- a/plugin/advisor/mysql/advisor_migration_compatibility_test.go
+++ b/plugin/advisor/mysql/advisor_migration_compatibility_test.go
@@ -4,33 +4,12 @@ import (
"testing"
_ "github.com/pingcap/tidb/types/parser_driver"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
+ "github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
- "go.uber.org/zap"
)
-type test struct {
- statement string
- want []advisor.Advice
-}
-
-func runTests(t *testing.T, tests []test, adv advisor.Advisor) {
- logger, _ := zap.NewDevelopmentConfig().Build()
- ctx := advisor.Context{
- Logger: logger,
- Charset: "",
- Collation: "",
- }
- for _, tc := range tests {
- adviceList, err := adv.Check(ctx, tc.statement)
- require.NoError(t, err)
- assert.Equal(t, tc.want, adviceList)
- }
-}
-
func TestBasic(t *testing.T) {
tests := []test{
{
@@ -107,7 +86,11 @@ func TestBasic(t *testing.T) {
},
}
- runTests(t, tests, &CompatibilityAdvisor{})
+ runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &api.SchemaReviewRule{
+ Type: api.SchemaRuleSchemaBackwardCompatibility,
+ Level: api.SchemaRuleLevelWarning,
+ Payload: "",
+ }, &MockCatalogService{})
}
func TestAlterTable(t *testing.T) {
@@ -119,7 +102,7 @@ func TestAlterTable(t *testing.T) {
Status: advisor.Success,
Code: common.Ok,
Title: "OK",
- Content: "Migration is backward compatible",
+ Content: "",
},
},
},
@@ -217,7 +200,7 @@ func TestAlterTable(t *testing.T) {
{
Status: advisor.Success,
Title: "OK",
- Content: "Migration is backward compatible",
+ Content: "",
},
},
},
@@ -238,7 +221,7 @@ func TestAlterTable(t *testing.T) {
{
Status: advisor.Success,
Title: "OK",
- Content: "Migration is backward compatible",
+ Content: "",
},
},
},
@@ -260,7 +243,7 @@ func TestAlterTable(t *testing.T) {
Status: advisor.Success,
Code: common.Ok,
Title: "OK",
- Content: "Migration is backward compatible",
+ Content: "",
},
},
},
@@ -277,7 +260,11 @@ func TestAlterTable(t *testing.T) {
},
}
- runTests(t, tests, &CompatibilityAdvisor{})
+ runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &api.SchemaReviewRule{
+ Type: api.SchemaRuleSchemaBackwardCompatibility,
+ Level: api.SchemaRuleLevelWarning,
+ Payload: "",
+ }, &MockCatalogService{})
}
func TestAlterTableChangeColumnType(t *testing.T) {
@@ -339,5 +326,9 @@ func TestAlterTableChangeColumnType(t *testing.T) {
},
}
- runTests(t, tests, &CompatibilityAdvisor{})
+ runSchemaReviewRuleTests(t, tests, &CompatibilityAdvisor{}, &api.SchemaReviewRule{
+ Type: api.SchemaRuleSchemaBackwardCompatibility,
+ Level: api.SchemaRuleLevelWarning,
+ Payload: "",
+ }, &MockCatalogService{})
}
diff --git a/plugin/advisor/mysql/utils_for_test.go b/plugin/advisor/mysql/utils_for_test.go
index edf141e86e..b9c2e32a50 100644
--- a/plugin/advisor/mysql/utils_for_test.go
+++ b/plugin/advisor/mysql/utils_for_test.go
@@ -48,6 +48,11 @@ func (c *MockCatalogService) FindIndex(ctx context.Context, find *catalog.IndexF
return nil, fmt.Errorf("cannot find index for %v", find)
}
+type test struct {
+ statement string
+ want []advisor.Advice
+}
+
func runSchemaReviewRuleTests(
t *testing.T,
tests []test,
diff --git a/tests/schema_system_test.go b/tests/schema_system_test.go
index 3ec1c10a21..ef76ac6a6b 100644
--- a/tests/schema_system_test.go
+++ b/tests/schema_system_test.go
@@ -204,6 +204,17 @@ func TestSchemaSystem(t *testing.T) {
},
},
},
+ {
+ statement: "DROP TABLE user",
+ result: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusWarn,
+ Code: common.CompatibilityDropTable,
+ Title: "Potential incompatible migration",
+ Content: "\"DROP TABLE user\" may cause incompatibility with the existing data and code",
+ },
+ },
+ },
}
)
diff --git a/tests/tests.go b/tests/tests.go
index 758decd2c6..717f5bf967 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -1348,6 +1348,7 @@ func setDefaultSchemaReviewRulePayload(ruleTp api.SchemaReviewRuleType) (string,
case api.SchemaRuleStatementNoLeadingWildcardLike:
case api.SchemaRuleTableRequirePK:
case api.SchemaRuleColumnNotNull:
+ case api.SchemaRuleSchemaBackwardCompatibility:
case api.SchemaRuleTableNaming:
fallthrough
case api.SchemaRuleColumnNaming:
@@ -1439,6 +1440,10 @@ func prodTemplateSchemaReviewPolicy() (string, error) {
Type: api.SchemaRuleColumnNotNull,
Level: api.SchemaRuleLevelWarning,
},
+ {
+ Type: api.SchemaRuleSchemaBackwardCompatibility,
+ Level: api.SchemaRuleLevelWarning,
+ },
},
}