Skip to content

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

pg: move ExecuteMigration to a util package.

spinningbot430bf6ed7798
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 710b2df7a1..b54e1a70a6 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -3,7 +3,6 @@ package db
import (
"bufio"
"context"
- "database/sql"
"fmt"
"io"
"regexp"
@@ -354,23 +353,3 @@ func Open(dbType Type, driverConfig DriverConfig, connectionConfig ConnectionCon
return driver, nil
}
-
-// FormatErrorWithQuery will format the error with failed query.
-func FormatErrorWithQuery(err error, query string) error {
- return common.Errorf(common.DbExecutionError, fmt.Errorf("failed to execute error: %w\n\nquery:\n%q", err, query))
-}
-
-// NeedsSetupMigrationSchema will return whether it's needed to setup migration schema.
-func NeedsSetupMigrationSchema(ctx context.Context, sqldb *sql.DB, query string) (bool, error) {
- rows, err := sqldb.QueryContext(ctx, query)
- if err != nil {
- return false, FormatErrorWithQuery(err, query)
- }
- defer rows.Close()
-
- if rows.Next() {
- return false, nil
- }
-
- return true, nil
-}
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 2dc937986c..e55bf8b8bc 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -2,17 +2,16 @@ package mysql
import (
"bufio"
- "bytes"
"context"
"database/sql"
_ "embed"
"fmt"
"io"
"strings"
- "time"
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/db/util"
"github.com/go-sql-driver/mysql"
"go.uber.org/zap"
)
@@ -114,7 +113,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
query := "SELECT VERSION()"
versionRow, err := driver.db.QueryContext(ctx, query)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer versionRow.Close()
@@ -147,7 +146,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
userRows, err := driver.db.QueryContext(ctx, query)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer userRows.Close()
@@ -170,7 +169,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
query,
)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer grantRows.Close()
@@ -223,7 +222,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
}
indexRows, err := driver.db.QueryContext(ctx, query)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer indexRows.Close()
@@ -284,7 +283,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
WHERE ` + columnWhere
columnRows, err := driver.db.QueryContext(ctx, query)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer columnRows.Close()
@@ -346,7 +345,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
WHERE ` + tableWhere
tableRows, err := driver.db.QueryContext(ctx, query)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer tableRows.Close()
@@ -403,7 +402,7 @@ func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSch
WHERE ` + where
rows, err := driver.db.QueryContext(ctx, query)
if err != nil {
- return nil, nil, db.FormatErrorWithQuery(err, query)
+ return nil, nil, util.FormatErrorWithQuery(err, query)
}
defer rows.Close()
@@ -453,7 +452,7 @@ func (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'bytebase' AND TABLE_NAME = 'migration_history'
`
- return db.NeedsSetupMigrationSchema(ctx, driver.db, query)
+ return util.NeedsSetupMigrationSchema(ctx, driver.db, query)
}
func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
@@ -476,7 +475,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
zap.String("environment", driver.connectionCtx.EnvironmentName),
zap.String("database", driver.connectionCtx.InstanceName),
)
- return db.FormatErrorWithQuery(err, migrationSchema)
+ return util.FormatErrorWithQuery(err, migrationSchema)
}
driver.l.Info("Successfully created migration schema.",
zap.String("environment", driver.connectionCtx.EnvironmentName),
@@ -487,159 +486,47 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
return nil
}
+// ExecuteMigration will execute the migration for MySQL.
func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (string, error) {
- tx, err := driver.db.BeginTx(ctx, nil)
- if err != nil {
- return "", err
- }
- defer tx.Rollback()
-
- // Phase 1 - Precheck before executing migration
- // Check if the same migration version has alraedy been applied
- duplicate, err := checkDuplicateVersion(ctx, tx, m.Namespace, m.Engine, m.Version)
- if err != nil {
- return "", err
- }
- if duplicate {
- return "", common.Errorf(common.MigrationAlreadyApplied, fmt.Errorf("database %q has already applied version %s", m.Database, m.Version))
- }
-
- // Check if there is any higher version already been applied
- version, err := checkOutofOrderVersion(ctx, tx, m.Namespace, m.Engine, m.Version)
- if err != nil {
- return "", err
- }
- if version != nil {
- return "", common.Errorf(common.MigrationOutOfOrder, fmt.Errorf("database %q has already applied version %s which is higher than %s", m.Database, *version, m.Version))
- }
-
- // If the migration engine is VCS and type is not baseline and is not branch, then we can only proceed if there is existing baseline
- // This check is also wrapped in transaction to avoid edge case where two baselinings are running concurrently.
- if m.Engine == db.VCS && m.Type != db.Baseline && m.Type != db.Branch {
- hasBaseline, err := findBaseline(ctx, tx, m.Namespace)
- if err != nil {
- return "", err
- }
-
- if !hasBaseline {
- return "", common.Errorf(common.MigrationBaselineMissing, fmt.Errorf("%s has not created migration baseline yet", m.Database))
- }
- }
-
- // VCS based SQL migration requires existing baselining
- requireBaseline := m.Engine == db.VCS && m.Type == db.Migrate
- sequence, err := findNextSequence(ctx, tx, m.Namespace, requireBaseline)
- if err != nil {
- return "", err
- }
-
- // Phase 2 - Record migration history as PENDING
- // MySQL runs DDL in its own transaction, so we can't commit migration history together with DDL in a single transaction.
- // Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then
- // update the record to DONE together with the updated schema.
- var schemaBuf bytes.Buffer
- err = dumpTxn(ctx, tx, m.Database, &schemaBuf, true /*schemaOnly*/)
- if err != nil {
- return "", formatError(err)
- }
- query := `
- INSERT INTO bytebase.migration_history (
- created_by,
- created_ts,
- updated_by,
- updated_ts,
- namespace,
- sequence,
- ` + "`engine`," + `
- ` + "`type`," + `
- ` + "`status`," + `
- version,
- description,
- statement,
- ` + "`schema`," + `
- execution_duration,
- issue_id,
- payload
- )
- VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)
- `
- res, err := tx.ExecContext(ctx, query,
- m.Creator,
- m.Creator,
- m.Namespace,
+ insertHistoryQuery := `
+ INSERT INTO bytebase.migration_history (
+ created_by,
+ created_ts,
+ updated_by,
+ updated_ts,
+ namespace,
sequence,
- m.Engine,
- m.Type,
- m.Version,
- m.Description,
+ ` + "`engine`," + `
+ ` + "`type`," + `
+ ` + "`status`," + `
+ version,
+ description,
statement,
- schemaBuf.String(),
- m.IssueId,
- m.Payload,
+ ` + "`schema`," + `
+ execution_duration,
+ issue_id,
+ payload
)
-
- if err != nil {
- return "", db.FormatErrorWithQuery(err, query)
- }
-
- insertedId, err := res.LastInsertId()
- if err != nil {
- return "", db.FormatErrorWithQuery(err, query)
- }
-
- if err := tx.Commit(); err != nil {
- return "", err
- }
-
- // Phase 3 - Executing migration
- // Branch migration type always has empty sql.
- // Baseline migration type could also has empty sql when the database is newly created.
- startedTs := time.Now().Unix()
- if statement != "" {
- // MySQL executes DDL in its own transaction, so there is no need to supply a transaction.
- _, err = driver.db.ExecContext(ctx, statement)
- if err != nil {
- return "", formatError(err)
- }
- }
- duration := time.Now().Unix() - startedTs
-
- afterTx, err := driver.db.BeginTx(ctx, nil)
- if err != nil {
- return "", err
- }
- defer afterTx.Rollback()
-
- // Phase 4 - Dump the schema after migration
- var afterSchemaBuf bytes.Buffer
- err = dumpTxn(ctx, afterTx, m.Database, &afterSchemaBuf, true /*schemaOnly*/)
- if err != nil {
- return "", formatError(err)
- }
-
- // Phase 5 - Update the migration history with 'DONE', execution_duration, updated schema.
- query = `
+ VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)
+`
+ updateHistoryQuery := `
UPDATE bytebase.migration_history SET
` + "`status` = 'DONE'," + `
` + "execution_duration = ?," + `
` + "`schema` = ?" + `
WHERE id = ?
`
- _, err = afterTx.ExecContext(ctx, query,
- duration,
- afterSchemaBuf.String(),
- insertedId,
- )
- if err != nil {
- return "", db.FormatErrorWithQuery(err, query)
+ args := util.MigrationExecutionArgs{
+ CheckDuplicateVersion: checkDuplicateVersion,
+ CheckOutofOrderVersion: checkOutofOrderVersion,
+ FindBaseline: findBaseline,
+ FindNextSequence: findNextSequence,
+ DumpTxn: dumpTxn,
+ InsertHistoryQuery: insertHistoryQuery,
+ UpdateHistoryQuery: updateHistoryQuery,
}
-
- if err := afterTx.Commit(); err != nil {
- return "", err
- }
-
- return afterSchemaBuf.String(), nil
+ return util.ExecuteMigration(ctx, driver.db, m, statement, args)
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
@@ -688,7 +575,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
- return nil, db.FormatErrorWithQuery(err, query)
+ return nil, util.FormatErrorWithQuery(err, query)
}
defer rows.Close()
@@ -741,7 +628,7 @@ func findBaseline(ctx context.Context, tx *sql.Tx, namespace string) (bool, erro
)
if err != nil {
- return false, db.FormatErrorWithQuery(err, query)
+ return false, util.FormatErrorWithQuery(err, query)
}
defer row.Close()
@@ -762,7 +649,7 @@ func checkDuplicateVersion(ctx context.Context, tx *sql.Tx, namespace string, en
)
if err != nil {
- return false, db.FormatErrorWithQuery(err, query)
+ return false, util.FormatErrorWithQuery(err, query)
}
defer row.Close()
@@ -782,7 +669,7 @@ func checkOutofOrderVersion(ctx context.Context, tx *sql.Tx, namespace string, e
)
if err != nil {
- return nil, db.FormatErrorWithQuery(err, query)
+ return nil, util.FormatErrorWithQuery(err, query)
}
defer row.Close()
@@ -809,7 +696,7 @@ func findNextSequence(ctx context.Context, tx *sql.Tx, namespace string, require
)
if err != nil {
- return -1, db.FormatErrorWithQuery(err, query)
+ return -1, util.FormatErrorWithQuery(err, query)
}
defer row.Close()
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index dc19e72e20..9fda8d977d 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -11,6 +11,7 @@ import (
"strings"
"github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/plugin/db/util"
_ "github.com/lib/pq"
"go.uber.org/zap"
)
@@ -329,7 +330,7 @@ func (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {
FROM information_schema.tables
WHERE table_name = 'migration_history'
`
- return db.NeedsSetupMigrationSchema(ctx, driver.db, query)
+ return util.NeedsSetupMigrationSchema(ctx, driver.db, query)
}
func (driver *Driver) hasBytebaseDatabase(ctx context.Context) (bool, error) {
@@ -376,7 +377,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
zap.String("environment", driver.connectionCtx.EnvironmentName),
zap.String("database", driver.connectionCtx.InstanceName),
)
- return db.FormatErrorWithQuery(err, createBytebaseDatabaseStmt)
+ return util.FormatErrorWithQuery(err, createBytebaseDatabaseStmt)
}
}
@@ -395,7 +396,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
zap.String("environment", driver.connectionCtx.EnvironmentName),
zap.String("database", driver.connectionCtx.InstanceName),
)
- return db.FormatErrorWithQuery(err, migrationSchema)
+ return util.FormatErrorWithQuery(err, migrationSchema)
}
driver.l.Info("Successfully created migration schema.",
zap.String("environment", driver.connectionCtx.EnvironmentName),
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
new file mode 100644
index 0000000000..8b96a8def3
--- /dev/null
+++ b/plugin/db/util/driverutil.go
@@ -0,0 +1,188 @@
+package util
+
+import (
+ "bytes"
+ "context"
+ "database/sql"
+ "fmt"
+ "io"
+ "strings"
+ "time"
+
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+// FormatErrorWithQuery will format the error with failed query.
+func FormatErrorWithQuery(err error, query string) error {
+ return common.Errorf(common.DbExecutionError, fmt.Errorf("failed to execute error: %w\n\nquery:\n%q", err, query))
+}
+
+// NeedsSetupMigrationSchema will return whether it's needed to setup migration schema.
+func NeedsSetupMigrationSchema(ctx context.Context, sqldb *sql.DB, query string) (bool, error) {
+ rows, err := sqldb.QueryContext(ctx, query)
+ if err != nil {
+ return false, FormatErrorWithQuery(err, query)
+ }
+ defer rows.Close()
+
+ if rows.Next() {
+ return false, nil
+ }
+
+ return true, nil
+}
+
+// MigrationExecutionArgs includes the arguments for ExecuteMigration().
+type MigrationExecutionArgs struct {
+ CheckDuplicateVersion func(ctx context.Context, tx *sql.Tx, namespace string, engine db.MigrationEngine, version string) (bool, error)
+ CheckOutofOrderVersion func(ctx context.Context, tx *sql.Tx, namespace string, engine db.MigrationEngine, version string) (*string, error)
+ FindBaseline func(ctx context.Context, tx *sql.Tx, namespace string) (bool, error)
+ FindNextSequence func(ctx context.Context, tx *sql.Tx, namespace string, requireBaseline bool) (int, error)
+ DumpTxn func(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool) error
+ InsertHistoryQuery string
+ UpdateHistoryQuery string
+}
+
+// ExecuteMigration will execute the database migration.
+func ExecuteMigration(ctx context.Context, sqldb *sql.DB, m *db.MigrationInfo, statement string, args MigrationExecutionArgs) (string, error) {
+ tx, err := sqldb.BeginTx(ctx, nil)
+ if err != nil {
+ return "", err
+ }
+ defer tx.Rollback()
+
+ // Phase 1 - Precheck before executing migration
+ // Check if the same migration version has alraedy been applied
+ duplicate, err := args.CheckDuplicateVersion(ctx, tx, m.Namespace, m.Engine, m.Version)
+ if err != nil {
+ return "", err
+ }
+ if duplicate {
+ return "", common.Errorf(common.MigrationAlreadyApplied, fmt.Errorf("database %q has already applied version %s", m.Database, m.Version))
+ }
+
+ // Check if there is any higher version already been applied
+ version, err := args.CheckOutofOrderVersion(ctx, tx, m.Namespace, m.Engine, m.Version)
+ if err != nil {
+ return "", err
+ }
+ if version != nil {
+ return "", common.Errorf(common.MigrationOutOfOrder, fmt.Errorf("database %q has already applied version %s which is higher than %s", m.Database, *version, m.Version))
+ }
+
+ // If the migration engine is VCS and type is not baseline and is not branch, then we can only proceed if there is existing baseline
+ // This check is also wrapped in transaction to avoid edge case where two baselinings are running concurrently.
+ if m.Engine == db.VCS && m.Type != db.Baseline && m.Type != db.Branch {
+ hasBaseline, err := args.FindBaseline(ctx, tx, m.Namespace)
+ if err != nil {
+ return "", err
+ }
+
+ if !hasBaseline {
+ return "", common.Errorf(common.MigrationBaselineMissing, fmt.Errorf("%s has not created migration baseline yet", m.Database))
+ }
+ }
+
+ // VCS based SQL migration requires existing baselining
+ requireBaseline := m.Engine == db.VCS && m.Type == db.Migrate
+ sequence, err := args.FindNextSequence(ctx, tx, m.Namespace, requireBaseline)
+ if err != nil {
+ return "", err
+ }
+
+ // Phase 2 - Record migration history as PENDING
+ // MySQL runs DDL in its own transaction, so we can't commit migration history together with DDL in a single transaction.
+ // Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then
+ // update the record to DONE together with the updated schema.
+ var schemaBuf bytes.Buffer
+ err = args.DumpTxn(ctx, tx, m.Database, &schemaBuf, true /*schemaOnly*/)
+ if err != nil {
+ return "", formatError(err)
+ }
+ res, err := tx.ExecContext(ctx, args.InsertHistoryQuery,
+ m.Creator,
+ m.Creator,
+ m.Namespace,
+ sequence,
+ m.Engine,
+ m.Type,
+ m.Version,
+ m.Description,
+ statement,
+ schemaBuf.String(),
+ m.IssueId,
+ m.Payload,
+ )
+
+ if err != nil {
+ return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)
+ }
+
+ insertedId, err := res.LastInsertId()
+ if err != nil {
+ return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)
+ }
+
+ if err := tx.Commit(); err != nil {
+ return "", err
+ }
+
+ // Phase 3 - Executing migration
+ // Branch migration type always has empty sql.
+ // Baseline migration type could also has empty sql when the database is newly created.
+ startedTs := time.Now().Unix()
+ if statement != "" {
+ // MySQL executes DDL in its own transaction, so there is no need to supply a transaction.
+ _, err = sqldb.ExecContext(ctx, statement)
+ if err != nil {
+ return "", formatError(err)
+ }
+ }
+ duration := time.Now().Unix() - startedTs
+
+ afterTx, err := sqldb.BeginTx(ctx, nil)
+ if err != nil {
+ return "", err
+ }
+ defer afterTx.Rollback()
+
+ // Phase 4 - Dump the schema after migration
+ var afterSchemaBuf bytes.Buffer
+ err = args.DumpTxn(ctx, afterTx, m.Database, &afterSchemaBuf, true /*schemaOnly*/)
+ if err != nil {
+ return "", formatError(err)
+ }
+
+ // Phase 5 - Update the migration history with 'DONE', execution_duration, updated schema.
+
+ _, err = afterTx.ExecContext(ctx, args.UpdateHistoryQuery,
+ duration,
+ afterSchemaBuf.String(),
+ insertedId,
+ )
+
+ if err != nil {
+ return "", FormatErrorWithQuery(err, args.UpdateHistoryQuery)
+ }
+
+ if err := afterTx.Commit(); err != nil {
+ return "", err
+ }
+
+ return afterSchemaBuf.String(), nil
+}
+
+func formatError(err error) error {
+ if err == nil {
+ return nil
+ }
+
+ if strings.Contains(err.Error(), "bytebase_idx_unique_migration_history_namespace_version") {
+ return fmt.Errorf("version has already been applied")
+ } else if strings.Contains(err.Error(), "bytebase_idx_unique_migration_history_namespace_sequence") {
+ return fmt.Errorf("concurrent migration")
+ }
+
+ return err
+}