Skip to content

Commit 47e723b

Browse files
authored
Bugfix/Query Runner already released (#3525)
Merge branch 'feature/add-couchbase-vectore-store' into feature/Couchbase
1 parent 1ccd976 commit 47e723b

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

packages/server/src/services/assistants/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Credential } from '../../database/entities/Credential'
77
import { decryptCredentialData, getAppVersion } from '../../utils'
88
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
99
import { getErrorMessage } from '../../errors/utils'
10-
import { DeleteResult } from 'typeorm'
10+
import { DeleteResult, QueryRunner } from 'typeorm'
1111
import { FLOWISE_METRIC_COUNTERS, FLOWISE_COUNTER_STATUS } from '../../Interface.Metrics'
1212

1313
const createAssistant = async (requestBody: any): Promise<Assistant> => {
@@ -291,9 +291,10 @@ const updateAssistant = async (assistantId: string, requestBody: any): Promise<A
291291
}
292292
}
293293

294-
const importAssistants = async (newAssistants: Partial<Assistant>[]): Promise<any> => {
294+
const importAssistants = async (newAssistants: Partial<Assistant>[], queryRunner?: QueryRunner): Promise<any> => {
295295
try {
296296
const appServer = getRunningExpressApp()
297+
const repository = queryRunner ? queryRunner.manager.getRepository(Assistant) : appServer.AppDataSource.getRepository(Assistant)
297298

298299
// step 1 - check whether array is zero
299300
if (newAssistants.length == 0) return
@@ -309,7 +310,7 @@ const importAssistants = async (newAssistants: Partial<Assistant>[]): Promise<an
309310
count += 1
310311
})
311312

312-
const selectResponse = await appServer.AppDataSource.getRepository(Assistant)
313+
const selectResponse = await repository
313314
.createQueryBuilder('assistant')
314315
.select('assistant.id')
315316
.where(`assistant.id IN ${ids}`)
@@ -329,7 +330,7 @@ const importAssistants = async (newAssistants: Partial<Assistant>[]): Promise<an
329330
})
330331

331332
// step 4 - transactional insert array of entities
332-
const insertResponse = await appServer.AppDataSource.getRepository(Assistant).insert(prepVariables)
333+
const insertResponse = await repository.insert(prepVariables)
333334

334335
return insertResponse
335336
} catch (error) {

packages/server/src/services/chatflows/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
1414
import { utilGetUploadsConfig } from '../../utils/getUploadsConfig'
1515
import logger from '../../utils/logger'
1616
import { FLOWISE_METRIC_COUNTERS, FLOWISE_COUNTER_STATUS } from '../../Interface.Metrics'
17+
import { QueryRunner } from 'typeorm'
1718

1819
// Check if chatflow valid for streaming
1920
const checkIfChatflowIsValidForStreaming = async (chatflowId: string): Promise<any> => {
@@ -206,9 +207,10 @@ const saveChatflow = async (newChatFlow: ChatFlow): Promise<any> => {
206207
}
207208
}
208209

209-
const importChatflows = async (newChatflows: Partial<ChatFlow>[]): Promise<any> => {
210+
const importChatflows = async (newChatflows: Partial<ChatFlow>[], queryRunner?: QueryRunner): Promise<any> => {
210211
try {
211212
const appServer = getRunningExpressApp()
213+
const repository = queryRunner ? queryRunner.manager.getRepository(ChatFlow) : appServer.AppDataSource.getRepository(ChatFlow)
212214

213215
// step 1 - check whether file chatflows array is zero
214216
if (newChatflows.length == 0) return
@@ -224,11 +226,7 @@ const importChatflows = async (newChatflows: Partial<ChatFlow>[]): Promise<any>
224226
count += 1
225227
})
226228

227-
const selectResponse = await appServer.AppDataSource.getRepository(ChatFlow)
228-
.createQueryBuilder('cf')
229-
.select('cf.id')
230-
.where(`cf.id IN ${ids}`)
231-
.getMany()
229+
const selectResponse = await repository.createQueryBuilder('cf').select('cf.id').where(`cf.id IN ${ids}`).getMany()
232230
const foundIds = selectResponse.map((response) => {
233231
return response.id
234232
})
@@ -248,7 +246,7 @@ const importChatflows = async (newChatflows: Partial<ChatFlow>[]): Promise<any>
248246
})
249247

250248
// step 4 - transactional insert array of entities
251-
const insertResponse = await appServer.AppDataSource.getRepository(ChatFlow).insert(prepChatflows)
249+
const insertResponse = await repository.insert(prepChatflows)
252250

253251
return insertResponse
254252
} catch (error) {

packages/server/src/services/export-import/index.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,22 @@ const importData = async (importData: ExportData) => {
8787
const queryRunner = appServer.AppDataSource.createQueryRunner()
8888

8989
try {
90-
queryRunner.startTransaction()
90+
await queryRunner.startTransaction()
9191

92-
// step 1 - importTools
93-
if (importData.Tool.length > 0) await toolsService.importTools(importData.Tool)
94-
// step 2 - importChatflows
95-
if (importData.ChatFlow.length > 0) await chatflowService.importChatflows(importData.ChatFlow)
96-
// step 3 - importAgentlows
97-
if (importData.AgentFlow.length > 0) await chatflowService.importChatflows(importData.AgentFlow)
98-
if (importData.Variable.length > 0) await variableService.importVariables(importData.Variable)
99-
if (importData.Assistant.length > 0) await assistantService.importAssistants(importData.Assistant)
100-
queryRunner.commitTransaction()
92+
if (importData.Tool.length > 0) await toolsService.importTools(importData.Tool, queryRunner)
93+
if (importData.ChatFlow.length > 0) await chatflowService.importChatflows(importData.ChatFlow, queryRunner)
94+
if (importData.AgentFlow.length > 0) await chatflowService.importChatflows(importData.AgentFlow, queryRunner)
95+
if (importData.Variable.length > 0) await variableService.importVariables(importData.Variable, queryRunner)
96+
if (importData.Assistant.length > 0) await assistantService.importAssistants(importData.Assistant, queryRunner)
97+
98+
await queryRunner.commitTransaction()
10199
} catch (error) {
102-
queryRunner.rollbackTransaction()
100+
await queryRunner.rollbackTransaction()
103101
throw error
104102
} finally {
105-
queryRunner.release()
103+
if (!queryRunner.isReleased) {
104+
await queryRunner.release()
105+
}
106106
}
107107
} catch (error) {
108108
throw new InternalFlowiseError(

packages/server/src/services/tools/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getErrorMessage } from '../../errors/utils'
55
import { getAppVersion } from '../../utils'
66
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
77
import { FLOWISE_METRIC_COUNTERS, FLOWISE_COUNTER_STATUS } from '../../Interface.Metrics'
8+
import { QueryRunner } from 'typeorm'
89

910
const createTool = async (requestBody: any): Promise<any> => {
1011
try {
@@ -81,9 +82,10 @@ const updateTool = async (toolId: string, toolBody: any): Promise<any> => {
8182
}
8283
}
8384

84-
const importTools = async (newTools: Partial<Tool>[]) => {
85+
const importTools = async (newTools: Partial<Tool>[], queryRunner?: QueryRunner) => {
8586
try {
8687
const appServer = getRunningExpressApp()
88+
const repository = queryRunner ? queryRunner.manager.getRepository(Tool) : appServer.AppDataSource.getRepository(Tool)
8789

8890
// step 1 - check whether file tools array is zero
8991
if (newTools.length == 0) return
@@ -99,11 +101,7 @@ const importTools = async (newTools: Partial<Tool>[]) => {
99101
count += 1
100102
})
101103

102-
const selectResponse = await appServer.AppDataSource.getRepository(Tool)
103-
.createQueryBuilder('t')
104-
.select('t.id')
105-
.where(`t.id IN ${ids}`)
106-
.getMany()
104+
const selectResponse = await repository.createQueryBuilder('t').select('t.id').where(`t.id IN ${ids}`).getMany()
107105
const foundIds = selectResponse.map((response) => {
108106
return response.id
109107
})
@@ -120,7 +118,7 @@ const importTools = async (newTools: Partial<Tool>[]) => {
120118
})
121119

122120
// step 4 - transactional insert array of entities
123-
const insertResponse = await appServer.AppDataSource.getRepository(Tool).insert(prepTools)
121+
const insertResponse = await repository.insert(prepTools)
124122

125123
return insertResponse
126124
} catch (error) {

packages/server/src/services/variables/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
33
import { Variable } from '../../database/entities/Variable'
44
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
55
import { getErrorMessage } from '../../errors/utils'
6+
import { QueryRunner } from 'typeorm'
67

78
const createVariable = async (newVariable: Variable) => {
89
try {
@@ -73,9 +74,10 @@ const updateVariable = async (variable: Variable, updatedVariable: Variable) =>
7374
}
7475
}
7576

76-
const importVariables = async (newVariables: Partial<Variable>[]): Promise<any> => {
77+
const importVariables = async (newVariables: Partial<Variable>[], queryRunner?: QueryRunner): Promise<any> => {
7778
try {
7879
const appServer = getRunningExpressApp()
80+
const repository = queryRunner ? queryRunner.manager.getRepository(Variable) : appServer.AppDataSource.getRepository(Variable)
7981

8082
// step 1 - check whether array is zero
8183
if (newVariables.length == 0) return
@@ -91,11 +93,7 @@ const importVariables = async (newVariables: Partial<Variable>[]): Promise<any>
9193
count += 1
9294
})
9395

94-
const selectResponse = await appServer.AppDataSource.getRepository(Variable)
95-
.createQueryBuilder('v')
96-
.select('v.id')
97-
.where(`v.id IN ${ids}`)
98-
.getMany()
96+
const selectResponse = await repository.createQueryBuilder('v').select('v.id').where(`v.id IN ${ids}`).getMany()
9997
const foundIds = selectResponse.map((response) => {
10098
return response.id
10199
})
@@ -112,7 +110,7 @@ const importVariables = async (newVariables: Partial<Variable>[]): Promise<any>
112110
})
113111

114112
// step 4 - transactional insert array of entities
115-
const insertResponse = await appServer.AppDataSource.getRepository(Variable).insert(prepVariables)
113+
const insertResponse = await repository.insert(prepVariables)
116114

117115
return insertResponse
118116
} catch (error) {

0 commit comments

Comments
 (0)