- 数据库迁移与本地缓存:在 `core/db/migrations.ts` 新增 version 5 迁移 (`005_workflow_job_cache`),创建 `workflow_job_index` 缓存表及高效复合索引;实现 `core/db/workflow-job-cache.ts`,支持按企业+工作流+单号快速检索与幂等增量缓存; - 流式早停探查与业务编排:在 `domains/platform/workflow/execution/search-by-flow.ts` 实现 `searchWorkflowJobByFlowCode`,采用「本地 SQLite 缓存 0ms 直达 ➔ 远端流式探查早停熔断并自动建立本地索引」机制,彻底解决云端 Piper API 无单号索引导致的排查低效问题; - CLI 命令层升级: - `workflow logs <pipelineId> --flow=<单号>`:直接秒级定位单据关联 Job 实例; - `platform records <业务对象>`:支持单参数业务对象名称,自动在企业全量自建应用中全局反查与智能寻址,若跨应用重名则抛出精准候选; - 命令行元数据对齐:在 `command-metadata.ts` 为 `workflow` 补齐 `--max-pages`、`--maxPages` 与 `--page-limit` 选项注册,满足严格参数词汇表静态检查; - 测试与文档:新增并更新 `workflow-job-cache.test.ts`、`search-by-flow.test.ts`、`platform.test.ts` 与 `db.test.ts`,更新核心与域层文档,全量 1126 个测试与类型检查全部通过。
119 lines
4.6 KiB
TypeScript
119 lines
4.6 KiB
TypeScript
import { Database } from "bun:sqlite";
|
|
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import { closeDatabase, getDatabase } from "./connection.js";
|
|
import { runMigrations } from "./migrations.js";
|
|
|
|
describe("Core DB - 数据库连接与结构化迁移机制 (加固测试)", () => {
|
|
let db: Database;
|
|
|
|
beforeEach(() => {
|
|
db = getDatabase(":memory:");
|
|
});
|
|
|
|
afterEach(() => {
|
|
closeDatabase();
|
|
});
|
|
|
|
test("数据库初始化时自动应用所有版本迁移并记录到 _schema_migrations", () => {
|
|
const rows = db.query("SELECT version, name FROM _schema_migrations ORDER BY version ASC").all() as Array<{
|
|
version: number;
|
|
name: string;
|
|
}>;
|
|
expect(rows.length).toBeGreaterThanOrEqual(1);
|
|
expect(rows[0].version).toBe(1);
|
|
expect(rows[0].name).toBe("001_core_schema_and_approval_tables");
|
|
});
|
|
|
|
test("核心数据表与新增的多级缓存表结构完备且索引有效", () => {
|
|
// 检查表是否存在
|
|
const tables = db
|
|
.query("SELECT name FROM sqlite_master WHERE type = 'table'")
|
|
.all() as Array<{ name: string }>;
|
|
const tableNames = new Set(tables.map((t) => t.name));
|
|
|
|
expect(tableNames.has("rules")).toBe(true);
|
|
expect(tableNames.has("preferences")).toBe(true);
|
|
expect(tableNames.has("approval_runs")).toBe(true);
|
|
expect(tableNames.has("approval_findings")).toBe(true);
|
|
expect(tableNames.has("aipro_logs")).toBe(true);
|
|
expect(tableNames.has("cache_entries")).toBe(true);
|
|
expect(tableNames.has("observed_events")).toBe(true);
|
|
expect(tableNames.has("workflow_error_alerts")).toBe(true);
|
|
expect(tableNames.has("workflow_job_index")).toBe(true);
|
|
|
|
// 检查加固索引:UPPER(short_id)、findings(rule_id)
|
|
const indices = db
|
|
.query("SELECT name FROM sqlite_master WHERE type = 'index'")
|
|
.all() as Array<{ name: string }>;
|
|
const indexNames = new Set(indices.map((i) => i.name));
|
|
|
|
expect(indexNames.has("idx_runs_corp_id")).toBe(true);
|
|
expect(indexNames.has("idx_runs_expires_at")).toBe(true);
|
|
expect(indexNames.has("idx_runs_flow_id")).toBe(true);
|
|
expect(indexNames.has("idx_runs_short_id_upper")).toBe(true);
|
|
expect(indexNames.has("idx_findings_rule_id")).toBe(true);
|
|
expect(indexNames.has("idx_cache_namespace")).toBe(true);
|
|
expect(indexNames.has("idx_cache_expires_at")).toBe(true);
|
|
expect(indexNames.has("idx_observed_events_scope")).toBe(true);
|
|
expect(indexNames.has("idx_workflow_alerts_scope")).toBe(true);
|
|
expect(indexNames.has("idx_wf_jobs_corp_pipeline_code")).toBe(true);
|
|
expect(indexNames.has("idx_wf_jobs_code_upper")).toBe(true);
|
|
});
|
|
|
|
test("cache_entries 具备复合主键 (namespace, key) 约束", () => {
|
|
db.run(
|
|
"INSERT INTO cache_entries (namespace, key, value, expires_at, updated_at) VALUES ('n1', 'k1', 'v1', 9999, 1000)"
|
|
);
|
|
// 相同 key 但不同 namespace 可以成功插入
|
|
db.run(
|
|
"INSERT INTO cache_entries (namespace, key, value, expires_at, updated_at) VALUES ('n2', 'k1', 'v2', 9999, 1000)"
|
|
);
|
|
|
|
const countRow = db.query("SELECT COUNT(*) AS c FROM cache_entries WHERE key = 'k1'").get() as { c: number };
|
|
expect(countRow.c).toBe(2);
|
|
});
|
|
|
|
test("migration 2 可以平滑升级遗留的单列主键 cache_entries 表", () => {
|
|
const legacyDb = new Database(":memory:");
|
|
legacyDb.run(`
|
|
CREATE TABLE cache_entries (
|
|
key TEXT PRIMARY KEY,
|
|
namespace TEXT NOT NULL,
|
|
value TEXT NOT NULL,
|
|
expires_at INTEGER NOT NULL,
|
|
updated_at INTEGER NOT NULL
|
|
);
|
|
`);
|
|
legacyDb.run(
|
|
"INSERT INTO cache_entries (key, namespace, value, expires_at, updated_at) VALUES ('k1', 'n1', 'v1', 9999, 1000)"
|
|
);
|
|
|
|
// 运行全量迁移
|
|
runMigrations(legacyDb);
|
|
|
|
// 验证升级后支持相同 key 不同 namespace
|
|
expect(() => {
|
|
legacyDb.run(
|
|
"INSERT INTO cache_entries (key, namespace, value, expires_at, updated_at) VALUES ('k1', 'n2', 'v2', 9999, 1000)"
|
|
);
|
|
}).not.toThrow();
|
|
|
|
// 验证 ON CONFLICT(namespace, key) 正常工作
|
|
expect(() => {
|
|
legacyDb.run(`
|
|
INSERT INTO cache_entries (namespace, key, value, expires_at, updated_at)
|
|
VALUES ('n1', 'k1', 'v1_updated', 9999, 2000)
|
|
ON CONFLICT(namespace, key) DO UPDATE SET value = excluded.value
|
|
`);
|
|
}).not.toThrow();
|
|
|
|
const row = legacyDb.query("SELECT value FROM cache_entries WHERE namespace = 'n1' AND key = 'k1'").get() as { value: string };
|
|
expect(row.value).toBe("v1_updated");
|
|
legacyDb.close();
|
|
});
|
|
|
|
test("再次执行 runMigrations 保证幂等且不抛错", () => {
|
|
expect(() => runMigrations(db)).not.toThrow();
|
|
});
|
|
});
|