Skip to content

Commit 1bed203

Browse files
author
Vikhyath Mondreti
committed
fix(search): preserve ordinary knowledge base classification
1 parent 6360f9e commit 1bed203

2 files changed

Lines changed: 30 additions & 24 deletions

File tree

‎apps/sim/lib/knowledge/__integration__/search-source-setup.integration.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe('Search source identity and concurrent creation', () => {
236236
).toHaveLength(0)
237237
})
238238

239-
it('adopts a legacy index only during admin setup and persists its canonical identity', async () => {
239+
it('preserves ordinary KB name collisions and creates one explicit Search configuration', async () => {
240240
await db
241241
.update(knowledgeBase)
242242
.set({ name: 'Sim Search' })
@@ -258,18 +258,40 @@ describe('Search source identity and concurrent creation', () => {
258258
principal: { kind: 'session', userId: other.aliceId, sessionId: 'fixture-admin' },
259259
input,
260260
})
261-
const results = await Promise.all([prepare(), prepare()])
262-
expect(results.map((result) => result.knowledgeBaseId)).toEqual([
263-
other.knowledgeBaseId,
264-
other.knowledgeBaseId,
265-
])
261+
await expect(prepare()).rejects.toMatchObject({ code: 'conflict' })
262+
expect(
263+
await db
264+
.select({ name: knowledgeBase.name, isSearchIndex: knowledgeBase.isSearchIndex })
265+
.from(knowledgeBase)
266+
.where(eq(knowledgeBase.id, other.knowledgeBaseId))
267+
).toEqual([{ name: 'Sim Search', isSearchIndex: false }])
266268
await db
267269
.update(knowledgeBase)
268-
.set({ name: 'Renamed adopted index' })
270+
.set({ name: 'Ordinary knowledge' })
269271
.where(eq(knowledgeBase.id, other.knowledgeBaseId))
272+
const results = await Promise.all([prepare(), prepare()])
273+
const searchId = results[0].knowledgeBaseId
274+
expect(searchId).not.toBe(other.knowledgeBaseId)
275+
expect(results[1].knowledgeBaseId).toBe(searchId)
276+
expect(
277+
await db
278+
.select({ id: knowledgeBase.id })
279+
.from(knowledgeBase)
280+
.where(
281+
and(
282+
eq(knowledgeBase.workspaceId, other.workspaceId),
283+
eq(knowledgeBase.isSearchIndex, true)
284+
)
285+
)
286+
).toEqual([{ id: searchId }])
287+
await db
288+
.update(knowledgeBase)
289+
.set({ name: 'Renamed Search configuration' })
290+
.where(eq(knowledgeBase.id, searchId))
270291
await expect(readSearchIndex.execute({ principal: reader, input })).resolves.toMatchObject({
271-
knowledgeBaseId: other.knowledgeBaseId,
292+
knowledgeBaseId: searchId,
272293
})
294+
await expect(prepare()).resolves.toMatchObject({ knowledgeBaseId: searchId })
273295
})
274296

275297
it('serializes matching creates across independent database transactions and keeps one grant', async () => {

‎apps/sim/lib/knowledge/application/sim-search.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { type Principal, resolvePrincipalSubjectUserId } from '@sim/auth/principal'
2-
import { db } from '@sim/db'
3-
import { knowledgeBase } from '@sim/db/schema'
4-
import { and, eq, isNull } from 'drizzle-orm'
52
import { coalesceLocally } from '@/lib/concurrency/singleflight'
63
import { requireOrganizationMembership } from '@/lib/core/application/organization-authorization'
74
import {
@@ -84,19 +81,6 @@ async function ensureSearchKnowledgeBase(
8481
return created.id
8582
}
8683
const workspaceId = scope.workspaceId
87-
const [legacy] = await db
88-
.update(knowledgeBase)
89-
.set({ isSearchIndex: true, updatedAt: new Date() })
90-
.where(
91-
and(
92-
eq(knowledgeBase.workspaceId, workspaceId),
93-
eq(knowledgeBase.name, SIM_SEARCH_KNOWLEDGE_BASE_NAME),
94-
eq(knowledgeBase.isSearchIndex, false),
95-
isNull(knowledgeBase.deletedAt)
96-
)
97-
)
98-
.returning({ id: knowledgeBase.id })
99-
if (legacy) return legacy.id
10084
const created = await createKnowledgeBase.execute({
10185
principal,
10286
input: {

0 commit comments

Comments
 (0)