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

@@ -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",
});