refactor(db): 统一数据库 schema — 软删除、命名规范、约束标准化

- 全表新增 deleted_at 列,统一软删除替代硬删除+archived_at
- models.model_id 重命名为 external_id,消除语义混淆
- conversations.model_id 改为可空(模型为建议而非绑定)
- messages 新增 updated_at,移除 CASCADE 改为 DAO 层级联
- 移除 DB 层 UNIQUE 约束,改为应用层检查(配合软删除)
- 新增 helpers.ts(baseColumns + 构造层防御)、ESLint 规则、契约测试
- 迁移 0004 补全 CHECK 约束(providers.type/materials.status/messages.role)
- DAO 层全面重写:级联软删除、应用层唯一、provider 删除保护
- 路由/前端/测试全量适配 externalId 重命名及类型变更
This commit is contained in:
2026-06-05 01:02:23 +08:00
parent e25b2537fd
commit db40d04dc5
37 changed files with 1564 additions and 324 deletions

View File

@@ -11,7 +11,6 @@ import { installFetchMock, jsonResponse, renderWithProviders } from "../test-uti
const PROJECT_ID = "proj-1";
const MOCK_PROJECT: Project = {
archivedAt: null,
createdAt: "2026-01-01T00:00:00.000Z",
description: "",
id: PROJECT_ID,
@@ -24,9 +23,9 @@ const TEXT_MODEL: Model = {
capabilities: ["text"],
contextLength: null,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "gpt-4o",
id: "model-1",
maxOutputTokens: null,
modelId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
updatedAt: "2024-01-01T00:00:00.000Z",

View File

@@ -13,9 +13,9 @@ const TEXT_MODEL: Model = {
capabilities: ["text"],
contextLength: null,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "gpt-4o",
id: "model-1",
maxOutputTokens: null,
modelId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
updatedAt: "2024-01-01T00:00:00.000Z",

View File

@@ -24,9 +24,9 @@ const ENABLED_MODEL: Model = {
capabilities: ["text", "reasoning"],
contextLength: 128000,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "gpt-4o",
id: "m1",
maxOutputTokens: 4096,
modelId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
updatedAt: "2024-01-01T00:00:00.000Z",
@@ -36,9 +36,9 @@ const DISABLED_MODEL: Model = {
capabilities: ["text"],
contextLength: null,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "deepseek-chat",
id: "m2",
maxOutputTokens: null,
modelId: "deepseek-chat",
name: "DeepSeek Chat",
providerId: "pv2",
updatedAt: "2024-01-01T00:00:00.000Z",

View File

@@ -9,7 +9,6 @@ import { ProjectContext } from "../../../../src/web/shared/hooks/use-current-pro
import { installFetchMock, jsonResponse, renderWithProviders } from "../../test-utils";
const MOCK_PROJECT: Project = {
archivedAt: null,
createdAt: "2026-01-01T00:00:00.000Z",
description: "",
id: "project-1",

View File

@@ -14,9 +14,9 @@ const MODEL = {
capabilities: ["text"] as Array<"text">,
contextLength: null,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "gpt-4o",
id: "m1",
maxOutputTokens: null,
modelId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
updatedAt: "2024-01-01T00:00:00.000Z",
@@ -59,7 +59,7 @@ describe("use-models request helpers", () => {
await createModel({
capabilities: ["text"],
modelId: "gpt-4o",
externalId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
});
@@ -75,7 +75,7 @@ describe("use-models request helpers", () => {
]);
expect(jsonBody(calls[0]?.body)).toEqual({
capabilities: ["text"],
modelId: "gpt-4o",
externalId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
});
@@ -86,7 +86,7 @@ describe("use-models request helpers", () => {
installFetchMock(() => jsonResponse({ error: "模型名称已存在" }, { status: 409 }));
await expectRejectsWithMessage(
() => createModel({ capabilities: ["text"], modelId: "gpt-4o", name: "重复", providerId: "pv1" }),
() => createModel({ capabilities: ["text"], externalId: "gpt-4o", name: "重复", providerId: "pv1" }),
"模型名称已存在",
);
});
@@ -100,12 +100,12 @@ describe("use-models request helpers", () => {
test("testModelConnection 调用正确 URL 和 body", async () => {
const calls = installFetchMock(() => jsonResponse({ modelTestResponse: { message: "模型连接成功", ok: true } }));
const result = await testModelConnection({ modelId: "gpt-4o", providerId: "pv1" });
const result = await testModelConnection({ externalId: "gpt-4o", providerId: "pv1" });
expect(result.ok).toBe(true);
expect(result.message).toBe("模型连接成功");
expect(calls[0]?.method).toBe("POST");
expect(calls[0]?.url).toBe("/api/models/test");
expect(jsonBody(calls[0]?.body)).toEqual({ modelId: "gpt-4o", providerId: "pv1" });
expect(jsonBody(calls[0]?.body)).toEqual({ externalId: "gpt-4o", providerId: "pv1" });
});
});

View File

@@ -12,7 +12,6 @@ import {
import { installFetchMock, jsonResponse } from "../test-utils";
const PROJECT = {
archivedAt: null,
createdAt: "2024-01-01T00:00:00.000Z",
description: "描述",
id: "p1",

View File

@@ -32,9 +32,9 @@ const ENABLED_MODEL: Model = {
capabilities: ["text", "reasoning"],
contextLength: 128000,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "gpt-4o",
id: "m1",
maxOutputTokens: 4096,
modelId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
updatedAt: "2024-01-01T00:00:00.000Z",
@@ -190,7 +190,7 @@ describe("ModelFormModal", () => {
await waitFor(() =>
expect(testModelConnection).toHaveBeenCalledWith({
modelId: "gpt-4o",
externalId: "gpt-4o",
providerId: "pv1",
}),
);
@@ -231,9 +231,9 @@ const TEST_MODEL: Model = {
capabilities: ["text"],
contextLength: 128000,
createdAt: "2024-01-01T00:00:00.000Z",
externalId: "gpt-4o",
id: "m1",
maxOutputTokens: 4096,
modelId: "gpt-4o",
name: "GPT-4o",
providerId: "pv1",
updatedAt: "2024-01-01T00:00:00.000Z",
@@ -281,7 +281,7 @@ function createModelFetchMock() {
if (url.pathname === "/api/models" && call.method === "GET") {
const keyword = url.searchParams.get("keyword") ?? "";
const items = keyword ? models.filter((m) => `${m.name}${m.modelId}`.includes(keyword)) : models;
const items = keyword ? models.filter((m) => `${m.name}${m.externalId}`.includes(keyword)) : models;
return jsonResponse({ items, page: 1, pageSize: 20, total: items.length });
}

View File

@@ -11,7 +11,6 @@ import { ProjectTable } from "../../../src/web/features/projects/components/Proj
import { installFetchMock, jsonResponse, mockMetaResponse, renderWithProviders } from "../test-utils";
const ACTIVE_PROJECT: Project = {
archivedAt: null,
createdAt: "2024-01-01T00:00:00.000Z",
description: "活跃描述",
id: "p1",
@@ -21,7 +20,6 @@ const ACTIVE_PROJECT: Project = {
};
const ARCHIVED_PROJECT: Project = {
archivedAt: "2024-01-02T00:00:00.000Z",
createdAt: "2024-01-01T00:00:00.000Z",
description: "归档描述",
id: "p2",
@@ -57,7 +55,6 @@ function createProjectFetchMock(initialProjects: Project[] = [ACTIVE_PROJECT, AR
if (url.pathname === "/api/projects" && call.method === "POST") {
const data = jsonBody(call.body) as { description?: string; name: string };
const created: Project = {
archivedAt: null,
createdAt: "2024-01-03T00:00:00.000Z",
description: data.description ?? "",
id: "p-created",
@@ -83,13 +80,13 @@ function createProjectFetchMock(initialProjects: Project[] = [ACTIVE_PROJECT, AR
}
if (call.method === "POST" && action === "archive") {
const archived = { ...project, archivedAt: "2024-01-04T00:00:00.000Z", status: "archived" as const };
const archived = { ...project, status: "archived" as const };
projects = projects.map((item) => (item.id === id ? archived : item));
return jsonResponse({ project: archived });
}
if (call.method === "POST" && action === "restore") {
const restored = { ...project, archivedAt: null, status: "active" as const };
const restored = { ...project, status: "active" as const };
projects = projects.map((item) => (item.id === id ? restored : item));
return jsonResponse({ project: restored });
}

View File

@@ -6,7 +6,6 @@ import { App } from "../../../src/web/app";
import { renderWithProviders } from "../test-utils";
const MOCK_PROJECT = {
archivedAt: null,
createdAt: "2024-01-01T00:00:00.000Z",
description: "测试项目",
id: "test-project-id",
@@ -15,7 +14,7 @@ const MOCK_PROJECT = {
updatedAt: "2024-01-01T00:00:00.000Z",
};
function createMockHandler(overrides?: { archivedAt?: string; status?: "active" | "archived" }) {
function createMockHandler(overrides?: { status?: "active" | "archived" }) {
const project = { ...MOCK_PROJECT, ...overrides };
const handler = (input: RequestInfo | URL) => {
const url = input instanceof Request ? input.url : typeof input === "string" ? input : input.toString();
@@ -92,7 +91,7 @@ describe("Workbench 路由", () => {
});
test("archived 项目显示不可访问", async () => {
createMockHandler({ archivedAt: "2024-06-01T00:00:00.000Z", status: "archived" });
createMockHandler({ status: "archived" });
renderWithProviders(createElement(App), {
initialRoute: `/workbench/${MOCK_PROJECT.id}`,