Skip to content

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

Add schema_prev to migration schema

天舟715b87067291
diff --git a/api/instance.go b/api/instance.go
index a6d6f28344..558338e29c 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -130,6 +130,7 @@ type MigrationHistory struct {
Description string `jsonapi:"attr,description"`
Statement string `jsonapi:"attr,statement"`
Schema string `jsonapi:"attr,schema"`
+ SchemaPrev string `jsonapi:"attr,schemaPrev"`
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/plugin/db/driver.go b/plugin/db/driver.go
index 1da8cf04db..bb3d940f48 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -263,6 +263,7 @@ type MigrationHistory struct {
Description string
Statement string
Schema string
+ SchemaPrev string
ExecutionDuration int
IssueId string
Payload string
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index fd05d3267b..9152d9b438 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -507,11 +507,12 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
description,
statement,
` + "`schema`," + `
+ schema_prev,
execution_duration,
issue_id,
payload
)
- VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)
+ VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, ?, 0, ?, ?)
`
updateHistoryQuery := `
UPDATE
@@ -547,6 +548,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
description,
statement,
` + "`schema`," + `
+ schema_prev,
execution_duration,
issue_id,
payload
diff --git a/plugin/db/mysql/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sql
index 716fe78845..2184dbbc9a 100644
--- a/plugin/db/mysql/mysql_migration_schema.sql
+++ b/plugin/db/mysql/mysql_migration_schema.sql
@@ -63,6 +63,9 @@ CREATE TABLE bytebase.migration_history (
statement TEXT NOT NULL,
-- Record the schema after migration
`schema` TEXT NOT NULL,
+ -- Record the schema before migration. Though we could also fetch it from the previous migration history, it would complicate fetching logic.
+ -- Besides, by storing the schema_prev, we can perform consistency check to see if the migration history has any gaps.
+ schema_prev TEXT NOT NULL,
execution_duration INTEGER NOT NULL,
issue_id TEXT NOT NULL,
payload TEXT NOT NULL
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 58de8cc892..c53bafc8ee 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -574,11 +574,12 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
description,
statement,
` + `"schema",` + `
+ schema_prev,
execution_duration,
issue_id,
payload
)
- VALUES ($1, EXTRACT(epoch from NOW()), $2, EXTRACT(epoch from NOW()), $3, $4, $5, $6, 'PENDING', $7, $8, $9, $10, 0, $11, $12)
+ VALUES ($1, EXTRACT(epoch from NOW()), $2, EXTRACT(epoch from NOW()), $3, $4, $5, $6, 'PENDING', $7, $8, $9, $10, $11, 0, $12, $13)
RETURNING id
`
updateHistoryQuery := `
@@ -616,6 +617,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
description,
statement,
` + `"schema",` + `
+ schema_prev,
execution_duration,
issue_id,
payload
diff --git a/plugin/db/pg/pg_migration_schema.sql b/plugin/db/pg/pg_migration_schema.sql
index ba39a9cf0f..eb169bef54 100644
--- a/plugin/db/pg/pg_migration_schema.sql
+++ b/plugin/db/pg/pg_migration_schema.sql
@@ -63,6 +63,9 @@ CREATE TABLE migration_history (
statement TEXT NOT NULL,
-- Record the schema after migration
schema TEXT NOT NULL,
+ -- Record the schema before migration. Though we could also fetch it from the previous migration history, it would complicate fetching logic.
+ -- Besides, by storing the schema_prev, we can perform consistency check to see if the migration history has any gaps.
+ schema_prev TEXT NOT NULL,
execution_duration INTEGER NOT NULL,
issue_id TEXT NOT NULL,
payload TEXT NOT NULL
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index cdc3ad9652..1914a5c00d 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -46,10 +46,10 @@ type MigrationExecutionArgs struct {
// ExecuteMigration will execute the database migration.
// 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
+ var prevSchemaBuf 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 {
+ if err := driver.Dump(ctx, m.Database, &prevSchemaBuf, true /*schemaOnly*/); err != nil {
return -1, "", formatError(err)
}
}
@@ -119,7 +119,8 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
m.Version,
m.Description,
statement,
- schemaBuf.String(),
+ prevSchemaBuf.String(),
+ prevSchemaBuf.String(),
m.IssueId,
m.Payload,
).Scan(&insertedId)
@@ -134,7 +135,8 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
m.Version,
m.Description,
statement,
- schemaBuf.String(),
+ prevSchemaBuf.String(),
+ prevSchemaBuf.String(),
m.IssueId,
m.Payload,
)
@@ -377,6 +379,7 @@ func FindMigrationHistoryList(ctx context.Context, dbType db.Type, driver db.Dri
&history.Description,
&history.Statement,
&history.Schema,
+ &history.SchemaPrev,
&history.ExecutionDuration,
&history.IssueId,
&history.Payload,
diff --git a/server/instance.go b/server/instance.go
index fe3188412a..f037e7d574 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -323,6 +323,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
Description: entry.Description,
Statement: entry.Statement,
Schema: entry.Schema,
+ SchemaPrev: entry.SchemaPrev,
ExecutionDuration: entry.ExecutionDuration,
IssueId: entry.IssueId,
Payload: entry.Payload,
@@ -389,6 +390,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
Description: entry.Description,
Statement: entry.Statement,
Schema: entry.Schema,
+ SchemaPrev: entry.SchemaPrev,
ExecutionDuration: entry.ExecutionDuration,
IssueId: entry.IssueId,
Payload: entry.Payload,