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:
@@ -44,12 +44,12 @@ async function patchConversationViaHandler(req: Request, db: Database): Promise<
|
||||
return h(req, db, MODE, LOG);
|
||||
}
|
||||
|
||||
function seedModel(db: Database, providerId: string, modelName = "GPT-4o", modelId = "gpt-4o"): string {
|
||||
function seedModel(db: Database, providerId: string, modelName = "GPT-4o", externalId = "gpt-4o"): string {
|
||||
const result = createModel(
|
||||
db,
|
||||
{
|
||||
capabilities: ["text"],
|
||||
modelId,
|
||||
externalId,
|
||||
name: modelName,
|
||||
providerId,
|
||||
},
|
||||
@@ -111,7 +111,7 @@ describe("聊天 API 路由", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("无可用模型时返回 400", async () => {
|
||||
test("无可用模型时创建会话 modelId 为 null", async () => {
|
||||
const handle = createMigratedMemoryTestDatabase("chat-create-no-model");
|
||||
try {
|
||||
const db = handle.db;
|
||||
@@ -123,9 +123,9 @@ describe("聊天 API 路由", () => {
|
||||
method: "POST",
|
||||
});
|
||||
const res = await createConversationViaHandler(req, db);
|
||||
expect(res.status).toBe(400);
|
||||
const body = (await res.json()) as { error: string };
|
||||
expect(body.error).toContain("模型");
|
||||
expect(res.status).toBe(201);
|
||||
const body = (await res.json()) as { conversation: Conversation };
|
||||
expect(body.conversation.modelId).toBeNull();
|
||||
handle.close();
|
||||
} finally {
|
||||
handle.cleanup();
|
||||
|
||||
@@ -22,7 +22,7 @@ function createTestModel(db: Database, pName: string, providerId?: string): Mode
|
||||
db,
|
||||
{
|
||||
capabilities: ["text"],
|
||||
modelId: pName.toLowerCase().replace(/[^a-z0-9-]/g, "-"),
|
||||
externalId: pName.toLowerCase().replace(/[^a-z0-9-]/g, "-"),
|
||||
name: pName,
|
||||
providerId: pid,
|
||||
},
|
||||
@@ -93,7 +93,7 @@ describe("models API routes", () => {
|
||||
const req = new Request("http://localhost/api/models", {
|
||||
body: JSON.stringify({
|
||||
capabilities: ["text", "reasoning"],
|
||||
modelId: "gpt-4o",
|
||||
externalId: "gpt-4o",
|
||||
name: "GPT-4o",
|
||||
providerId,
|
||||
}),
|
||||
@@ -104,7 +104,7 @@ describe("models API routes", () => {
|
||||
expect(res.status).toBe(201);
|
||||
const body = (await res.json()) as { model: Model };
|
||||
expect(body.model.name).toBe("GPT-4o");
|
||||
expect(body.model.modelId).toBe("gpt-4o");
|
||||
expect(body.model.externalId).toBe("gpt-4o");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -142,10 +142,10 @@ describe("models API routes", () => {
|
||||
test("GET /api/models filter by capabilities", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const p = seedProvider(db, "CapP");
|
||||
createModel(db, { capabilities: ["text"], modelId: "text-1", name: "TextModel", providerId: p }, LOG);
|
||||
createModel(db, { capabilities: ["text"], externalId: "text-1", name: "TextModel", providerId: p }, LOG);
|
||||
createModel(
|
||||
db,
|
||||
{ capabilities: ["reasoning"], modelId: "reasoning-1", name: "ReasoningModel", providerId: p },
|
||||
{ capabilities: ["reasoning"], externalId: "reasoning-1", name: "ReasoningModel", providerId: p },
|
||||
LOG,
|
||||
);
|
||||
|
||||
@@ -228,7 +228,7 @@ describe("models API routes", () => {
|
||||
const req = new Request("http://localhost/api/models", {
|
||||
body: JSON.stringify({
|
||||
capabilities: ["invalid-cap"],
|
||||
modelId: "test",
|
||||
externalId: "test",
|
||||
name: "Test",
|
||||
providerId,
|
||||
}),
|
||||
@@ -248,7 +248,7 @@ describe("models API routes", () => {
|
||||
body: JSON.stringify({
|
||||
capabilities: ["text"],
|
||||
contextLength: 0,
|
||||
modelId: "test",
|
||||
externalId: "test",
|
||||
name: "Test",
|
||||
providerId,
|
||||
}),
|
||||
@@ -274,7 +274,7 @@ describe("models API routes", () => {
|
||||
const providerId = seedProvider(db);
|
||||
|
||||
const req = new Request("http://localhost/api/models/test", {
|
||||
body: JSON.stringify({ modelId: "gpt-4o", providerId }),
|
||||
body: JSON.stringify({ externalId: "gpt-4o", providerId }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
@@ -289,7 +289,7 @@ describe("models API routes", () => {
|
||||
test("POST /api/models/test 缺少 providerId 返回 400", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const req = new Request("http://localhost/api/models/test", {
|
||||
body: JSON.stringify({ modelId: "gpt-4o" }),
|
||||
body: JSON.stringify({ externalId: "gpt-4o" }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
@@ -301,7 +301,7 @@ describe("models API routes", () => {
|
||||
test("POST /api/models/test 不存在的供应商返回 404", async () => {
|
||||
await withRouteDb(async (db) => {
|
||||
const req = new Request("http://localhost/api/models/test", {
|
||||
body: JSON.stringify({ modelId: "gpt-4o", providerId: "nonexistent" }),
|
||||
body: JSON.stringify({ externalId: "gpt-4o", providerId: "nonexistent" }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
@@ -236,7 +236,7 @@ describe("供应商 API 路由", () => {
|
||||
db,
|
||||
{
|
||||
capabilities: ["text"],
|
||||
modelId: "gpt-4o",
|
||||
externalId: "gpt-4o",
|
||||
name: "GPT-4o",
|
||||
providerId: provider.id,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user