Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add backward compatibility to schema review policy (#1207)
ecmadao4aa94a327542
diff --git a/api/plan.go b/api/plan.goindex c03ca8c99a..c808cedf1f 100644--- a/api/plan.go+++ b/api/plan.go@@ -37,6 +37,7 @@ const (// See https://bytebase.com/docs/features/sql-advisor/backward-compatibility-migration-check//// Currently, we only support MySQL/TiDB thanks to github.com/pingcap/parser+ // TODO(ed): remove this after TaskCheckDatabaseStatementCompatibility is entirely moved into schema review policyFeatureBackwardCompatibility FeatureType = "bb.feature.backward-compatibility"// FeatureSchemaDrift detects if there occurs schema drift.// See https://bytebase.com/docs/features/drift-detectiondiff --git a/api/schema_system.go b/api/schema_system.goindex 62f3458b88..6d1f7c36a3 100644--- a/api/schema_system.go+++ b/api/schema_system.go@@ -50,6 +50,9 @@ const (SchemaRuleRequiredColumn SchemaReviewRuleType = "column.required"// SchemaRuleColumnNotNull enforce the columns cannot have NULL value.SchemaRuleColumnNotNull SchemaReviewRuleType = "column.no-null"++ // SchemaRuleSchemaBackwardCompatibility enforce the MySQL and TiDB support check whether the schema change is backward compatible.+ SchemaRuleSchemaBackwardCompatibility SchemaReviewRuleType = "schema.backward-compatibility")// SchemaReviewPolicy is the policy configuration for schema review.diff --git a/server/server.go b/server/server.goindex d133f1d782..7ad4262e7e 100644--- a/server/server.go+++ b/server/server.go@@ -143,6 +143,7 @@ func NewServer(logger *zap.Logger, storeInstance *store.Store, loggerLevel *zap.statementSimpleExecutor := NewTaskCheckStatementAdvisorSimpleExecutor(logger)taskCheckScheduler.Register(string(api.TaskCheckDatabaseStatementFakeAdvise), statementSimpleExecutor)taskCheckScheduler.Register(string(api.TaskCheckDatabaseStatementSyntax), statementSimpleExecutor)+ // TODO(ed): remove this after TaskCheckDatabaseStatementCompatibility is entirely moved into schema review policytaskCheckScheduler.Register(string(api.TaskCheckDatabaseStatementCompatibility), statementSimpleExecutor)statementCompositeExecutor := NewTaskCheckStatementAdvisorCompositeExecutor(logger)diff --git a/server/task.go b/server/task.goindex e1857f878e..8f29212761 100644--- a/server/task.go+++ b/server/task.go@@ -213,6 +213,12 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {)}}++ if s.feature(api.FeatureSchemaReviewPolicy) {+ if err := s.triggerDatabaseStatementAdviseTask(ctx, *taskPatch.Statement, taskPatched); err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Failed to trigger database statement advise task, err: %w", err)).SetInternal(err)+ }+ }}}@@ -579,3 +585,46 @@ func (s *Server) changeTaskStatusWithPatch(ctx context.Context, task *api.Task,return taskPatched, nil}++func (s *Server) triggerDatabaseStatementAdviseTask(ctx context.Context, statement string, task *api.Task) error {+ policyID, err := s.store.GetSchemaReviewPolicyIDByEnvID(ctx, task.Instance.EnvironmentID)++ if err != nil {+ // It's OK if we failed to find the schema review policy, just emit an error log+ s.l.Error("Failed to found schema review policy id for task",+ zap.Int("task_id", task.ID),+ zap.String("task_name", task.Name),+ zap.Int("environment_id", task.Instance.EnvironmentID),+ zap.Error(err),+ )+ return nil+ }++ payload, err := json.Marshal(api.TaskCheckDatabaseStatementAdvisePayload{+ Statement: statement,+ DbType: task.Database.Instance.Engine,+ Charset: task.Database.CharacterSet,+ Collation: task.Database.Collation,+ PolicyID: policyID,+ })+ if err != nil {+ return fmt.Errorf("failed to marshal statement advise payload: %v, err: %w", task.Name, err)+ }++ if _, err := s.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{+ CreatorID: api.SystemBotID,+ TaskID: task.ID,+ Type: api.TaskCheckDatabaseStatementAdvise,+ Payload: string(payload),+ SkipIfAlreadyTerminated: false,+ }); err != nil {+ // It's OK if we failed to trigger a check, just emit an error log+ s.l.Error("Failed to trigger statement advise task after changing task statement",+ zap.Int("task_id", task.ID),+ zap.String("task_name", task.Name),+ zap.Error(err),+ )+ }++ return nil+}diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.goindex 65c02673a3..34d1a40307 100644--- a/server/task_check_executor_statement_advisor_composite.go+++ b/server/task_check_executor_statement_advisor_composite.go@@ -118,6 +118,11 @@ func getAdvisorTypeByRule(ruleType api.SchemaReviewRuleType, engine db.Type) (adcase db.MySQL, db.TiDB:return advisor.MySQLWhereRequirement, nil}+ case api.SchemaRuleSchemaBackwardCompatibility:+ switch engine {+ case db.MySQL, db.TiDB:+ return advisor.MySQLMigrationCompatibility, nil+ }case api.SchemaRuleTableNaming:switch engine {case db.MySQL, db.TiDB:diff --git a/server/task_check_executor_statement_advisor_simple.go b/server/task_check_executor_statement_advisor_simple.goindex d221979fad..85f38f15be 100644--- a/server/task_check_executor_statement_advisor_simple.go+++ b/server/task_check_executor_statement_advisor_simple.go@@ -32,6 +32,7 @@ func (exec *TaskCheckStatementAdvisorSimpleExecutor) Run(ctx context.Context, secase api.TaskCheckDatabaseStatementSyntax:advisorType = advisor.MySQLSyntaxcase api.TaskCheckDatabaseStatementCompatibility:+ // TODO(ed): remove this after TaskCheckDatabaseStatementCompatibility is entirely moved into schema review policyif !server.feature(api.FeatureBackwardCompatibility) {return nil, common.Errorf(common.NotAuthorized, fmt.Errorf(api.FeatureBackwardCompatibility.AccessErrorMessage()))}diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.goindex 0f88471224..a246352ce9 100644--- a/server/task_check_scheduler.go+++ b/server/task_check_scheduler.go@@ -316,6 +316,7 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *apreturn nil, err}+ // TODO(ed): remove this after TaskCheckDatabaseStatementCompatibility is entirely moved into schema review policyif s.server.feature(api.FeatureBackwardCompatibility) {_, err = s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{CreatorID: creatorID,diff --git a/server/task_scheduler.go b/server/task_scheduler.goindex e498f0f3a3..8a81d1f36e 100644--- a/server/task_scheduler.go+++ b/server/task_scheduler.go@@ -282,6 +282,7 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*return task, nil}+ // TODO(ed): remove this after TaskCheckDatabaseStatementCompatibility is entirely moved into schema review policyif s.server.feature(api.FeatureBackwardCompatibility) {pass, err = s.server.passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementCompatibility)if err != nil {@@ -291,6 +292,16 @@ func (s *TaskScheduler) ScheduleIfNeeded(ctx context.Context, task *api.Task) (*return task, nil}}++ if s.server.feature(api.FeatureSchemaReviewPolicy) {+ pass, err = s.server.passCheck(ctx, s.server, task, api.TaskCheckDatabaseStatementAdvise)+ if err != nil {+ return nil, err+ }+ if !pass {+ return task, nil+ }+ }}}updatedTask, err := s.server.changeTaskStatus(ctx, task, api.TaskRunning, api.SystemBotID)