新增统一项目根实体及三库迁移并验证项目备份恢复
This commit is contained in:
1 parent
adb7d7f536
commit
6b72ac2e20
20 files changed
+846
-4
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
//go:embed migrations/sqlite/0001_initial.sql migrations/sqlite/0002_domain_options.sql migrations/sqlite/0003_deployment.sql migrations/sqlite/0004_task_attempts.sql migrations/sqlite/0005_users.sql migrations/sqlite/0006_server_validation.sql migrations/sqlite/0007_server_password_auth.sql migrations/sqlite/0009_server_ssh_key_status.sql migrations/mysql/0001_initial.sql migrations/mysql/0002_domain_options.sql migrations/mysql/0003_deployment.sql migrations/mysql/0004_task_attempts.sql migrations/mysql/0005_users.sql migrations/mysql/0006_server_validation.sql migrations/mysql/0007_server_password_auth.sql migrations/mysql/0009_server_ssh_key_status.sql migrations/postgres/0001_initial.sql migrations/postgres/0002_domain_options.sql migrations/postgres/0003_deployment.sql migrations/postgres/0004_task_attempts.sql migrations/postgres/0005_users.sql migrations/postgres/0006_server_validation.sql migrations/postgres/0007_server_password_auth.sql migrations/postgres/0009_server_ssh_key_status.sql
|
||||
//go:embed migrations/sqlite/0008_user_groups.sql migrations/mysql/0008_user_groups.sql migrations/postgres/0008_user_groups.sql
|
||||
//go:embed migrations/sqlite/0010_projects.sql migrations/mysql/0010_projects.sql migrations/postgres/0010_projects.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
@@ -35,7 +36,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const latestMigrationVersion = 9
|
||||
const latestMigrationVersion = 10
|
||||
|
||||
// currentMigrationVersion 在执行业务迁移前拒绝新版数据库及不连续的迁移历史。
|
||||
func currentMigrationVersion(ctx context.Context, db *sql.DB) (int, error) {
|
||||
@@ -164,6 +165,8 @@ func migrationName(version int) string {
|
||||
return "user_groups"
|
||||
case 9:
|
||||
return "server_ssh_key_status"
|
||||
case 10:
|
||||
return "projects"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ func TestMigrationCatalogDetectsIncompleteChanges(t *testing.T) {
|
||||
mutate func(fstest.MapFS, fstest.MapFS)
|
||||
}{
|
||||
{"unregistered_source", func(source, embedded fstest.MapFS) {
|
||||
source["migrations/mysql/0010_extra.sql"] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
source[fmt.Sprintf("migrations/mysql/%04d_extra.sql", latestMigrationVersion+1)] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
}},
|
||||
{"missing_embed", func(source, embedded fstest.MapFS) { delete(embedded, "migrations/postgres/0001_initial.sql") }},
|
||||
{"missing_source", func(source, embedded fstest.MapFS) { delete(source, "migrations/sqlite/0001_initial.sql") }},
|
||||
@@ -73,7 +73,7 @@ func TestMigrationCatalogDetectsIncompleteChanges(t *testing.T) {
|
||||
delete(source, "migrations/mysql/0001_initial.sql")
|
||||
}},
|
||||
{"unregistered_embed", func(source, embedded fstest.MapFS) {
|
||||
embedded["migrations/postgres/0010_extra.sql"] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
embedded[fmt.Sprintf("migrations/postgres/%04d_extra.sql", latestMigrationVersion+1)] = &fstest.MapFile{Data: []byte("SELECT 1;")}
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS projects (
|
||||
id VARCHAR(36) NOT NULL PRIMARY KEY,
|
||||
name VARCHAR(128) NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
||||
updated_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||||
|
||||
INSERT INTO projects (id, name, description)
|
||||
SELECT 'default', '默认项目', '历史资源的默认归属项目'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM projects WHERE id = 'default');
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS projects (
|
||||
id VARCHAR(36) NOT NULL PRIMARY KEY,
|
||||
name VARCHAR(128) NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO projects (id, name, description)
|
||||
SELECT 'default', '默认项目', '历史资源的默认归属项目'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM projects WHERE id = 'default');
|
||||
@@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS projects (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO projects (id, name, description)
|
||||
SELECT 'default', '默认项目', '历史资源的默认归属项目'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM projects WHERE id = 'default');
|
||||
@@ -0,0 +1,78 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSQLiteProjectMigrationPreservesBusiness(t *testing.T) {
|
||||
db, err := Open(t.Context(), sqliteBusinessUpgradeConfig(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
createSQLiteHistoricalBusiness(t, db.DB, 9)
|
||||
before := snapshotSQLiteHistoricalBusiness(t, db.DB)
|
||||
if err := Migrate(t.Context(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertSQLiteHistoricalBusiness(t, db.DB, before)
|
||||
var name string
|
||||
if err := db.DB.QueryRowContext(t.Context(), "SELECT name FROM projects WHERE id = 'default'").Scan(&name); err != nil || name != "默认项目" {
|
||||
t.Fatalf("默认项目未创建:%q,%v", name, err)
|
||||
}
|
||||
if _, err := db.DB.ExecContext(t.Context(), "UPDATE projects SET name = '调整后的默认项目' WHERE id = 'default'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 模拟建表、种子写入已成功,但版本记录尚未落盘的可重试情况。
|
||||
if _, err := db.DB.ExecContext(t.Context(), "DELETE FROM schema_migrations WHERE version = 10"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for round := 0; round < 2; round++ {
|
||||
if err := Migrate(t.Context(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var count int
|
||||
if err := db.DB.QueryRowContext(t.Context(), "SELECT COUNT(*) FROM projects").Scan(&count); err != nil || count != 1 {
|
||||
t.Fatalf("重复迁移改变项目数量:%d,%v", count, err)
|
||||
}
|
||||
if err := db.DB.QueryRowContext(t.Context(), "SELECT name FROM projects WHERE id = 'default'").Scan(&name); err != nil || name != "调整后的默认项目" {
|
||||
t.Fatalf("重复迁移覆盖用户修改:%q,%v", name, err)
|
||||
}
|
||||
assertSQLiteHistoricalBusiness(t, db.DB, before)
|
||||
}
|
||||
|
||||
func TestSQLiteProjectMigrationRollsBackAndRetries(t *testing.T) {
|
||||
db, err := Open(t.Context(), sqliteBusinessUpgradeConfig(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
createSQLiteHistoricalBusiness(t, db.DB, 9)
|
||||
before := snapshotSQLiteHistoricalBusiness(t, db.DB)
|
||||
// 在登记版本时制造失败,验证同一事务中的新表及默认项目也会回滚。
|
||||
_, err = db.DB.ExecContext(t.Context(), `CREATE TRIGGER fail_project_version BEFORE INSERT ON schema_migrations
|
||||
WHEN NEW.version = 10 BEGIN SELECT RAISE(ABORT, 'test-only'); END`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Migrate(t.Context(), db.DB, "sqlite"); err == nil {
|
||||
t.Fatal("版本登记故障未阻断迁移")
|
||||
}
|
||||
version, err := currentMigrationVersion(t.Context(), db.DB)
|
||||
if err != nil || version != 9 {
|
||||
t.Fatalf("失败迁移错误登记版本:%d,%v", version, err)
|
||||
}
|
||||
var count int
|
||||
if err := db.DB.QueryRowContext(t.Context(), "SELECT COUNT(*) FROM sqlite_master WHERE name = 'projects'").Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("失败迁移未回滚项目表:%d,%v", count, err)
|
||||
}
|
||||
assertSQLiteHistoricalBusiness(t, db.DB, before)
|
||||
if _, err := db.DB.ExecContext(t.Context(), "DROP TRIGGER fail_project_version"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Migrate(t.Context(), db.DB, "sqlite"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertSQLiteHistoricalBusiness(t, db.DB, before)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMySQLRealProjectMigrationRetry(t *testing.T) {
|
||||
fixture := startRealMySQL(t)
|
||||
db := fixture.database(t)
|
||||
ctx := t.Context()
|
||||
if _, err := db.DB.ExecContext(ctx, migrationTableSQL("mysql")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for version := 1; version <= 9; version++ {
|
||||
if err := applyMigration(ctx, db.DB, "mysql", version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := db.DB.ExecContext(ctx, `INSERT INTO tasks (id, domain_id, task_type, status, message)
|
||||
VALUES ('historical-task', 'historical-domain', 'deploy', 'success', '保留历史任务')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// MySQL 的 DDL 不会随事务回滚,必须验证建表后版本登记失败可安全重试。
|
||||
var schema string
|
||||
if err := db.DB.QueryRowContext(ctx, "SELECT DATABASE()").Scan(&schema); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema = "`" + strings.ReplaceAll(schema, "`", "``") + "`"
|
||||
// 故障注入仅使用隔离实例管理连接,不给产品连接增加全局权限。
|
||||
if _, err := fixture.admin.ExecContext(ctx, `CREATE TRIGGER `+schema+`.fail_project_version BEFORE INSERT ON `+schema+`.schema_migrations
|
||||
FOR EACH ROW SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'test-only'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Migrate(ctx, db.DB, "mysql"); err == nil {
|
||||
t.Fatal("版本登记故障未阻断 MySQL 迁移")
|
||||
}
|
||||
version, err := currentMigrationVersion(ctx, db.DB)
|
||||
if err != nil || version != 9 {
|
||||
t.Fatalf("失败迁移错误登记版本:%d,%v", version, err)
|
||||
}
|
||||
var name string
|
||||
if err := db.DB.QueryRowContext(ctx, "SELECT name FROM projects WHERE id = 'default'").Scan(&name); err != nil || name != "默认项目" {
|
||||
t.Fatalf("MySQL 部分迁移的默认项目缺失:%q,%v", name, err)
|
||||
}
|
||||
if _, err := db.DB.ExecContext(ctx, "UPDATE projects SET name = '保留用户调整' WHERE id = 'default'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := fixture.admin.ExecContext(ctx, "DROP TRIGGER "+schema+".fail_project_version"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for repeat := 0; repeat < 2; repeat++ {
|
||||
if err := Migrate(ctx, db.DB, "mysql"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var count int
|
||||
if err := db.DB.QueryRowContext(ctx, "SELECT COUNT(*) FROM projects").Scan(&count); err != nil || count != 1 {
|
||||
t.Fatalf("重试后项目数量错误:%d,%v", count, err)
|
||||
}
|
||||
if err := db.DB.QueryRowContext(ctx, "SELECT name FROM projects WHERE id = 'default'").Scan(&name); err != nil || name != "保留用户调整" {
|
||||
t.Fatalf("重试覆盖已有默认项目:%q,%v", name, err)
|
||||
}
|
||||
var message string
|
||||
if err := db.DB.QueryRowContext(ctx, "SELECT message FROM tasks WHERE id = 'historical-task'").Scan(&message); err != nil || message != "保留历史任务" {
|
||||
t.Fatalf("迁移影响历史任务:%q,%v", message, err)
|
||||
}
|
||||
version, err = currentMigrationVersion(ctx, db.DB)
|
||||
if err != nil || version != latestMigrationVersion {
|
||||
t.Fatalf("重试后迁移版本不完整:%d,%v", version, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user