Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Returns the migration history id on ExecuteMigration success
天舟cf761472645d
diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex cee348f3ae..1da8cf04db 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -309,8 +309,8 @@ type Driver interface {// Create or upgrade migration related tablesSetupMigrationIfNeeded(ctx context.Context) error// Execute migration will apply the statement and record the migration history, the schema after migration on success.- // It returns the schema after migration on success.- ExecuteMigration(ctx context.Context, m *MigrationInfo, statement string) (string, error)+ // It returns the migration history id and the schema after migration on success.+ ExecuteMigration(ctx context.Context, m *MigrationInfo, statement string) (int64, string, error)// Find the migration history list and return most recent item first.FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error)diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.goindex f75e2064ed..55ebf5449a 100644--- a/plugin/db/mysql/mysql.go+++ b/plugin/db/mysql/mysql.go@@ -491,7 +491,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {}// ExecuteMigration will execute the migration for MySQL.-func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (string, error) {+func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (int64, string, error) {insertHistoryQuery := `INSERT INTO bytebase.migration_history (created_by,diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.goindex 76b285bfb2..5086cfcd0e 100644--- a/plugin/db/pg/pg.go+++ b/plugin/db/pg/pg.go@@ -551,7 +551,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {return nil}-func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (string, error) {+func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (int64, string, error) {insertHistoryQuery := `INSERT INTO migration_history (created_by,diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.goindex fc0b2bcf69..44f46a063e 100644--- a/plugin/db/util/driverutil.go+++ b/plugin/db/util/driverutil.go@@ -44,22 +44,23 @@ type MigrationExecutionArgs struct {}// ExecuteMigration will execute the database migration.-func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *db.MigrationInfo, statement string, args MigrationExecutionArgs) (string, error) {+// Returns the created migraiton history id and the updated schema on success.+func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *db.MigrationInfo, statement string, args MigrationExecutionArgs) (int64, string, error) {var schemaBuf bytes.Buffer// Don't record schema if the database hasn't exist yet.if !m.CreateDatabase {if err := driver.Dump(ctx, m.Database, &schemaBuf, true /*schemaOnly*/); err != nil {- return "", formatError(err)+ return -1, "", formatError(err)}}sqldb, err := driver.GetDbConnection(ctx, bytebaseDatabase)if err != nil {- return "", err+ return -1, "", err}tx, err := sqldb.BeginTx(ctx, nil)if err != nil {- return "", err+ return -1, "", err}defer tx.Rollback()@@ -67,19 +68,19 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *// Check if the same migration version has alraedy been appliedduplicate, err := checkDuplicateVersion(ctx, dbType, tx, m.Namespace, m.Engine, m.Version, args.TablePrefix)if err != nil {- return "", err+ return -1, "", err}if duplicate {- return "", common.Errorf(common.MigrationAlreadyApplied, fmt.Errorf("database %q has already applied version %s", m.Database, m.Version))+ return -1, "", 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 appliedversion, err := checkOutofOrderVersion(ctx, dbType, tx, m.Namespace, m.Engine, m.Version, args.TablePrefix)if err != nil {- return "", err+ return -1, "", 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))+ return -1, "", 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@@ -87,11 +88,11 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *if m.Engine == db.VCS && m.Type != db.Baseline && m.Type != db.Branch {hasBaseline, err := findBaseline(ctx, dbType, tx, m.Namespace, args.TablePrefix)if err != nil {- return "", err+ return -1, "", err}if !hasBaseline {- return "", common.Errorf(common.MigrationBaselineMissing, fmt.Errorf("%s has not created migration baseline yet", m.Database))+ return -1, "", common.Errorf(common.MigrationBaselineMissing, fmt.Errorf("%s has not created migration baseline yet", m.Database))}}@@ -99,7 +100,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *requireBaseline := m.Engine == db.VCS && m.Type == db.Migratesequence, err := findNextSequence(ctx, dbType, tx, m.Namespace, requireBaseline, args.TablePrefix)if err != nil {- return "", err+ return -1, "", err}// Phase 2 - Record migration history as PENDING@@ -139,17 +140,17 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *)if err != nil {- return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)+ return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)}insertedId, err = res.LastInsertId()if err != nil {- return "", FormatErrorWithQuery(err, args.InsertHistoryQuery)+ return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)}}if err := tx.Commit(); err != nil {- return "", err+ return -1, "", err}// Phase 3 - Executing migration@@ -161,14 +162,14 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *if !m.CreateDatabase {d, err := driver.GetDbConnection(ctx, m.Database)if err != nil {- return "", err+ return -1, "", err}sqldb = d}// 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)+ return -1, "", formatError(err)}}duration := time.Now().Unix() - startedTs@@ -176,17 +177,17 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *// Phase 4 - Dump the schema after migrationvar afterSchemaBuf bytes.Bufferif err = driver.Dump(ctx, m.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {- return "", formatError(err)+ return -1, "", formatError(err)}// Phase 5 - Update the migration history with 'DONE', execution_duration, updated schema.afterSqldb, err := driver.GetDbConnection(ctx, bytebaseDatabase)if err != nil {- return "", err+ return -1, "", err}afterTx, err := afterSqldb.BeginTx(ctx, nil)if err != nil {- return "", err+ return -1, "", err}defer afterTx.Rollback()_, err = afterTx.ExecContext(ctx, args.UpdateHistoryQuery,@@ -196,14 +197,14 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *)if err != nil {- return "", FormatErrorWithQuery(err, args.UpdateHistoryQuery)+ return -1, "", FormatErrorWithQuery(err, args.UpdateHistoryQuery)}if err := afterTx.Commit(); err != nil {- return "", err+ return -1, "", err}- return afterSchemaBuf.String(), nil+ return insertedId, afterSchemaBuf.String(), nil}func findBaseline(ctx context.Context, dbType db.Type, tx *sql.Tx, namespace, tablePrefix string) (bool, error) {