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: unify bbdataarchive references (#16397)
boojack5502465f6b4b
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 4b08d1653d..81fa96a68b 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -49,11 +49,6 @@ import (v1pb "github.com/bytebase/bytebase/proto/generated-go/v1")-const (- backupDatabaseName = "bbdataarchive"- oracleBackupDatabaseName = "BBDATAARCHIVE"-)-// SQLService is the service for SQL.type SQLService struct {v1pb.UnimplementedSQLServiceServer@@ -302,7 +297,7 @@ func extractSourceTable(comment string) (string, string, string, error) {func getSchemaMetadata(engine storepb.Engine, dbSchema *model.DatabaseSchema) *model.SchemaMetadata {switch engine {case storepb.Engine_POSTGRES:- return dbSchema.GetDatabaseMetadata().GetSchema(backupDatabaseName)+ return dbSchema.GetDatabaseMetadata().GetSchema(base.BackupDatabaseNameOfEngine(storepb.Engine_POSTGRES))case storepb.Engine_MSSQL:return dbSchema.GetDatabaseMetadata().GetSchema("dbo")default:@@ -316,11 +311,11 @@ func replaceBackupTableWithSource(ctx context.Context, stores *store.Store, inst// Don't need to check the database name for postgres here.// We backup the table to the same database with bbdataarchive schema for Postgres.case storepb.Engine_ORACLE:- if database.DatabaseName != oracleBackupDatabaseName {+ if database.DatabaseName != base.BackupDatabaseNameOfEngine(storepb.Engine_ORACLE) {return nil}default:- if database.DatabaseName != backupDatabaseName {+ if database.DatabaseName != base.BackupDatabaseNameOfEngine(instance.Metadata.GetEngine()) {return nil}}@@ -390,11 +385,11 @@ func generateNewColumn(engine storepb.Engine, column parserbase.ColumnResource,func isBackupTable(engine storepb.Engine, column parserbase.ColumnResource) bool {switch engine {case storepb.Engine_POSTGRES:- return column.Schema == backupDatabaseName+ return column.Schema == base.BackupDatabaseNameOfEngine(storepb.Engine_POSTGRES)case storepb.Engine_ORACLE:- return column.Database == oracleBackupDatabaseName+ return column.Database == base.BackupDatabaseNameOfEngine(storepb.Engine_ORACLE)default:- return column.Database == backupDatabaseName+ return column.Database == base.BackupDatabaseNameOfEngine(engine)}}diff --git a/backend/base/database.go b/backend/base/database.gonew file mode 100644index 0000000000..93afd3c27d--- /dev/null+++ b/backend/base/database.go@@ -0,0 +1,48 @@+package base++import storepb "github.com/bytebase/bytebase/proto/generated-go/store"++func BackupDatabaseNameOfEngine(e storepb.Engine) string {+ //exhaustive:enforce+ switch e {+ case+ storepb.Engine_MYSQL,+ storepb.Engine_TIDB,+ storepb.Engine_MSSQL,+ storepb.Engine_POSTGRES:+ return "bbdataarchive"+ case+ storepb.Engine_ORACLE:+ return "BBDATAARCHIVE"+ case+ storepb.Engine_ENGINE_UNSPECIFIED,+ storepb.Engine_SNOWFLAKE,+ storepb.Engine_CASSANDRA,+ storepb.Engine_SQLITE,+ storepb.Engine_MONGODB,+ storepb.Engine_REDIS,+ storepb.Engine_CLICKHOUSE,+ storepb.Engine_SPANNER,+ storepb.Engine_BIGQUERY,+ storepb.Engine_REDSHIFT,+ storepb.Engine_MARIADB,+ storepb.Engine_OCEANBASE,+ storepb.Engine_OCEANBASE_ORACLE,+ storepb.Engine_DM,+ storepb.Engine_STARROCKS,+ storepb.Engine_RISINGWAVE,+ storepb.Engine_HIVE,+ storepb.Engine_COCKROACHDB,+ storepb.Engine_DORIS,+ storepb.Engine_DYNAMODB,+ storepb.Engine_ELASTICSEARCH,+ storepb.Engine_DATABRICKS,+ storepb.Engine_COSMOSDB,+ storepb.Engine_TRINO:+ // Fallback to the default name for other engines.+ return "bbdataarchive"+ default:+ // Fallback to the default name for other engines.+ return "bbdataarchive"+ }+}diff --git a/backend/component/ghost/config.go b/backend/component/ghost/config.goindex 4934dea300..0c97090d4b 100644--- a/backend/component/ghost/config.go+++ b/backend/component/ghost/config.go@@ -5,14 +5,14 @@ import ("strconv""strings"- "github.com/github/gh-ost/go/base"+ ghostbase "github.com/github/gh-ost/go/base"ghostsql "github.com/github/gh-ost/go/sql""github.com/pkg/errors"+ "github.com/bytebase/bytebase/backend/base"secretcomp "github.com/bytebase/bytebase/backend/component/secret"- storepb "github.com/bytebase/bytebase/proto/generated-go/store"-"github.com/bytebase/bytebase/backend/store"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")var defaultConfig = struct {@@ -98,7 +98,7 @@ func GetUserFlags(flags map[string]string) (*UserFlags, error) {}if v, ok := flags["max-load"]; ok {- if _, err := base.ParseLoadMap(v); err != nil {+ if _, err := ghostbase.ParseLoadMap(v); err != nil {return nil, errors.Wrapf(err, "failed to parse max-load %q", v)}f.maxLoad = &v@@ -201,13 +201,13 @@ func GetUserFlags(flags map[string]string) (*UserFlags, error) {}// NewMigrationContext is the context for gh-ost migration.-func NewMigrationContext(ctx context.Context, taskID int, database *store.DatabaseMessage, dataSource *storepb.DataSource, tableName string, tmpTableNameSuffix string, statement string, noop bool, flags map[string]string, serverIDOffset uint) (*base.MigrationContext, error) {+func NewMigrationContext(ctx context.Context, taskID int, database *store.DatabaseMessage, dataSource *storepb.DataSource, tableName string, tmpTableNameSuffix string, statement string, noop bool, flags map[string]string, serverIDOffset uint) (*ghostbase.MigrationContext, error) {password, err := secretcomp.ReplaceExternalSecret(ctx, dataSource.GetPassword(), dataSource.GetExternalSecret())if err != nil {return nil, err}- migrationContext := base.NewMigrationContext()+ migrationContext := ghostbase.NewMigrationContext()migrationContext.Log = newGhostLogger()migrationContext.InspectorConnectionConfig.Key.Hostname = dataSource.GetHost()port := 3306@@ -232,7 +232,8 @@ func NewMigrationContext(ctx context.Context, taskID int, database *store.DatabamigrationContext.CliUser = dataSource.GetUsername()migrationContext.CliPassword = password// GhostDatabaseName is our homemade parameter to allow creating temporary tables under another database.- migrationContext.GhostDatabaseName = "bbdataarchive"+ // Use MySQL/TiDB backup database name for gh-ost+ migrationContext.GhostDatabaseName = base.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)migrationContext.DatabaseName = database.DatabaseNamemigrationContext.OriginalTableName = tableNamemigrationContext.AlterStatement = strings.Join(strings.Fields(statement), " ")@@ -249,7 +250,7 @@ func NewMigrationContext(ctx context.Context, taskID int, database *store.DatabamigrationContext.AllowedRunningOnMaster = defaultConfig.allowedRunningOnMastermigrationContext.ConcurrentCountTableRows = defaultConfig.concurrentCountTableRowsmigrationContext.HooksStatusIntervalSec = defaultConfig.hooksStatusIntervalSec- migrationContext.CutOverType = base.CutOverAtomic+ migrationContext.CutOverType = ghostbase.CutOverAtomicmigrationContext.ThrottleHTTPIntervalMillis = defaultConfig.throttleHTTPIntervalMillismigrationContext.ThrottleHTTPTimeoutMillis = defaultConfig.throttleHTTPTimeoutMillisdiff --git a/backend/plugin/advisor/mysql/advisor_online_migration.go b/backend/plugin/advisor/mysql/advisor_online_migration.goindex 9e8d4b027d..cfdf05d698 100644--- a/backend/plugin/advisor/mysql/advisor_online_migration.go+++ b/backend/plugin/advisor/mysql/advisor_online_migration.go@@ -11,20 +11,15 @@ import (mysql "github.com/bytebase/mysql-parser"+ "github.com/bytebase/bytebase/backend/base""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/parser/base"+ parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql""github.com/bytebase/bytebase/backend/store/model"storepb "github.com/bytebase/bytebase/proto/generated-go/store")-const (- // The database name to save temporary data for gh-ost.- // Use backup database name for now.- ghostDatabaseName = "bbdataarchive"-)-var (_ advisor.Advisor = (*OnlineMigrationAdvisor)(nil))@@ -60,6 +55,7 @@ func (*OnlineMigrationAdvisor) Check(ctx context.Context, checkCtx advisor.Conte// Check gh-ost database existence first if the change type is gh-ost.if checkCtx.ChangeType == storepb.PlanCheckRunConfig_DDL_GHOST {+ ghostDatabaseName := base.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)if !advisor.DatabaseExists(ctx, checkCtx, ghostDatabaseName) {return []*storepb.Advice{{@@ -78,7 +74,7 @@ func (*OnlineMigrationAdvisor) Check(ctx context.Context, checkCtx advisor.Contefor _, stmt := range stmtList {checker := &useGhostChecker{currentDatabase: checkCtx.CurrentDatabase,- changedResources: make(map[string]base.SchemaResource),+ changedResources: make(map[string]parserbase.SchemaResource),baseline: int32(stmt.BaseLine),checkCtx: checkCtx,}@@ -153,7 +149,7 @@ type useGhostChecker struct {checkCtx advisor.ContextcurrentDatabase string- changedResources map[string]base.SchemaResource+ changedResources map[string]parserbase.SchemaResourceghostCompatible boolbaseline int32@@ -185,7 +181,7 @@ func (c *useGhostChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {return}- resource := base.SchemaResource{+ resource := parserbase.SchemaResource{Database: c.currentDatabase,}db, table := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex 43ec986a98..91e212240f 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -117,7 +117,7 @@ var (},},{- Name: "bbdataarchive",+ Name: "bbdataarchive", // MySQL backup database for testing},},}@@ -231,7 +231,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.EDBSchema: schemaMetadata,ChangeType: tc.ChangeType,PreUpdateBackupDetail: &storepb.PreUpdateBackupDetail{- Database: "instances/instanceName/databases/bbdataarchive",+ Database: "instances/instanceName/databases/bbdataarchive", // MySQL backup database for testing},NoAppendBuiltin: true,UsePostgresDatabaseOwner: true,diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.goindex 5b3edb3598..0022953e28 100644--- a/backend/plugin/schema/pg/get_database_definition.go+++ b/backend/plugin/schema/pg/get_database_definition.go@@ -6,7 +6,8 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/base"+ parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base""github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg""github.com/bytebase/bytebase/backend/plugin/schema"@@ -29,7 +30,6 @@ SET row_security = off;`setDefaultTableSpace = "SET default_tablespace = '';\n\n"- backupSchemaName = "bbdataarchive")func init() {@@ -87,7 +87,7 @@ func GetDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *storepb.Da}// Build the graph for topological sort.- graph := base.NewGraph()+ graph := parserbase.NewGraph()functionMap := make(map[string]*storepb.FunctionMetadata)tableMap := make(map[string]*storepb.TableMetadata)viewMap := make(map[string]*storepb.ViewMetadata)@@ -272,7 +272,7 @@ func GetSchemaDefinition(schema *storepb.SchemaMetadata) (string, error) {}// Build the graph for topological sort.- graph := base.NewGraph()+ graph := parserbase.NewGraph()functionMap := make(map[string]*storepb.FunctionMetadata)tableMap := make(map[string]*storepb.TableMetadata)viewMap := make(map[string]*storepb.ViewMetadata)@@ -512,7 +512,7 @@ func filterBackupSchemaIfNecessary(ctx schema.GetDefinitionContext, metadata *stExtensions: metadata.Extensions,}for _, schema := range metadata.Schemas {- if schema.Name == backupSchemaName {+ if schema.Name == base.BackupDatabaseNameOfEngine(storepb.Engine_POSTGRES) {continue}filtered.Schemas = append(filtered.Schemas, schema)diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.goindex aed7cedd0f..16c46c24a5 100644--- a/backend/runner/plancheck/ghost_sync_executor.go+++ b/backend/runner/plancheck/ghost_sync_executor.go@@ -12,6 +12,7 @@ import (gomysql "github.com/go-sql-driver/mysql""github.com/pkg/errors"+ "github.com/bytebase/bytebase/backend/base""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/dbfactory"@@ -128,10 +129,12 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, config *storepb.PlanCheckRu}defer driver.Close(ctx)+ // Use the backup database name of MySQL as the ghost database name.+ ghostDBName := base.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)sql := fmt.Sprintf("DROP TABLE IF EXISTS `%s`.`%s`; DROP TABLE IF EXISTS `%s`.`%s`;",- "bbdataarchive",+ ghostDBName,migrationContext.GetGhostTableName(),- "bbdataarchive",+ ghostDBName,migrationContext.GetChangelogTableName(),)diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex edb8d19117..0b29b9df82 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -36,9 +36,6 @@ const (// defaultSyncInterval means never sync.defaultSyncInterval = 0 * time.SecondMaximumOutstanding = 100-- backupDatabaseName = "bbdataarchive"- oracleBackupDatabaseName = "BBDATAARCHIVE")// NewSyncer creates a schema syncer.@@ -598,12 +595,12 @@ func (s *Syncer) databaseBackupAvailable(ctx context.Context, instance *store.Inreturn false}for _, schema := range dbSchema.Schemas {- if schema.GetName() == backupDatabaseName {+ if schema.GetName() == base.BackupDatabaseNameOfEngine(storepb.Engine_POSTGRES) {return true}}case storepb.Engine_MYSQL, storepb.Engine_MSSQL, storepb.Engine_TIDB:- dbName := backupDatabaseName+ dbName := base.BackupDatabaseNameOfEngine(instance.Metadata.GetEngine())backupDB, err := s.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{InstanceID: &instance.ResourceID,DatabaseName: &dbName,@@ -614,7 +611,7 @@ func (s *Syncer) databaseBackupAvailable(ctx context.Context, instance *store.In}return backupDB != nilcase storepb.Engine_ORACLE:- dbName := oracleBackupDatabaseName+ dbName := base.BackupDatabaseNameOfEngine(storepb.Engine_ORACLE)backupDB, err := s.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{InstanceID: &instance.ResourceID,DatabaseName: &dbName,diff --git a/backend/runner/taskrun/schema_update_ghost_executor.go b/backend/runner/taskrun/schema_update_ghost_executor.goindex fb33ed0e54..8e5c45f94d 100644--- a/backend/runner/taskrun/schema_update_ghost_executor.go+++ b/backend/runner/taskrun/schema_update_ghost_executor.go@@ -11,6 +11,7 @@ import (gomysql "github.com/go-sql-driver/mysql""github.com/pkg/errors"+ "github.com/bytebase/bytebase/backend/base""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/component/config"@@ -110,10 +111,12 @@ func (exec *SchemaUpdateGhostExecutor) RunOnce(ctx context.Context, driverCtx co}defer driver.Close(ctx)+ // Use the backup database name of MySQL as the ghost database name.+ ghostDBName := base.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)sql := fmt.Sprintf("DROP TABLE IF EXISTS `%s`.`%s`; DROP TABLE IF EXISTS `%s`.`%s`;",- "bbdataarchive",+ ghostDBName,migrationContext.GetGhostTableName(),- "bbdataarchive",+ ghostDBName,migrationContext.GetChangelogTableName(),)diff --git a/backend/tests/ghost_test.go b/backend/tests/ghost_test.goindex fcfaa367be..fb44b541c3 100644--- a/backend/tests/ghost_test.go+++ b/backend/tests/ghost_test.go@@ -6,6 +6,8 @@ import ("fmt""testing"+ "github.com/bytebase/bytebase/backend/base"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"_ "github.com/go-sql-driver/mysql"@@ -74,7 +76,9 @@ func TestGhostSchemaUpdate(t *testing.T) {})a.NoError(err)- err = ctl.createDatabaseV2(ctx, ctl.project, instance, nil /* environment */, "bbdataarchive", "")+ // Create backup database for MySQL.+ backupDBName := base.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)+ err = ctl.createDatabaseV2(ctx, ctl.project, instance, nil /* environment */, backupDBName, "")a.NoError(err)err = ctl.createDatabaseV2(ctx, ctl.project, instance, nil /* environment */, databaseName, "")