保存执行租约与原子绑定复核前检查点
This commit is contained in:
1 parent
73dba287d9
commit
7a3b78daa3
28 files changed
+1236
-48
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package database
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMySQLDNSExecutionInterruptedMigration(t *testing.T) {
|
||||
f := startRealMySQL(t)
|
||||
for _, boundary := range []string{"before-create", "after-create", "with-record", "bad-column", "bad-index", "bad-collation"} {
|
||||
t.Run(boundary, func(t *testing.T) {
|
||||
db := f.database(t)
|
||||
mysqlPrepareVersion(t, db.DB, 28)
|
||||
statements := mysqlDeploymentStatements(t, 29)
|
||||
if boundary != "before-create" {
|
||||
mysqlDeploymentExec(t, db.DB, statements[0])
|
||||
}
|
||||
conflict := true
|
||||
switch boundary {
|
||||
case "bad-column":
|
||||
mysqlDeploymentExec(t, db.DB, "ALTER TABLE dns_execution_leases MODIFY COLUMN account_revision INT NULL")
|
||||
case "bad-index":
|
||||
mysqlDeploymentExec(t, db.DB, "ALTER TABLE dns_execution_leases DROP INDEX uq_dns_leases_attempt")
|
||||
case "bad-collation":
|
||||
mysqlDeploymentExec(t, db.DB, "ALTER TABLE dns_execution_leases CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
|
||||
default:
|
||||
conflict = false
|
||||
}
|
||||
if boundary == "with-record" {
|
||||
mysqlDeploymentExec(t, db.DB, `INSERT INTO dns_execution_leases VALUES ('kept','domain','binding','project','account',9,'cloudflare','secret://dns/test','task','attempt',1,0,'active',NOW(),NOW())`)
|
||||
}
|
||||
err := Migrate(t.Context(), db.DB, "mysql")
|
||||
if (err != nil) != conflict {
|
||||
t.Fatal("租约迁移中断恢复或冲突拒绝失败", err)
|
||||
}
|
||||
version, err := currentMigrationVersion(t.Context(), db.DB)
|
||||
if err != nil || (conflict && version != 28) || (!conflict && version != latestMigrationVersion) {
|
||||
t.Fatal("错误标记迁移版本", version, err)
|
||||
}
|
||||
if boundary == "with-record" {
|
||||
var status string
|
||||
if err := db.DB.QueryRowContext(t.Context(), `SELECT status FROM dns_execution_leases WHERE id = 'kept'`).Scan(&status); err != nil || status != "active" {
|
||||
t.Fatal("重入丢失活动秘密引用", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
//go:embed migrations/sqlite/0026_domain_challenge.sql migrations/mysql/0026_domain_challenge.sql migrations/postgres/0026_domain_challenge.sql
|
||||
//go:embed migrations/sqlite/0027_dns_accounts.sql migrations/mysql/0027_dns_accounts.sql migrations/postgres/0027_dns_accounts.sql
|
||||
//go:embed migrations/sqlite/0028_domain_dns_accounts.sql migrations/mysql/0028_domain_dns_accounts.sql migrations/postgres/0028_domain_dns_accounts.sql
|
||||
//go:embed migrations/sqlite/0029_dns_execution_leases.sql migrations/mysql/0029_dns_execution_leases.sql migrations/postgres/0029_dns_execution_leases.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
@@ -53,7 +54,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
const latestMigrationVersion = 28
|
||||
const latestMigrationVersion = 29
|
||||
|
||||
// currentMigrationVersion 在执行业务迁移前拒绝新版数据库及不连续的迁移历史。
|
||||
func currentMigrationVersion(ctx context.Context, db *sql.DB) (int, error) {
|
||||
@@ -99,7 +100,7 @@ func applyMigration(ctx context.Context, db *sql.DB, driver string, version int)
|
||||
if version == 27 {
|
||||
return applyMySQLDNSAccountMigration(ctx, db, version, statements)
|
||||
}
|
||||
if (version >= 15 && version <= 17) || (version >= 20 && version <= 24) {
|
||||
if (version >= 15 && version <= 17) || (version >= 20 && version <= 24) || version == 29 {
|
||||
return applyMySQLDeploymentMigration(ctx, db, version, statements)
|
||||
}
|
||||
if version == 14 {
|
||||
@@ -298,6 +299,8 @@ func migrationName(version int) string {
|
||||
return "dns_accounts"
|
||||
case 28:
|
||||
return "domain_dns_accounts"
|
||||
case 29:
|
||||
return "dns_execution_leases"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
CREATE TABLE dns_execution_leases (
|
||||
id VARCHAR(64) NOT NULL,
|
||||
domain_id VARCHAR(64) NOT NULL,
|
||||
binding_id VARCHAR(64) NOT NULL,
|
||||
project_id VARCHAR(64) NOT NULL,
|
||||
account_id VARCHAR(64) NOT NULL,
|
||||
account_revision INT NOT NULL,
|
||||
provider VARCHAR(64) NOT NULL,
|
||||
credential_ref VARCHAR(255) NOT NULL,
|
||||
task_id VARCHAR(64) NOT NULL,
|
||||
attempt_id VARCHAR(64) NOT NULL,
|
||||
attempt_number INT NOT NULL,
|
||||
scheduled INT NOT NULL,
|
||||
status VARCHAR(32) NOT NULL,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY uq_dns_leases_attempt (task_id, attempt_id),
|
||||
INDEX idx_dns_leases_account (account_id, status),
|
||||
INDEX idx_dns_leases_domain (domain_id, status)
|
||||
);
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE dns_execution_leases (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
domain_id TEXT NOT NULL,
|
||||
binding_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
account_id TEXT NOT NULL,
|
||||
account_revision INTEGER NOT NULL,
|
||||
provider TEXT NOT NULL,
|
||||
credential_ref TEXT NOT NULL,
|
||||
task_id TEXT NOT NULL,
|
||||
attempt_id TEXT NOT NULL,
|
||||
attempt_number INTEGER NOT NULL,
|
||||
scheduled INTEGER NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL,
|
||||
UNIQUE (task_id, attempt_id)
|
||||
);
|
||||
CREATE INDEX idx_dns_leases_account ON dns_execution_leases (account_id, status);
|
||||
CREATE INDEX idx_dns_leases_domain ON dns_execution_leases (domain_id, status);
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE dns_execution_leases (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
domain_id TEXT NOT NULL,
|
||||
binding_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
account_id TEXT NOT NULL,
|
||||
account_revision INTEGER NOT NULL,
|
||||
provider TEXT NOT NULL,
|
||||
credential_ref TEXT NOT NULL,
|
||||
task_id TEXT NOT NULL,
|
||||
attempt_id TEXT NOT NULL,
|
||||
attempt_number INTEGER NOT NULL,
|
||||
scheduled INTEGER NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL,
|
||||
UNIQUE (task_id, attempt_id)
|
||||
);
|
||||
CREATE INDEX idx_dns_leases_account ON dns_execution_leases (account_id, status);
|
||||
CREATE INDEX idx_dns_leases_domain ON dns_execution_leases (domain_id, status);
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestMySQLDeploymentShapeDefinitions(t *testing.T) {
|
||||
for _, version := range []int{15, 16, 17, 20, 21, 22, 23, 24, 27, 28} {
|
||||
for _, version := range []int{15, 16, 17, 20, 21, 22, 23, 24, 27, 28, 29} {
|
||||
statements := mysqlDeploymentStatements(t, version)
|
||||
shape, err := parseMySQLDeploymentShape(statements[0])
|
||||
if err != nil || len(shape.Columns) == 0 || len(shape.Indexes) == 0 {
|
||||
|
||||
Reference in new issue
Block a user