Skip to content

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

Record the schema snapshot after applying the migration

天舟ad0f57976667
diff --git a/api/instance.go b/api/instance.go
index d60575f2d7..2a388a5034 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -128,6 +128,7 @@ type MigrationHistory struct {
Version string `jsonapi:"attr,version"`
Description string `jsonapi:"attr,description"`
Statement string `jsonapi:"attr,statement"`
+ Schema string `jsonapi:"attr,schema"`
ExecutionDuration int `jsonapi:"attr,executionDuration"`
// This is a string instead of int as the issue id may come from other issue tracking system in the future
IssueId string `jsonapi:"attr,issueId"`
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index 2cf80b9e27..bfc3aaa778 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -85,6 +85,7 @@ export type MigrationHistory = {
version: string;
description: string;
statement: string;
+ schema: string;
executionDuration: number;
issueId: number;
payload?: MigrationHistoryPayload;
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 9f3087ba7a..eff2b18917 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -238,6 +238,7 @@ type MigrationHistory struct {
Version string
Description string
Statement string
+ Schema string
ExecutionDuration int
IssueId string
Payload string
@@ -277,7 +278,7 @@ type Driver interface {
NeedsSetupMigration(ctx context.Context) (bool, error)
// Create or upgrade migration related tables
SetupMigrationIfNeeded(ctx context.Context) error
- // Execute migration will apply the statement and record the migration history on success.
+ // Execute migration will apply the statement and record the migration history, the schema after migration on success.
ExecuteMigration(ctx context.Context, m *MigrationInfo, statement 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.go
index cf71ad6eb5..9b1220247f 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -2,6 +2,7 @@ package mysql
import (
"bufio"
+ "bytes"
"context"
"database/sql"
_ "embed"
@@ -535,7 +536,14 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
}
}
- // Phase 3 - Record migration
+ // Phase 3 - Dump the schema after migration
+ var buf bytes.Buffer
+ err = dumpTxn(ctx, tx, m.Database, &buf, true /*schemaOnly*/)
+ if err != nil {
+ return formatError(err)
+ }
+
+ // Phase 4 - Record migration
const query = `
INSERT INTO bytebase.migration_history (
created_by,
@@ -549,11 +557,12 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
version,
description,
statement,
+ ` + "`schema`," + `
execution_duration,
issue_id,
payload
)
- VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
_, err = tx.ExecContext(ctx, query,
m.Creator,
@@ -565,6 +574,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
m.Version,
m.Description,
statement,
+ buf.String(),
time.Now().Unix()-startedTs,
m.IssueId,
m.Payload,
@@ -607,6 +617,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
version,
description,
statement,
+ ` + "`schema`," + `
execution_duration,
issue_id,
payload
@@ -640,6 +651,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
&history.Version,
&history.Description,
&history.Statement,
+ &history.Schema,
&history.ExecutionDuration,
&history.IssueId,
&history.Payload,
@@ -776,7 +788,7 @@ func formatError(err error) error {
}
func formatErrorWithQuery(err error, query string) error {
- return fmt.Errorf("failed to execute \"%s\"\n\n%w", query, err)
+ return fmt.Errorf("failed to execute error: %w\n\nquery:\n%q", err, query)
}
// Dump and restore
@@ -837,6 +849,70 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
}
defer txn.Rollback()
+ if err := dumpTxn(ctx, txn, database, out, schemaOnly); err != nil {
+ return err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error) {
+ txn, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer txn.Rollback()
+
+ s := ""
+ delimiter := false
+ for sc.Scan() {
+ line := sc.Text()
+
+ execute := false
+ switch {
+ case s == "" && line == "":
+ continue
+ case strings.HasPrefix(line, "--"):
+ continue
+ case line == "DELIMITER ;;":
+ delimiter = true
+ continue
+ case line == "DELIMITER ;" && delimiter:
+ delimiter = false
+ execute = true
+ case strings.HasSuffix(line, ";"):
+ s = s + line + "\n"
+ if !delimiter {
+ execute = true
+ }
+ default:
+ s = s + line + "\n"
+ continue
+ }
+ if execute {
+ _, err := txn.Exec(s)
+ if err != nil {
+ return fmt.Errorf("execute query %q failed: %v", s, err)
+ }
+ s = ""
+ }
+ }
+ if err := sc.Err(); err != nil {
+ return err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool) error {
// Find all dumpable databases
dbNames, err := getDatabases(txn)
if err != nil {
@@ -939,62 +1015,6 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
}
}
- if err := txn.Commit(); err != nil {
- return err
- }
-
- return nil
-}
-
-func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error) {
- txn, err := driver.db.BeginTx(ctx, nil)
- if err != nil {
- return err
- }
- defer txn.Rollback()
-
- s := ""
- delimiter := false
- for sc.Scan() {
- line := sc.Text()
-
- execute := false
- switch {
- case s == "" && line == "":
- continue
- case strings.HasPrefix(line, "--"):
- continue
- case line == "DELIMITER ;;":
- delimiter = true
- continue
- case line == "DELIMITER ;" && delimiter:
- delimiter = false
- execute = true
- case strings.HasSuffix(line, ";"):
- s = s + line + "\n"
- if !delimiter {
- execute = true
- }
- default:
- s = s + line + "\n"
- continue
- }
- if execute {
- _, err := txn.Exec(s)
- if err != nil {
- return fmt.Errorf("execute query %q failed: %v", s, err)
- }
- s = ""
- }
- }
- if err := sc.Err(); err != nil {
- return err
- }
-
- if err := txn.Commit(); err != nil {
- return err
- }
-
return nil
}
diff --git a/plugin/db/mysql/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sql
index 5c8d3315b3..b125be2367 100644
--- a/plugin/db/mysql/mysql_migration_schema.sql
+++ b/plugin/db/mysql/mysql_migration_schema.sql
@@ -56,8 +56,10 @@ CREATE TABLE bytebase.migration_history (
`type` ENUM('BASELINE', 'MIGRATE', 'BRANCH') NOT NULL,
version TEXT NOT NULL,
description TEXT NOT NULL,
- -- Recorded the migration statement
+ -- Record the migration statement
statement TEXT NOT NULL,
+ -- Record the schema after migration
+ `schema` TEXT NOT NULL,
execution_duration INTEGER NOT NULL,
issue_id TEXT NOT NULL,
payload TEXT NOT NULL
diff --git a/server/instance.go b/server/instance.go
index 4e4527008d..160597fd23 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -323,6 +323,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
Version: entry.Version,
Description: entry.Description,
Statement: entry.Statement,
+ Schema: entry.Schema,
ExecutionDuration: entry.ExecutionDuration,
IssueId: entry.IssueId,
Payload: entry.Payload,