Skip to content

Commit c065eb7

Browse files
committed
moved doc update inside tx for embeddings chunks upload
1 parent 16c5515 commit c065eb7

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

apps/sim/app/api/knowledge/utils.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ vi.mock('@sim/db', () => {
139139
delete: () => ({
140140
where: () => Promise.resolve(),
141141
}),
142-
insert: (table: any) => ({
142+
insert: () => ({
143143
values: (records: any) => {
144144
dbOps.order.push('insert')
145145
dbOps.insertRecords.push(records)
@@ -151,14 +151,14 @@ vi.mock('@sim/db', () => {
151151
delete: () => ({
152152
where: () => Promise.resolve(),
153153
}),
154-
insert: (table: any) => ({
154+
insert: () => ({
155155
values: (records: any) => {
156156
dbOps.order.push('insert')
157157
dbOps.insertRecords.push(records)
158158
return Promise.resolve()
159159
},
160160
}),
161-
update: (table: any) => ({
161+
update: () => ({
162162
set: (payload: any) => ({
163163
where: () => {
164164
dbOps.updatePayloads.push(payload)

apps/sim/lib/knowledge/documents/service.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ export async function processDocumentAsync(
552552
updatedAt: now,
553553
}))
554554

555-
if (embeddingRecords.length > 0) {
556-
await db.transaction(async (tx) => {
555+
await db.transaction(async (tx) => {
556+
if (embeddingRecords.length > 0) {
557557
await tx.delete(embedding).where(eq(embedding.documentId, documentId))
558558

559559
const insertBatchSize = LARGE_DOC_CONFIG.MAX_CHUNKS_PER_BATCH
@@ -564,20 +564,20 @@ export async function processDocumentAsync(
564564

565565
logger.info(`[${documentId}] Inserting ${embeddingRecords.length} embeddings`)
566566
await Promise.all(batches.map((batch) => tx.insert(embedding).values(batch)))
567-
})
568-
}
567+
}
569568

570-
await db
571-
.update(document)
572-
.set({
573-
chunkCount: processed.metadata.chunkCount,
574-
tokenCount: processed.metadata.tokenCount,
575-
characterCount: processed.metadata.characterCount,
576-
processingStatus: 'completed',
577-
processingCompletedAt: now,
578-
processingError: null,
579-
})
580-
.where(eq(document.id, documentId))
569+
await tx
570+
.update(document)
571+
.set({
572+
chunkCount: processed.metadata.chunkCount,
573+
tokenCount: processed.metadata.tokenCount,
574+
characterCount: processed.metadata.characterCount,
575+
processingStatus: 'completed',
576+
processingCompletedAt: now,
577+
processingError: null,
578+
})
579+
.where(eq(document.id, documentId))
580+
})
581581
})(),
582582
TIMEOUTS.OVERALL_PROCESSING,
583583
'Document processing'

0 commit comments

Comments
 (0)