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: cleanup create database parameter (#5452)
Danny Xuef89265359fa
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 257b25deac..4fff400336 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -177,8 +177,7 @@ type MigrationInfo struct {Creator stringIssueID string// Payload contains JSON-encoded string of VCS push event if the migration is triggered by a VCS push event.- Payload string- CreateDatabase bool+ Payload string// UseSemanticVersion is whether version is a semantic version.// When UseSemanticVersion is set, version should be set to the format specified in Semantic Versioning 2.0.0 (https://semver.org/).// For example, for setting non-semantic version "hello", the values should be Version = "hello", UseSemanticVersion = false, SemanticVersionSuffix = "".diff --git a/backend/plugin/db/oracle/migrate.go b/backend/plugin/db/oracle/migrate.goindex d1976efb23..0af75fc428 100644--- a/backend/plugin/db/oracle/migrate.go+++ b/backend/plugin/db/oracle/migrate.go@@ -3,22 +3,15 @@ package oracleimport ("context""database/sql"- "errors"-- "github.com/bytebase/bytebase/backend/plugin/db"- "github.com/bytebase/bytebase/backend/plugin/db/util")// ExecuteMigrationWithBeforeCommitTxFunc executes the migration, `beforeCommitTxFunc` will be called before transaction commit and after executing `statement`.//// Callers can use `beforeCommitTx` to do some extra work before transaction commit, like get the transaction id.// Any error returned by `beforeCommitTx` will rollback the transaction, so it is the callers' responsibility to return nil if the error occurs in `beforeCommitTx` is not fatal.-func (d *Driver) ExecuteMigrationWithBeforeCommitTxFunc(ctx context.Context, m *db.MigrationInfo, statement string, beforeCommitTxFunc func(tx *sql.Tx) error) (migrationHistoryID string, updatedSchema string, resErr error) {- if m.CreateDatabase {- return "", "", errors.New("creating databases is not supported")- }- if _, err := d.executeWithBeforeCommitTxFunc(ctx, statement, m.CreateDatabase, beforeCommitTxFunc); err != nil {- return "", "", util.FormatError(err)+func (d *Driver) ExecuteMigrationWithBeforeCommitTxFunc(ctx context.Context, statement string, beforeCommitTxFunc func(tx *sql.Tx) error) (migrationHistoryID string, updatedSchema string, resErr error) {+ if _, err := d.executeWithBeforeCommitTxFunc(ctx, statement, beforeCommitTxFunc); err != nil {+ return "", "", err}return "", "", nil}diff --git a/backend/plugin/db/oracle/oracle.go b/backend/plugin/db/oracle/oracle.goindex fd8d0ed66b..a2afa9c3c0 100644--- a/backend/plugin/db/oracle/oracle.go+++ b/backend/plugin/db/oracle/oracle.go@@ -77,8 +77,8 @@ func (driver *Driver) GetDB() *sql.DB {}// Execute executes a SQL statement and returns the affected rows.-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool) (int64, error) {- return driver.executeWithBeforeCommitTxFunc(ctx, statement, createDatabase, nil)+func (driver *Driver) Execute(ctx context.Context, statement string, _ bool) (int64, error) {+ return driver.executeWithBeforeCommitTxFunc(ctx, statement, nil)}// executeWithBeforeCommitTxFunc executes the SQL statements and returns the effected rows, `beforeCommitTx` will be called before transaction commit and after executing `statement`.@@ -86,7 +86,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, createDatab// Callers can use `beforeCommitTx` to do some extra work before transaction commit, like get the transaction id.//// Any error returned by `beforeCommitTx` will rollback the transaction, so it is the callers' responsibility to return nil if the error occurs in `beforeCommitTx` is not fatal.-func (driver *Driver) executeWithBeforeCommitTxFunc(ctx context.Context, statement string, _ bool, beforeCommitTx func(tx *sql.Tx) error) (int64, error) {+func (driver *Driver) executeWithBeforeCommitTxFunc(ctx context.Context, statement string, beforeCommitTx func(tx *sql.Tx) error) (int64, error) {conn, err := driver.db.Conn(ctx)if err != nil {return 0, errors.Wrapf(err, "failed to get connection")diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.goindex 6ef3138e42..147a09683d 100644--- a/backend/plugin/db/util/driverutil.go+++ b/backend/plugin/db/util/driverutil.go@@ -420,21 +420,6 @@ func FindMigrationHistoryList(ctx context.Context, findMigrationHistoryListQueryreturn migrationHistoryList, nil}-// FormatError formats schema migration errors.-func FormatError(err error) error {- if err == nil {- return nil- }-- if strings.Contains(err.Error(), "bytebase_idx_unique_migration_history_namespace_version") {- return errors.Errorf("version has already been applied")- } else if strings.Contains(err.Error(), "bytebase_idx_unique_migration_history_namespace_sequence") {- return errors.Errorf("concurrent migration")- }-- return err-}-// NonSemanticPrefix is the prefix for non-semantic version.const NonSemanticPrefix = "0000.0000.0000-"diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.goindex 37d2ff792c..6008be6896 100644--- a/backend/runner/taskrun/database_create_executor.go+++ b/backend/runner/taskrun/database_create_executor.go@@ -206,7 +206,6 @@ func (exec *DatabaseCreateExecutor) createInitialSchema(ctx context.Context, envSource: db.UI,Type: db.Migrate,Description: "Create database",- CreateDatabase: true,Force: true,}creator, err := exec.store.GetUserByID(ctx, task.CreatorID)diff --git a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go b/backend/runner/taskrun/schema_update_ghost_cutover_executor.goindex 1337f18a3c..c9fadd87ad 100644--- a/backend/runner/taskrun/schema_update_ghost_cutover_executor.go+++ b/backend/runner/taskrun/schema_update_ghost_cutover_executor.go@@ -22,7 +22,6 @@ import (enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi""github.com/bytebase/bytebase/backend/plugin/db"- "github.com/bytebase/bytebase/backend/plugin/db/util"vcsPlugin "github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/runner/schemasync""github.com/bytebase/bytebase/backend/store"@@ -166,7 +165,7 @@ func cutover(ctx context.Context, stores *store.Store, dbFactory *dbfactory.DBFavar afterSchemaBuf bytes.Bufferif _, err := driver.Dump(ctx, &afterSchemaBuf, true /* schemaOnly */); err != nil {- return "", "", util.FormatError(err)+ return "", "", err}return insertedID, afterSchemaBuf.String(), nildiff --git a/backend/server/issue.go b/backend/server/issue.goindex cc312376d8..5b5e27dda0 100644--- a/backend/server/issue.go+++ b/backend/server/issue.go@@ -23,7 +23,6 @@ import (api "github.com/bytebase/bytebase/backend/legacyapi"metricAPI "github.com/bytebase/bytebase/backend/metric""github.com/bytebase/bytebase/backend/plugin/db"- "github.com/bytebase/bytebase/backend/plugin/db/util""github.com/bytebase/bytebase/backend/plugin/metric""github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/store"@@ -1091,14 +1090,14 @@ func (s *Server) createDatabaseCreateTaskList(ctx context.Context, c api.CreateDreturn nil, err}if c.DatabaseName == "" {- return nil, util.FormatError(common.Errorf(common.Invalid, "Failed to create issue, database name missing"))+ return nil, common.Errorf(common.Invalid, "Failed to create issue, database name missing")}if instance.Engine == db.Snowflake {// Snowflake needs to use upper case of DatabaseName.c.DatabaseName = strings.ToUpper(c.DatabaseName)}if instance.Engine == db.MongoDB && c.TableName == "" {- return nil, util.FormatError(common.Errorf(common.Invalid, "Failed to create issue, collection name missing for MongoDB"))+ return nil, common.Errorf(common.Invalid, "Failed to create issue, collection name missing for MongoDB")}// Validate the labels. Labels are set upon task completion.if _, err := convertDatabaseLabels(c.Labels); err != nil {diff --git a/backend/utils/utils.go b/backend/utils/utils.goindex 31f958cf61..fc719f4c75 100644--- a/backend/utils/utils.go+++ b/backend/utils/utils.go@@ -476,13 +476,11 @@ func passCheck(taskCheckRunList []*store.TaskCheckRunMessage, checkType api.Task// ExecuteMigration executes migration.func ExecuteMigration(ctx context.Context, store *store.Store, driver db.Driver, m *db.MigrationInfo, statement string, executeBeforeCommitTx func(tx *sql.Tx) error) (migrationHistoryID string, updatedSchema string, resErr error) {var prevSchemaBuf bytes.Buffer- // Don't record schema if the database hasn't existed yet or is schemaless (e.g. Mongo).- if !m.CreateDatabase {- // For baseline migration, we also record the live schema to detect the schema drift.- // See https://bytebase.com/blog/what-is-database-schema-drift- if _, err := driver.Dump(ctx, &prevSchemaBuf, true /* schemaOnly */); err != nil {- return "", "", err- }+ // Don't record schema if the database hasn't existed yet or is schemaless, e.g. MongoDB.+ // For baseline migration, we also record the live schema to detect the schema drift.+ // See https://bytebase.com/blog/what-is-database-schema-drift+ if _, err := driver.Dump(ctx, &prevSchemaBuf, true /* schemaOnly */); err != nil {+ return "", "", err}insertedID, err := BeginMigration(ctx, store, m, prevSchemaBuf.String(), statement)@@ -521,11 +519,11 @@ func ExecuteMigration(ctx context.Context, store *store.Store, driver db.Driver,if !ok {return "", "", errors.New("failed to cast driver to oracle driver")}- if _, _, err := oracleDriver.ExecuteMigrationWithBeforeCommitTxFunc(ctx, m, statement, executeBeforeCommitTx); err != nil {+ if _, _, err := oracleDriver.ExecuteMigrationWithBeforeCommitTxFunc(ctx, statement, executeBeforeCommitTx); err != nil {return "", "", err}} else {- if _, err := driver.Execute(ctx, statement, m.CreateDatabase); err != nil {+ if _, err := driver.Execute(ctx, statement, false /* createDatabase */); err != nil {return "", "", err}}