Skip to content

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

chore: cleanup create database parameter (#5452)

Danny Xuef89265359fa
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index 257b25deac..4fff400336 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -177,8 +177,7 @@ type MigrationInfo struct {
Creator string
IssueID 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.go
index d1976efb23..0af75fc428 100644
--- a/backend/plugin/db/oracle/migrate.go
+++ b/backend/plugin/db/oracle/migrate.go
@@ -3,22 +3,15 @@ package oracle
import (
"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.go
index 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.go
index 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, findMigrationHistoryListQuery
return 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.go
index 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, env
Source: 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.go
index 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.DBFa
var afterSchemaBuf bytes.Buffer
if _, err := driver.Dump(ctx, &afterSchemaBuf, true /* schemaOnly */); err != nil {
- return "", "", util.FormatError(err)
+ return "", "", err
}
return insertedID, afterSchemaBuf.String(), nil
diff --git a/backend/server/issue.go b/backend/server/issue.go
index 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.CreateD
return 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.go
index 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
}
}