Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Add `status` into mysql_migration_schema.
天舟9e1516fb5102
diff --git a/api/instance.go b/api/instance.goindex 2a388a5034..a6d6f28344 100644--- a/api/instance.go+++ b/api/instance.go@@ -125,6 +125,7 @@ type MigrationHistory struct {Database string `jsonapi:"attr,database"`Engine db.MigrationEngine `jsonapi:"attr,engine"`Type db.MigrationType `jsonapi:"attr,type"`+ Status db.MigrationStatus `jsonapi:"attr,status"`Version string `jsonapi:"attr,version"`Description string `jsonapi:"attr,description"`Statement string `jsonapi:"attr,statement"`diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex c496a4a881..b54e1a70a6 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -134,6 +134,23 @@ func (e MigrationType) String() string {return "UNKNOWN"}+type MigrationStatus string++const (+ Pending MigrationStatus = "PENDING"+ Done MigrationStatus = "DONE"+)++func (e MigrationStatus) String() string {+ switch e {+ case Pending:+ return "PENDING"+ case Done:+ return "DONE"+ }+ return "UNKNOWN"+}+type MigrationInfoPayload struct {VCSPushEvent *common.VCSPushEvent `json:"pushEvent,omitempty"`}@@ -145,6 +162,7 @@ type MigrationInfo struct {Environment stringEngine MigrationEngineType MigrationType+ Status MigrationStatusDescription stringCreator stringIssueId string@@ -238,6 +256,7 @@ type MigrationHistory struct {Sequence intEngine MigrationEngineType MigrationType+ Status MigrationStatusVersion stringDescription stringStatement stringdiff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.goindex 83abe5d2c8..1285e57187 100644--- a/plugin/db/mysql/mysql.go+++ b/plugin/db/mysql/mysql.go@@ -503,8 +503,6 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,}defer tx.Rollback()- startedTs := time.Now().Unix()-// Phase 1 - Precheck before executing migration// Check if the same migration version has alraedy been appliedduplicate, err := checkDuplicateVersion(ctx, tx, m.Namespace, m.Engine, m.Version)@@ -544,25 +542,16 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,return "", err}- // Phase 2 - Executing migration- // Branch migration type always has empty sql.- // Baseline migration type could also has empty sql when the database is newly created.- if statement != "" {- _, err = tx.ExecContext(ctx, statement)- if err != nil {- return "", formatError(err)- }- }-- // Phase 3 - Dump the schema after migration+ // 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.Buffererr = dumpTxn(ctx, tx, m.Database, &schemaBuf, true /*schemaOnly*/)if err != nil {return "", formatError(err)}-- // Phase 4 - Record migration- const query = `+ query := `INSERT INTO bytebase.migration_history (created_by,created_ts,@@ -572,6 +561,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,sequence,` + "`engine`," + `` + "`type`," + `+ ` + "`status`," + `version,description,statement,@@ -580,9 +570,9 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,issue_id,payload)- VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)+ VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)`- _, err = tx.ExecContext(ctx, query,+ res, err := tx.ExecContext(ctx, query,m.Creator,m.Creator,m.Namespace,@@ -593,7 +583,6 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,m.Description,statement,schemaBuf.String(),- time.Now().Unix()-startedTs,m.IssueId,m.Payload,)@@ -602,11 +591,64 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,return "", formatErrorWithQuery(err, query)}+ insertedId, err := res.LastInsertId()+ if err != nil {+ return "", formatErrorWithQuery(err, query)+ }+if err := tx.Commit(); err != nil {return "", err}- return schemaBuf.String(), nil+ // 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 = `+ 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 "", formatErrorWithQuery(err, query)+ }++ if err := afterTx.Commit(); err != nil {+ return "", err+ }++ return afterSchemaBuf.String(), nil}func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {@@ -638,6 +680,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Migsequence,` + "`engine`," + `` + "`type`," + `+ ` + "`status`," + `version,description,statement,@@ -672,6 +715,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig&history.Sequence,&history.Engine,&history.Type,+ &history.Status,&history.Version,&history.Description,&history.Statement,diff --git a/plugin/db/mysql/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sqlindex b125be2367..716fe78845 100644--- a/plugin/db/mysql/mysql_migration_schema.sql+++ b/plugin/db/mysql/mysql_migration_schema.sql@@ -54,6 +54,9 @@ CREATE TABLE bytebase.migration_history (-- We call it engine because maybe we could load history from other migration tool.`engine` ENUM('UI', 'VCS') NOT NULL,`type` ENUM('BASELINE', 'MIGRATE', 'BRANCH') NOT NULL,+ -- MySQL runs DDL in its own transaction, so we can't record DDL and migration_history into a single transaction.+ -- Thus, we create a "PENDING" record before applying the DDL and update that record to "DONE" after applying the DDL.+ `status` ENUM('PENDING', 'DONE') NOT NULL,version TEXT NOT NULL,description TEXT NOT NULL,-- Record the migration statementdiff --git a/server/instance.go b/server/instance.goindex e26e77b62d..45b878c3f3 100644--- a/server/instance.go+++ b/server/instance.go@@ -318,6 +318,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {Database: entry.Namespace,Engine: entry.Engine,Type: entry.Type,+ Status: entry.Status,Version: entry.Version,Description: entry.Description,Statement: entry.Statement,@@ -383,6 +384,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {Database: entry.Namespace,Engine: entry.Engine,Type: entry.Type,+ Status: entry.Status,Version: entry.Version,Description: entry.Description,Statement: entry.Statement,