Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: delete files should delete chunks、embedings、fileChunk (#9196)

Shinji-Li4ee1d292fffc
diff --git a/packages/database/src/models/__tests__/file.test.ts b/packages/database/src/models/__tests__/file.test.ts
index 075df0230cd..91a1582f738 100644
--- a/packages/database/src/models/__tests__/file.test.ts
+++ b/packages/database/src/models/__tests__/file.test.ts
@@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { FilesTabs, SortType } from '@/types/files';
-import { files, globalFiles, knowledgeBaseFiles, knowledgeBases, users } from '../../schemas';
+import { chunks, embeddings, fileChunks, files, globalFiles, knowledgeBaseFiles, knowledgeBases, users } from '../../schemas';
import { LobeChatDatabase } from '../../type';
import { FileModel } from '../file';
import { getTestDB } from './_util';
@@ -1058,15 +1058,8 @@ describe('FileModel', () => {
// Create many chunks for this file to trigger batch processing
// Note: This is a simplified test since we can't easily create 3000+ chunks
// But it will still exercise the batch deletion code path
- const chunkData = Array.from({ length: 10 }, (_, i) => ({
- id: `chunk-${i}`,
- text: `chunk content ${i}`,
- index: i,
- type: 'text' as const,
- userId,
- }));
- // Insert chunks (this might need to be done through proper API)
+ // Insert chunks (this might need to be done through proper API)
// For testing purposes, we'll delete the file which should trigger the batch deletion
await fileModel.delete(fileId, true);
@@ -1077,4 +1070,205 @@ describe('FileModel', () => {
expect(deletedFile).toBeUndefined();
});
});
+
+ describe('deleteFileChunks error handling', () => {
+ let consoleWarnSpy: any;
+
+ beforeEach(() => {
+ consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ consoleWarnSpy.mockRestore();
+ });
+
+ it('should delete file even when chunks deletion fails', async () => {
+ // 创建测试文件
+ const testFile = {
+ name: 'error-test-file.txt',
+ url: 'https://example.com/error-test-file.txt',
+ size: 100,
+ fileType: 'text/plain',
+ fileHash: 'error-test-hash',
+ };
+
+ const { id: fileId } = await fileModel.create(testFile, true);
+
+ // 创建一些测试数据来模拟chunks关联
+ const chunkId1 = '550e8400-e29b-41d4-a716-446655440001';
+ const chunkId2 = '550e8400-e29b-41d4-a716-446655440002';
+
+ // 插入chunks
+ await serverDB.insert(chunks).values([
+ { id: chunkId1, text: 'chunk 1', userId, type: 'text' },
+ { id: chunkId2, text: 'chunk 2', userId, type: 'text' },
+ ]);
+
+ // 插入fileChunks关联
+ await serverDB.insert(fileChunks).values([
+ { fileId, chunkId: chunkId1, userId },
+ { fileId, chunkId: chunkId2, userId },
+ ]);
+
+ // 插入embeddings (1024维向量)
+ const testEmbedding = new Array(1024).fill(0.1);
+ await serverDB.insert(embeddings).values([
+ { chunkId: chunkId1, embeddings: testEmbedding, model: 'test-model', userId },
+ ]);
+
+ // 跳过 documentChunks 测试,因为需要先创建 documents 记录
+
+ // 删除文件,应该会清理所有相关数据
+ const result = await fileModel.delete(fileId, true);
+
+ // 验证文件被删除
+ const deletedFile = await serverDB.query.files.findFirst({
+ where: eq(files.id, fileId),
+ });
+ expect(deletedFile).toBeUndefined();
+
+ // 验证chunks被删除
+ const remainingChunks = await serverDB.query.chunks.findMany({
+ where: inArray(chunks.id, [chunkId1, chunkId2]),
+ });
+ expect(remainingChunks).toHaveLength(0);
+
+ // 验证embeddings被删除
+ const remainingEmbeddings = await serverDB.query.embeddings.findMany({
+ where: inArray(embeddings.chunkId, [chunkId1, chunkId2]),
+ });
+ expect(remainingEmbeddings).toHaveLength(0);
+
+ // 验证fileChunks被删除
+ const remainingFileChunks = await serverDB.query.fileChunks.findMany({
+ where: eq(fileChunks.fileId, fileId),
+ });
+ expect(remainingFileChunks).toHaveLength(0);
+
+ expect(result).toBeDefined();
+ });
+
+ it('should successfully delete file with all related chunks and embeddings', async () => {
+ // 简化测试:只验证正常的完整删除流程(移除知识库保护后)
+ const testFile = {
+ name: 'complete-deletion-test.txt',
+ url: 'https://example.com/complete-deletion-test.txt',
+ size: 100,
+ fileType: 'text/plain',
+ fileHash: 'complete-deletion-hash',
+ };
+
+ const { id: fileId } = await fileModel.create(testFile, true);
+
+ const chunkId = '550e8400-e29b-41d4-a716-446655440003';
+
+ // 插入chunk
+ await serverDB.insert(chunks).values([
+ { id: chunkId, text: 'complete test chunk', userId, type: 'text' },
+ ]);
+
+ // 插入fileChunks关联
+ await serverDB.insert(fileChunks).values([
+ { fileId, chunkId, userId },
+ ]);
+
+ // 插入embeddings
+ const testEmbedding = new Array(1024).fill(0.1);
+ await serverDB.insert(embeddings).values([
+ { chunkId, embeddings: testEmbedding, model: 'test-model', userId },
+ ]);
+
+ // 删除文件
+ await fileModel.delete(fileId, true);
+
+ // 验证文件被删除
+ const deletedFile = await serverDB.query.files.findFirst({
+ where: eq(files.id, fileId),
+ });
+ expect(deletedFile).toBeUndefined();
+
+ // 验证chunks被删除
+ const remainingChunks = await serverDB.query.chunks.findMany({
+ where: eq(chunks.id, chunkId),
+ });
+ expect(remainingChunks).toHaveLength(0);
+
+ // 验证embeddings被删除
+ const remainingEmbeddings = await serverDB.query.embeddings.findMany({
+ where: eq(embeddings.chunkId, chunkId),
+ });
+ expect(remainingEmbeddings).toHaveLength(0);
+
+ // 验证fileChunks被删除
+ const remainingFileChunks = await serverDB.query.fileChunks.findMany({
+ where: eq(fileChunks.fileId, fileId),
+ });
+ expect(remainingFileChunks).toHaveLength(0);
+ });
+
+
+ it('should delete files that are in knowledge bases (removed protection)', async () => {
+ // 测试修复后的逻辑:知识库中的文件也应该被删除
+ const testFile = {
+ name: 'knowledge-base-file.txt',
+ url: 'https://example.com/knowledge-base-file.txt',
+ size: 100,
+ fileType: 'text/plain',
+ fileHash: 'kb-file-hash',
+ knowledgeBaseId: 'kb1',
+ };
+
+ const { id: fileId } = await fileModel.create(testFile, true);
+
+ const chunkId = '550e8400-e29b-41d4-a716-446655440007';
+
+ // 插入chunk和关联数据
+ await serverDB.insert(chunks).values([
+ { id: chunkId, text: 'knowledge base chunk', userId, type: 'text' },
+ ]);
+
+ await serverDB.insert(fileChunks).values([
+ { fileId, chunkId, userId },
+ ]);
+
+ // 插入embeddings (1024维向量)
+ const testEmbedding = new Array(1024).fill(0.1);
+ await serverDB.insert(embeddings).values([
+ { chunkId, embeddings: testEmbedding, model: 'test-model', userId },
+ ]);
+
+ // 验证文件确实在知识库中
+ const kbFile = await serverDB.query.knowledgeBaseFiles.findFirst({
+ where: eq(knowledgeBaseFiles.fileId, fileId),
+ });
+ expect(kbFile).toBeDefined();
+
+ // 删除文件
+ await fileModel.delete(fileId, true);
+
+ // 验证知识库中的文件也被完全删除
+ const deletedFile = await serverDB.query.files.findFirst({
+ where: eq(files.id, fileId),
+ });
+ expect(deletedFile).toBeUndefined();
+
+ // 验证chunks被删除(这是修复的核心:之前知识库文件的chunks不会被删除)
+ const remainingChunks = await serverDB.query.chunks.findMany({
+ where: eq(chunks.id, chunkId),
+ });
+ expect(remainingChunks).toHaveLength(0);
+
+ // 验证embeddings被删除
+ const remainingEmbeddings = await serverDB.query.embeddings.findMany({
+ where: eq(embeddings.chunkId, chunkId),
+ });
+ expect(remainingEmbeddings).toHaveLength(0);
+
+ // 验证fileChunks被删除
+ const remainingFileChunks = await serverDB.query.fileChunks.findMany({
+ where: eq(fileChunks.fileId, fileId),
+ });
+ expect(remainingFileChunks).toHaveLength(0);
+ });
+ });
});
diff --git a/packages/database/src/models/file.ts b/packages/database/src/models/file.ts
index 7463db53aa2..5c7bcea3156 100644
--- a/packages/database/src/models/file.ts
+++ b/packages/database/src/models/file.ts
@@ -1,7 +1,6 @@
import { and, asc, count, desc, eq, ilike, inArray, like, notExists, or, sum } from 'drizzle-orm';
import type { PgTransaction } from 'drizzle-orm/pg-core';
-import { LobeChatDatabase, Transaction } from '../type';
import { FilesTabs, QueryFileListParams, SortType } from '@/types/files';
import {
@@ -9,12 +8,14 @@ import {
NewFile,
NewGlobalFile,
chunks,
+ documentChunks,
embeddings,
fileChunks,
files,
globalFiles,
knowledgeBaseFiles,
} from '../schemas';
+import { LobeChatDatabase, Transaction } from '../type';
export class FileModel {
private readonly userId: string;
@@ -326,30 +327,19 @@ export class FileModel {
private deleteFileChunks = async (trx: PgTransaction<any>, fileIds: string[]) => {
if (fileIds.length === 0) return;
- // 直接使用 JOIN 优化查询,减少数据传输量
+ // 获取要删除的文件相关的所有 chunk IDs(移除知识库保护逻辑)
const relatedChunks = await trx
.select({ chunkId: fileChunks.chunkId })
.from(fileChunks)
- .where(
- and(
- inArray(fileChunks.fileId, fileIds),
- // 确保只查询有效的 chunkId
- notExists(
- trx
- .select()
- .from(knowledgeBaseFiles)
- .where(eq(knowledgeBaseFiles.fileId, fileChunks.fileId)),
- ),
- ),
- );
+ .where(inArray(fileChunks.fileId, fileIds));
const chunkIds = relatedChunks.map((c) => c.chunkId).filter(Boolean) as string[];
if (chunkIds.length === 0) return;
// 批量处理配置
- const BATCH_SIZE = 1000; // 增加批处理量
- const MAX_CONCURRENT_BATCHES = 3; // 最大并行批次数
+ const BATCH_SIZE = 1000;
+ const MAX_CONCURRENT_BATCHES = 3;
// 分批并行处理
for (let i = 0; i < chunkIds.length; i += BATCH_SIZE * MAX_CONCURRENT_BATCHES) {
@@ -363,12 +353,31 @@ export class FileModel {
const batchChunkIds = chunkIds.slice(startIdx, startIdx + BATCH_SIZE);
if (batchChunkIds.length === 0) continue;
- // 为每个批次创建一个删除任务
+ // 按正确的删除顺序处理每个批次,失败不阻止流程
const batchPromise = (async () => {
- // 先删除嵌入向量
- await trx.delete(embeddings).where(inArray(embeddings.chunkId, batchChunkIds));
- // 再删除块
- await trx.delete(chunks).where(inArray(chunks.id, batchChunkIds));
+ // 1. 删除 embeddings (最顶层,有外键依赖)
+ try {
+ await trx.delete(embeddings).where(inArray(embeddings.chunkId, batchChunkIds));
+ } catch (e) {
+ // 静默处理,不阻止删除流程
+ console.warn('Failed to delete embeddings:', e);
+ }
+
+ // 2. 删除 documentChunks 关联 (如果存在)
+ try {
+ await trx.delete(documentChunks).where(inArray(documentChunks.chunkId, batchChunkIds));
+ } catch (e) {
+ // 静默处理,不阻止删除流程
+ console.warn('Failed to delete documentChunks:', e);
+ }
+
+ // 3. 删除 chunks (核心数据)
+ try {
+ await trx.delete(chunks).where(inArray(chunks.id, batchChunkIds));
+ } catch (e) {
+ // 静默处理,不阻止删除流程
+ console.warn('Failed to delete chunks:', e);
+ }
})();
batchPromises.push(batchPromise);
@@ -378,6 +387,14 @@ export class FileModel {
await Promise.all(batchPromises);
}
+ // 4. 最后删除 fileChunks 关联表记录
+ try {
+ await trx.delete(fileChunks).where(inArray(fileChunks.fileId, fileIds));
+ } catch (e) {
+ // 静默处理,不阻止删除流程
+ console.warn('Failed to delete fileChunks:', e);
+ }
+
return chunkIds;
};
}