Skip to content

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

fix: use MIGRATE instead of BASELINE type for CREATE DATABASE (#2248)

tianzhou97f1fa492573
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index c45d8bb9aa..7fe7e8a6d1 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -183,7 +183,7 @@ const (
// 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.
+ // Used for DDL change including CREATE DATABASE.
Migrate MigrationType = "MIGRATE"
// Branch is the migration type for BRANCH.
// Used when restoring from a backup (the restored database branched from the original backup).
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 30e770fb39..4d9ed56953 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -163,13 +163,13 @@ func ExecuteMigration(ctx context.Context, executor MigrationExecutor, m *db.Mig
// Phase 3 - Executing migration
// Branch migration type always has empty sql.
- // Baseline migration type could has non-empty sql but will not execute, except for CreateDatabase.
+ // Baseline migration type could has non-empty sql but will not execute.
// https://github.com/bytebase/bytebase/issues/394
doMigrate := true
if statement == "" {
doMigrate = false
}
- if m.Type == db.Baseline && !m.CreateDatabase {
+ if m.Type == db.Baseline {
doMigrate = false
}
if doMigrate {
diff --git a/server/task_executor_database_create.go b/server/task_executor_database_create.go
index b5572019c2..a0365aff02 100644
--- a/server/task_executor_database_create.go
+++ b/server/task_executor_database_create.go
@@ -60,7 +60,7 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
zap.String("statement", statement),
)
- // Create a baseline migration history upon creating the database.
+ // Create a migrate migration history upon creating the database.
// TODO(d): support semantic versioning.
mi := &db.MigrationInfo{
ReleaseVersion: server.profile.Version,
@@ -69,7 +69,7 @@ func (exec *DatabaseCreateTaskExecutor) RunOnce(ctx context.Context, server *Ser
Database: payload.DatabaseName,
Environment: instance.Environment.Name,
Source: db.UI,
- Type: db.Baseline,
+ Type: db.Migrate,
Description: "Create database",
CreateDatabase: true,
Force: true,
diff --git a/store/pg_engine.go b/store/pg_engine.go
index e22ba43b93..3f71c02168 100644
--- a/store/pg_engine.go
+++ b/store/pg_engine.go
@@ -326,12 +326,15 @@ func migrate(ctx context.Context, d dbdriver.Driver, curVer *semver.Version, mod
}
var stmt string
+ var createDatabase bool
if strictDb {
// User gives only database instead of instance, we cannot create database again.
stmt = fmt.Sprintf("%s\n%s", buf, dataBuf)
+ createDatabase = false
} else {
// We will create the database together with initial schema and data migration.
stmt = fmt.Sprintf("CREATE DATABASE %s;\n\\connect \"%s\";\n%s\n%s", databaseName, databaseName, buf, dataBuf)
+ createDatabase = true
}
if _, _, err := d.ExecuteMigration(
@@ -347,7 +350,7 @@ func migrate(ctx context.Context, d dbdriver.Driver, curVer *semver.Version, mod
Source: dbdriver.LIBRARY,
Type: dbdriver.Migrate,
Description: fmt.Sprintf("Initial migration version %s server version %s with file %s.", cutoffSchemaVersion, serverVersion, latestSchemaPath),
- CreateDatabase: !strictDb,
+ CreateDatabase: createDatabase,
Force: true,
},
stmt,
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 773cd29f0e..7b0f9c945a 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -166,7 +166,7 @@ func TestSchemaAndDataUpdate(t *testing.T) {
{
Database: databaseName,
Source: db.UI,
- Type: db.Baseline,
+ Type: db.Migrate,
Status: db.Done,
Schema: "",
SchemaPrev: "",
@@ -227,7 +227,7 @@ func TestSchemaAndDataUpdate(t *testing.T) {
{
Database: cloneDatabaseName,
Source: db.UI,
- Type: db.Baseline,
+ Type: db.Migrate,
Status: db.Done,
Schema: "",
SchemaPrev: "",
@@ -516,7 +516,7 @@ func TestVCS(t *testing.T) {
{
Database: databaseName,
Source: db.UI,
- Type: db.Baseline,
+ Type: db.Migrate,
Status: db.Done,
Schema: "",
SchemaPrev: "",