Skip to content

Commit 9e92f35

Browse files
committed
address bugbot
1 parent 839d105 commit 9e92f35

File tree

1 file changed

+19
-14
lines changed
  • apps/sim/lib/workflows/persistence

1 file changed

+19
-14
lines changed

apps/sim/lib/workflows/persistence/utils.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ export async function loadDeployedWorkflowState(workflowId: string): Promise<Dep
106106
.where(eq(workflow.id, workflowId))
107107
.limit(1)
108108

109-
const { blocks: migratedBlocks } = await migrateCredentialIds(
110-
state.blocks || {},
111-
wfRow?.workspaceId ?? undefined
112-
)
109+
const resolvedBlocks = state.blocks || {}
110+
const { blocks: migratedBlocks } = wfRow?.workspaceId
111+
? await migrateCredentialIds(resolvedBlocks, wfRow.workspaceId)
112+
: { blocks: resolvedBlocks }
113113

114114
return {
115115
blocks: migratedBlocks,
@@ -206,7 +206,7 @@ const CREDENTIAL_SUBBLOCK_IDS = new Set(['credential', 'triggerCredentials'])
206206
*/
207207
async function migrateCredentialIds(
208208
blocks: Record<string, BlockState>,
209-
workspaceId?: string
209+
workspaceId: string
210210
): Promise<{ blocks: Record<string, BlockState>; migrated: boolean }> {
211211
const potentialLegacyIds = new Set<string>()
212212

@@ -237,15 +237,15 @@ async function migrateCredentialIds(
237237
return { blocks, migrated: false }
238238
}
239239

240-
const conditions = [inArray(credential.accountId, [...potentialLegacyIds])]
241-
if (workspaceId) {
242-
conditions.push(eq(credential.workspaceId, workspaceId))
243-
}
244-
245240
const rows = await db
246241
.select({ id: credential.id, accountId: credential.accountId })
247242
.from(credential)
248-
.where(and(...conditions))
243+
.where(
244+
and(
245+
inArray(credential.accountId, [...potentialLegacyIds]),
246+
eq(credential.workspaceId, workspaceId)
247+
)
248+
)
249249

250250
if (rows.length === 0) {
251251
return { blocks, migrated: false }
@@ -291,7 +291,11 @@ async function migrateCredentialIds(
291291
})
292292
)
293293

294-
return { blocks: migratedBlocks, migrated: true }
294+
const anyBlockChanged = Object.keys(migratedBlocks).some(
295+
(id) => migratedBlocks[id] !== blocks[id]
296+
)
297+
298+
return { blocks: migratedBlocks, migrated: anyBlockChanged }
295299
}
296300

297301
/**
@@ -352,8 +356,9 @@ export async function loadWorkflowFromNormalizedTables(
352356
const migratedBlocks = migrateAgentBlocksToMessagesFormat(sanitizedBlocks)
353357

354358
// Migrate legacy account.id → credential.id in OAuth subblocks
355-
const { blocks: credMigratedBlocks, migrated: credentialsMigrated } =
356-
await migrateCredentialIds(migratedBlocks, workflowRow?.workspaceId ?? undefined)
359+
const { blocks: credMigratedBlocks, migrated: credentialsMigrated } = workflowRow?.workspaceId
360+
? await migrateCredentialIds(migratedBlocks, workflowRow.workspaceId)
361+
: { blocks: migratedBlocks, migrated: false }
357362

358363
if (credentialsMigrated) {
359364
Promise.resolve().then(async () => {

0 commit comments

Comments
 (0)