Skip to content

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

feat: add schema change for supporting data migration (DML change) (#339)

tianzhoudbfdfd91b9bd
diff --git a/plugin/db/clickhouse/clickhouse_migration_schema.sql b/plugin/db/clickhouse/clickhouse_migration_schema.sql
index b58c858d41..a4813332b0 100644
--- a/plugin/db/clickhouse/clickhouse_migration_schema.sql
+++ b/plugin/db/clickhouse/clickhouse_migration_schema.sql
@@ -21,7 +21,7 @@ CREATE TABLE bytebase.migration_history (
sequence INTEGER UNSIGNED NOT NULL,
-- We call it engine because maybe we could load history from other migration tool.
`engine` ENUM('UI' = 0, 'VCS' = 1) NOT NULL,
- `type` ENUM('BASELINE' = 0, 'MIGRATE' = 1, 'BRANCH' = 2) NOT NULL,
+ `type` ENUM('BASELINE' = 0, 'MIGRATE' = 1, 'BRANCH' = 2, 'DATA' = 3) 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' = 0, 'DONE' = 1, 'FAILED' = 2) NOT NULL,
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 173e7fbe31..1735133929 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -148,11 +148,19 @@ type MigrationType string
const (
// Baseline is the migration type for BASELINE.
+ // Used for establishing schema baseline, this is used when
+ // 1. Onboard the database into Bytebase since Bytebase needs to know the current database schema.
+ // 2. Had schema drift and need to re-establish the baseline.
Baseline MigrationType = "BASELINE"
// Migrate is the migration type for MIGRATE.
+ // Used for DDL change.
Migrate MigrationType = "MIGRATE"
// Branch is the migration type for BRANCH.
+ // Used when restoring from a backup (the restored database branched from the original backup).
Branch MigrationType = "BRANCH"
+ // Data is the migration type for DATA.
+ // Used for DML change.
+ Data MigrationType = "DATA"
)
func (e MigrationType) String() string {
@@ -163,6 +171,8 @@ func (e MigrationType) String() string {
return "MIGRATE"
case Branch:
return "BRANCH"
+ case Data:
+ return "DATA"
}
return "UNKNOWN"
}
@@ -254,10 +264,12 @@ func ParseMigrationInfo(filePath string, filePathTemplate string) (*MigrationInf
} else if placeholder == "TYPE" {
if matchList[index] == "baseline" {
mi.Type = Baseline
+ } else if matchList[index] == "data" {
+ mi.Type = Data
} else if matchList[index] == "migrate" {
mi.Type = Migrate
} else {
- return nil, fmt.Errorf("file path %q contains invalid migration type %q, must be 'baseline' or 'migrate'", filePath, matchList[index])
+ return nil, fmt.Errorf("file path %q contains invalid migration type %q, must be 'baseline', 'migrate' or 'data'", filePath, matchList[index])
}
} else if placeholder == "DESCRIPTION" {
mi.Description = matchList[index]
@@ -275,8 +287,10 @@ func ParseMigrationInfo(filePath string, filePathTemplate string) (*MigrationInf
if mi.Description == "" {
if mi.Type == Baseline {
mi.Description = fmt.Sprintf("Create %s baseline", mi.Database)
+ } else if mi.Type == Data {
+ mi.Description = fmt.Sprintf("Create %s data change", mi.Database)
} else {
- mi.Description = fmt.Sprintf("Create %s migration", mi.Database)
+ mi.Description = fmt.Sprintf("Create %s schema migration", mi.Database)
}
} else {
// Replace _ with space
diff --git a/plugin/db/driver_test.go b/plugin/db/driver_test.go
index 0978d80534..be47a308fe 100644
--- a/plugin/db/driver_test.go
+++ b/plugin/db/driver_test.go
@@ -25,7 +25,7 @@ func TestParseMigrationInfo(t *testing.T) {
Environment: "",
Engine: VCS,
Type: Migrate,
- Description: "Create db1 migration",
+ Description: "Create db1 schema migration",
Creator: "",
},
wantErr: "",
@@ -40,7 +40,7 @@ func TestParseMigrationInfo(t *testing.T) {
Environment: "",
Engine: VCS,
Type: Migrate,
- Description: "Create db foo_+-=/_#?!$. migration",
+ Description: "Create db foo_+-=/_#?!$. schema migration",
Creator: "",
},
wantErr: "",
@@ -55,7 +55,7 @@ func TestParseMigrationInfo(t *testing.T) {
Environment: "",
Engine: VCS,
Type: Migrate,
- Description: "Create db1 migration",
+ Description: "Create db1 schema migration",
Creator: "",
},
wantErr: "",
@@ -70,7 +70,7 @@ func TestParseMigrationInfo(t *testing.T) {
Environment: "dev",
Engine: VCS,
Type: Migrate,
- Description: "Create db1 migration",
+ Description: "Create db1 schema migration",
Creator: "",
},
wantErr: "",
@@ -100,7 +100,7 @@ func TestParseMigrationInfo(t *testing.T) {
Environment: "",
Engine: VCS,
Type: Migrate,
- Description: "Create db1 migration",
+ Description: "Create db1 schema migration",
Creator: "",
},
wantErr: "",
@@ -150,6 +150,36 @@ func TestParseMigrationInfo(t *testing.T) {
},
wantErr: "",
},
+ {
+ filePath: "db_shop1__001foo__data",
+ filePathTemplate: "{{DB_NAME}}__{{VERSION}}__{{TYPE}}",
+ want: MigrationInfo{
+ Version: "001foo",
+ Namespace: "db_shop1",
+ Database: "db_shop1",
+ Environment: "",
+ Engine: VCS,
+ Type: Data,
+ Description: "Create db_shop1 data change",
+ Creator: "",
+ },
+ wantErr: "",
+ },
+ {
+ filePath: "db_shop1__001foo__data__fix_customer_info",
+ filePathTemplate: "{{DB_NAME}}__{{VERSION}}__{{TYPE}}__{{DESCRIPTION}}",
+ want: MigrationInfo{
+ Version: "001foo",
+ Namespace: "db_shop1",
+ Database: "db_shop1",
+ Environment: "",
+ Engine: VCS,
+ Type: Data,
+ Description: "Fix customer info",
+ Creator: "",
+ },
+ wantErr: "",
+ },
{
filePath: "db",
filePathTemplate: "{{DB_NAME}}__{{VERSION}}",
diff --git a/plugin/db/mysql/mysql_migration_schema.sql b/plugin/db/mysql/mysql_migration_schema.sql
index abacb2f380..32f0285d70 100644
--- a/plugin/db/mysql/mysql_migration_schema.sql
+++ b/plugin/db/mysql/mysql_migration_schema.sql
@@ -21,7 +21,7 @@ CREATE TABLE bytebase.migration_history (
sequence INTEGER UNSIGNED NOT NULL,
-- 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,
+ `type` ENUM('BASELINE', 'MIGRATE', 'BRANCH', 'DATA') 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', 'FAILED') NOT NULL,
diff --git a/plugin/db/pg/pg_migration_schema.sql b/plugin/db/pg/pg_migration_schema.sql
index 7b95657211..5071796350 100644
--- a/plugin/db/pg/pg_migration_schema.sql
+++ b/plugin/db/pg/pg_migration_schema.sql
@@ -21,7 +21,7 @@ CREATE TABLE migration_history (
sequence INTEGER NOT NULL CHECK (sequence >= 0),
-- 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')),
+ type TEXT NOT NULL CHECK (type in ('BASELINE', 'MIGRATE', 'BRANCH', 'DATA')),
-- 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', 'FAILED')),