补齐任务幂等并发保护

新增三数据库任务幂等登记迁移,在任务创建事务中防止重复幂等键,并补充并发回归测试与进度清单。
This commit is contained in:
xuans committed 2026-09-30 02:00:36 +08:00
1 parent 163b3e9c5d
commit 7e14bbf42f
11 files changed
+168 -5

No files matched your search

+4 -1
View File
@@ -16,6 +16,7 @@ import (
//go:embed migrations/sqlite/0012_resource_replacement.sql migrations/mysql/0012_resource_replacement.sql migrations/postgres/0012_resource_replacement.sql
//go:embed migrations/sqlite/0013_history_scope.sql migrations/mysql/0013_history_scope.sql migrations/postgres/0013_history_scope.sql
//go:embed migrations/sqlite/0014_project_members.sql migrations/mysql/0014_project_members.sql migrations/postgres/0014_project_members.sql
//go:embed migrations/sqlite/0015_task_idempotency.sql migrations/mysql/0015_task_idempotency.sql migrations/postgres/0015_task_idempotency.sql
var migrationFiles embed.FS
func Migrate(ctx context.Context, db *sql.DB, driver string) error {
@@ -40,7 +41,7 @@ func Migrate(ctx context.Context, db *sql.DB, driver string) error {
return nil
}
const latestMigrationVersion = 14
const latestMigrationVersion = 15
// currentMigrationVersion 在执行业务迁移前拒绝新版数据库及不连续的迁移历史。
func currentMigrationVersion(ctx context.Context, db *sql.DB) (int, error) {
@@ -220,6 +221,8 @@ func migrationName(version int) string {
return "history_scope"
case 14:
return "project_members"
case 15:
return "task_idempotency"
default:
return "unknown"
}
@@ -0,0 +1,14 @@
CREATE TABLE task_idempotency (
domain_id VARCHAR(36) NOT NULL,
idempotency_key VARCHAR(255) NOT NULL,
task_id VARCHAR(36) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (domain_id, idempotency_key),
UNIQUE KEY uq_task_idempotency_task (task_id)
);
INSERT INTO task_idempotency (domain_id, idempotency_key, task_id, created_at)
SELECT domain_id, idempotency_key, MIN(id), MIN(created_at)
FROM tasks
WHERE idempotency_key <> ''
GROUP BY domain_id, idempotency_key;
@@ -0,0 +1,14 @@
CREATE TABLE task_idempotency (
domain_id TEXT NOT NULL,
idempotency_key TEXT NOT NULL,
task_id TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (domain_id, idempotency_key),
UNIQUE (task_id)
);
INSERT INTO task_idempotency (domain_id, idempotency_key, task_id, created_at)
SELECT domain_id, idempotency_key, MIN(id), MIN(created_at)
FROM tasks
WHERE idempotency_key <> ''
GROUP BY domain_id, idempotency_key;
@@ -0,0 +1,14 @@
CREATE TABLE task_idempotency (
domain_id TEXT NOT NULL,
idempotency_key TEXT NOT NULL,
task_id TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (domain_id, idempotency_key),
UNIQUE (task_id)
);
INSERT INTO task_idempotency (domain_id, idempotency_key, task_id, created_at)
SELECT domain_id, idempotency_key, MIN(id), MIN(created_at)
FROM tasks
WHERE idempotency_key <> ''
GROUP BY domain_id, idempotency_key;