保存人工处置持久化与实库回归检查点

This commit is contained in:
xuans committed 2026-10-03 19:49:36 +08:00
1 parent ed0c97b957
commit 17eb2fc295
25 files changed
+1070 -3

No files matched your search

+11 -2
View File
@@ -22,6 +22,9 @@ import (
//go:embed migrations/sqlite/0018_recovery_evidence.sql migrations/mysql/0018_recovery_evidence.sql migrations/postgres/0018_recovery_evidence.sql migrations/sqlite/0019_password_auth_only.sql migrations/mysql/0019_password_auth_only.sql migrations/postgres/0019_password_auth_only.sql
//go:embed migrations/sqlite/0020_task_deployment_reports.sql migrations/mysql/0020_task_deployment_reports.sql migrations/postgres/0020_task_deployment_reports.sql
//go:embed migrations/sqlite/0021_task_validation_results.sql migrations/mysql/0021_task_validation_results.sql migrations/postgres/0021_task_validation_results.sql
//go:embed migrations/sqlite/0022_recovery_dispositions.sql migrations/mysql/0022_recovery_dispositions.sql migrations/postgres/0022_recovery_dispositions.sql
//go:embed migrations/sqlite/0023_recovery_claims.sql migrations/mysql/0023_recovery_claims.sql migrations/postgres/0023_recovery_claims.sql
//go:embed migrations/sqlite/0024_recovery_events.sql migrations/mysql/0024_recovery_events.sql migrations/postgres/0024_recovery_events.sql
var migrationFiles embed.FS
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
@@ -46,7 +49,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
return nil
}
const latestMigrationVersion = 21
const latestMigrationVersion = 24
// currentMigrationVersion 在执行业务迁移前拒绝新版数据库及不连续的迁移历史。
func currentMigrationVersion(ctx context.Context, db *sql.DB) (int, error) {
@@ -86,7 +89,7 @@ func applyMigration(ctx context.Context, db *sql.DB, driver string, version int)
return applySQLiteTaskMigration(ctx, db, statements, version)
}
if driver == "mysql" {
if (version >= 15 && version <= 17) || version == 20 || version == 21 {
if (version >= 15 && version <= 17) || (version >= 20 && version <= 24) {
return applyMySQLDeploymentMigration(ctx, db, version, statements)
}
if version == 14 {
@@ -243,6 +246,12 @@ func migrationName(version int) string {
return "task_deployment_reports"
case 21:
return "task_validation_results"
case 22:
return "recovery_dispositions"
case 23:
return "recovery_claims"
case 24:
return "recovery_events"
default:
return "unknown"
}
@@ -0,0 +1,17 @@
CREATE TABLE recovery_dispositions (
id VARCHAR(64) NOT NULL,
transaction_id VARCHAR(160) NOT NULL,
server_id VARCHAR(160) NOT NULL,
operator_id VARCHAR(160) NOT NULL,
action VARCHAR(32) NOT NULL,
request_key VARCHAR(64) NOT NULL,
record_digest VARCHAR(64) NOT NULL,
server_digest VARCHAR(64) NOT NULL,
observation_digest VARCHAR(64) NOT NULL,
status VARCHAR(32) NOT NULL,
result_code VARCHAR(64) NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY uq_recovery_request (request_key)
);
@@ -0,0 +1,7 @@
CREATE TABLE recovery_claims (
transaction_id VARCHAR(160) NOT NULL,
disposition_id VARCHAR(64) NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (transaction_id),
UNIQUE KEY uq_recovery_claim (disposition_id)
);
@@ -0,0 +1,7 @@
CREATE TABLE recovery_events (
disposition_id VARCHAR(64) NOT NULL,
event_type VARCHAR(32) NOT NULL,
result_code VARCHAR(64) NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (disposition_id, event_type)
);
@@ -0,0 +1,15 @@
CREATE TABLE recovery_dispositions (
id TEXT NOT NULL PRIMARY KEY,
transaction_id TEXT NOT NULL,
server_id TEXT NOT NULL,
operator_id TEXT NOT NULL,
action TEXT NOT NULL,
request_key TEXT NOT NULL UNIQUE,
record_digest TEXT NOT NULL,
server_digest TEXT NOT NULL,
observation_digest TEXT NOT NULL,
status TEXT NOT NULL,
result_code TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
@@ -0,0 +1,5 @@
CREATE TABLE recovery_claims (
transaction_id TEXT NOT NULL PRIMARY KEY,
disposition_id TEXT NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL
);
@@ -0,0 +1,7 @@
CREATE TABLE recovery_events (
disposition_id TEXT NOT NULL,
event_type TEXT NOT NULL,
result_code TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (disposition_id, event_type)
);
@@ -0,0 +1,15 @@
CREATE TABLE recovery_dispositions (
id TEXT NOT NULL PRIMARY KEY,
transaction_id TEXT NOT NULL,
server_id TEXT NOT NULL,
operator_id TEXT NOT NULL,
action TEXT NOT NULL,
request_key TEXT NOT NULL UNIQUE,
record_digest TEXT NOT NULL,
server_digest TEXT NOT NULL,
observation_digest TEXT NOT NULL,
status TEXT NOT NULL,
result_code TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
);
@@ -0,0 +1,5 @@
CREATE TABLE recovery_claims (
transaction_id TEXT NOT NULL PRIMARY KEY,
disposition_id TEXT NOT NULL UNIQUE,
created_at DATETIME NOT NULL
);
@@ -0,0 +1,7 @@
CREATE TABLE recovery_events (
disposition_id TEXT NOT NULL,
event_type TEXT NOT NULL,
result_code TEXT NOT NULL,
created_at DATETIME NOT NULL,
PRIMARY KEY (disposition_id, event_type)
);
@@ -8,7 +8,7 @@ import (
)
func TestMySQLDeploymentShapeDefinitions(t *testing.T) {
for _, version := range []int{15, 16, 17, 20, 21} {
for _, version := range []int{15, 16, 17, 20, 21, 22, 23, 24} {
statements := mysqlDeploymentStatements(t, version)
shape, err := parseMySQLDeploymentShape(statements[0])
if err != nil || len(shape.Columns) == 0 || len(shape.Indexes) == 0 {
@@ -0,0 +1,58 @@
package database
import (
"fmt"
"testing"
)
func TestMySQLRealRecoveryDispositionMigrationRecovery(t *testing.T) {
f := startRealMySQL(t)
baseline := f.database(t)
if err := Migrate(t.Context(), baseline.DB, "mysql"); err != nil {
t.Fatal(err)
}
expected := mysqlSchemaSnapshot(t, baseline.DB)
for _, item := range []struct {
version int
table string
seed string
conflict string
}{
{22, "recovery_dispositions", `INSERT INTO recovery_dispositions VALUES
('kept', 'transaction', 'server', 'unix:0', 'record_inspection', 'request', 'record', 'server', 'observation', 'running', '', NOW(), NOW())`,
`ALTER TABLE recovery_dispositions DROP INDEX uq_recovery_request`},
{23, "recovery_claims", `INSERT INTO recovery_claims VALUES ('transaction', 'kept', NOW())`,
`ALTER TABLE recovery_claims DROP PRIMARY KEY`},
{24, "recovery_events", `INSERT INTO recovery_events VALUES ('kept', 'started', '', NOW())`,
`ALTER TABLE recovery_events DROP PRIMARY KEY, ADD PRIMARY KEY (event_type, disposition_id)`},
} {
for _, boundary := range []string{"before_create", "after_create", "with_record", "conflict"} {
t.Run(fmt.Sprintf("%d/%s", item.version, boundary), func(t *testing.T) {
db := f.database(t)
mysqlPrepareVersion(t, db.DB, item.version-1)
if boundary != "before_create" {
mysqlDeploymentExec(t, db.DB, mysqlDeploymentStatements(t, item.version)[0])
}
if boundary == "with_record" || boundary == "conflict" {
mysqlDeploymentExec(t, db.DB, item.seed)
}
if boundary == "conflict" {
mysqlDeploymentExec(t, db.DB, item.conflict)
before := fmt.Sprint(mysqlSchemaSnapshot(t, db.DB))
assertMySQLDeploymentMigrationRejected(t, db.DB, item.version-1)
if after := fmt.Sprint(mysqlSchemaSnapshot(t, db.DB)); after != before {
t.Fatal("结构冲突不得自动修复或覆盖")
}
} else {
mysqlAssertUpgrade(t, db.DB, expected)
}
if boundary == "with_record" || boundary == "conflict" {
var count int
if err := db.DB.QueryRowContext(t.Context(), "SELECT COUNT(*) FROM "+item.table).Scan(&count); err != nil || count != 1 {
t.Fatal("迁移或冲突拒绝丢失已有处置记录", err)
}
}
})
}
}
}