Skip to content

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

Add release_version column to migration_history table

天舟6ac108f67e79
diff --git a/api/instance.go b/api/instance.go
index 558338e29c..3a722b8ae1 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -122,6 +122,7 @@ type MigrationHistory struct {
UpdatedTs int64 `jsonapi:"attr,updatedTs"`
// Domain specific fields
+ ReleaseVersion string `jsonapi:"attr,releaseVersion"`
Database string `jsonapi:"attr,database"`
Engine db.MigrationEngine `jsonapi:"attr,engine"`
Type db.MigrationType `jsonapi:"attr,type"`
diff --git a/frontend/src/types/instance.ts b/frontend/src/types/instance.ts
index a2bf19c4de..ac8158207e 100644
--- a/frontend/src/types/instance.ts
+++ b/frontend/src/types/instance.ts
@@ -81,6 +81,7 @@ export type MigrationHistory = {
createdTs: number;
updater: string;
updatedTs: number;
+ releaseVersion: string;
database: string;
engine: MigrationEngine;
type: MigrationType;
diff --git a/frontend/src/views/MigrationHistoryDetail.vue b/frontend/src/views/MigrationHistoryDetail.vue
index 0b6ec9c243..80a4277773 100644
--- a/frontend/src/views/MigrationHistoryDetail.vue
+++ b/frontend/src/views/MigrationHistoryDetail.vue
@@ -52,7 +52,8 @@
<dt class="sr-only">Created</dt>
<dd class="flex items-center text-sm md:mr-4">
<span class="textlabel">Created&nbsp;-&nbsp;</span>
- {{ humanizeTs(migrationHistory.createdTs) }}
+ {{ humanizeTs(migrationHistory.createdTs) }} by
+ {{ `version ${migrationHistory.releaseVersion}` }}
</dd>
</dl>
<div
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index bb3d940f48..a1b15ccb23 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -157,6 +157,7 @@ type MigrationInfoPayload struct {
}
type MigrationInfo struct {
+ ReleaseVersion string
Version string
Namespace string
Database string
@@ -254,6 +255,7 @@ type MigrationHistory struct {
Updater string
UpdatedTs int64
+ ReleaseVersion string
Namespace string
Sequence int
Engine MigrationEngine
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 9152d9b438..6723a42b0a 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -498,6 +498,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
created_ts,
updated_by,
updated_ts,
+ release_version,
namespace,
sequence,
engine,
@@ -512,7 +513,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
issue_id,
payload
)
- VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, ?, 0, ?, ?)
+ VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, ?, 0, ?, ?)
`
updateHistoryQuery := `
UPDATE
@@ -539,6 +540,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
created_ts,
updated_by,
updated_ts,
+ release_version,
namespace,
sequence,
engine,
diff --git a/plugin/db/mysql/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sql
index 2885436142..caecfe0e92 100644
--- a/plugin/db/mysql/mysql_migration_schema.sql
+++ b/plugin/db/mysql/mysql_migration_schema.sql
@@ -11,6 +11,9 @@ CREATE TABLE bytebase.migration_history (
created_ts BIGINT NOT NULL,
updated_by TEXT NOT NULL,
updated_ts BIGINT NOT NULL,
+ -- Record the client version creating this migration history. For Bytebase, we use its binary release version. Different Bytebase release might
+ -- record different history info and thie field helps to handle such situation properly. Moreover, it helps debugging.
+ release_version TEXT NOT NULL,
-- Allows granular tracking of migration history (e.g If an application manages schemas for a multi-tenant service and each tenant has its own schema, that application can use namespace to record the tenant name to track the per-tenant schema migration)
-- Since bytebase also manages different application databases from an instance, it leverages this field to track each database migration history.
namespace TEXT NOT NULL,
@@ -22,6 +25,7 @@ CREATE TABLE bytebase.migration_history (
-- 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,
+ -- Record the migration version.
version TEXT NOT NULL,
description TEXT NOT NULL,
-- Record the migration statement
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index c53bafc8ee..9cfe94ef58 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -565,6 +565,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
created_ts,
updated_by,
updated_ts,
+ release_version,
namespace,
sequence,
engine,
@@ -579,7 +580,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
issue_id,
payload
)
- VALUES ($1, EXTRACT(epoch from NOW()), $2, EXTRACT(epoch from NOW()), $3, $4, $5, $6, 'PENDING', $7, $8, $9, $10, $11, 0, $12, $13)
+ VALUES ($1, EXTRACT(epoch from NOW()), $2, EXTRACT(epoch from NOW()), $3, $4, $5, $6, $7, 'PENDING', $8, $9, $10, $11, $12, 0, $13, $14)
RETURNING id
`
updateHistoryQuery := `
@@ -608,6 +609,7 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
created_ts,
updated_by,
updated_ts,
+ release_version,
namespace,
sequence,
engine,
diff --git a/plugin/db/pg/pg_migration_schema.sql b/plugin/db/pg/pg_migration_schema.sql
index 5a7c4f995b..fb1bb33671 100644
--- a/plugin/db/pg/pg_migration_schema.sql
+++ b/plugin/db/pg/pg_migration_schema.sql
@@ -11,6 +11,9 @@ CREATE TABLE migration_history (
created_ts BIGINT NOT NULL,
updated_by TEXT NOT NULL,
updated_ts BIGINT NOT NULL,
+ -- Record the client version creating this migration history. For Bytebase, we use its binary release version. Different Bytebase release might
+ -- record different history info and thie field helps to handle such situation properly. Moreover, it helps debugging.
+ release_version TEXT NOT NULL,
-- Allows granular tracking of migration history (e.g If an application manages schemas for a multi-tenant service and each tenant has its own schema, that application can use namespace to record the tenant name to track the per-tenant schema migration)
-- Since bytebase also manages different application databases from an instance, it leverages this field to track each database migration history.
namespace TEXT NOT NULL,
@@ -19,9 +22,10 @@ CREATE TABLE migration_history (
-- We call it engine because maybe we could load history from other migration tool.
engine TEXT NOT NULL CHECK (engine in ('UI', 'VCS')),
type TEXT NOT NULL CHECK (type in ('BASELINE', 'MIGRATE', 'BRANCH')),
- -- MySQL runs DDL in its own transaction, so we can't record DDL and migration_history into a single transaction.
+ -- PostgreSQL can't do cross database 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 TEXT NOT NULL CHECK (status in ('PENDING', 'DONE')),
+ -- Record the migration version.
version TEXT NOT NULL,
description TEXT NOT NULL,
-- Record the migration statement
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 1914a5c00d..b846fd2e6c 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -112,6 +112,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
tx.QueryRowContext(ctx, args.InsertHistoryQuery,
m.Creator,
m.Creator,
+ m.ReleaseVersion,
m.Namespace,
sequence,
m.Engine,
@@ -128,6 +129,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
res, err := tx.ExecContext(ctx, args.InsertHistoryQuery,
m.Creator,
m.Creator,
+ m.ReleaseVersion,
m.Namespace,
sequence,
m.Engine,
@@ -370,6 +372,7 @@ func FindMigrationHistoryList(ctx context.Context, dbType db.Type, driver db.Dri
&history.CreatedTs,
&history.Updater,
&history.UpdatedTs,
+ &history.ReleaseVersion,
&history.Namespace,
&history.Sequence,
&history.Engine,
diff --git a/server/instance.go b/server/instance.go
index f037e7d574..0dd5a14812 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -315,6 +315,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
CreatedTs: entry.CreatedTs,
Updater: entry.Updater,
UpdatedTs: entry.UpdatedTs,
+ ReleaseVersion: entry.ReleaseVersion,
Database: entry.Namespace,
Engine: entry.Engine,
Type: entry.Type,
@@ -382,6 +383,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
CreatedTs: entry.CreatedTs,
Updater: entry.Updater,
UpdatedTs: entry.UpdatedTs,
+ ReleaseVersion: entry.ReleaseVersion,
Database: entry.Namespace,
Engine: entry.Engine,
Type: entry.Type,
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index e3f8568e17..2d29b32f3f 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -67,6 +67,7 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
// Create a baseline migration history upon creating the database.
mi := &db.MigrationInfo{
+ ReleaseVersion: server.version,
Version: defaultMigrationVersionFromTaskId(task.ID),
Namespace: payload.DatabaseName,
Database: payload.DatabaseName,
diff --git a/server/task_executor_database_restore.go b/server/task_executor_database_restore.go
index 389fb56e1d..edc05db9e6 100644
--- a/server/task_executor_database_restore.go
+++ b/server/task_executor_database_restore.go
@@ -173,16 +173,17 @@ func createBranchMigrationHistory(ctx context.Context, server *Server, sourceDat
description = fmt.Sprintf("Restored from backup %q of database %q in instance %q.", backup.Name, sourceDatabase.Name, sourceDatabase.Instance.Name)
}
m := &db.MigrationInfo{
- Version: defaultMigrationVersionFromTaskId(task.ID),
- Namespace: targetDatabase.Name,
- Database: targetDatabase.Name,
- Environment: targetDatabase.Instance.Environment.Name,
- Engine: db.MigrationEngine(targetDatabase.Project.WorkflowType),
- Type: db.Branch,
- Description: description,
- Creator: task.Creator.Name,
- IssueId: issueId,
- Payload: "",
+ ReleaseVersion: server.version,
+ Version: defaultMigrationVersionFromTaskId(task.ID),
+ Namespace: targetDatabase.Name,
+ Database: targetDatabase.Name,
+ Environment: targetDatabase.Instance.Environment.Name,
+ Engine: db.MigrationEngine(targetDatabase.Project.WorkflowType),
+ Type: db.Branch,
+ Description: description,
+ Creator: task.Creator.Name,
+ IssueId: issueId,
+ Payload: "",
}
migrationId, _, err := targetDriver.ExecuteMigration(ctx, m, "")
if err != nil {
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index 5e13f8437f..5017be9d27 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -52,7 +52,8 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
var repository *api.Repository
mi := &db.MigrationInfo{
- Type: payload.MigrationType,
+ ReleaseVersion: server.version,
+ Type: payload.MigrationType,
}
if payload.VCSPushEvent == nil {
mi.Engine = db.UI