Skip to content

Commit ff99fbe

Browse files
committed
fix migration func
1 parent a67aa3d commit ff99fbe

File tree

2 files changed

+40
-22
lines changed
  • apps/sim

2 files changed

+40
-22
lines changed

apps/sim/app/api/workflows/[id]/deployed/route.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { db, workflowDeploymentVersion } from '@sim/db'
21
import { createLogger } from '@sim/logger'
3-
import { and, desc, eq } from 'drizzle-orm'
42
import type { NextRequest, NextResponse } from 'next/server'
53
import { verifyInternalToken } from '@/lib/auth/internal'
64
import { generateRequestId } from '@/lib/core/utils/request'
5+
import { loadDeployedWorkflowState } from '@/lib/workflows/persistence/utils'
76
import { validateWorkflowPermissions } from '@/lib/workflows/utils'
87
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'
98

@@ -43,21 +42,21 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
4342
logger.debug(`[${requestId}] Internal API call for deployed workflow: ${id}`)
4443
}
4544

46-
const [active] = await db
47-
.select({ state: workflowDeploymentVersion.state })
48-
.from(workflowDeploymentVersion)
49-
.where(
50-
and(
51-
eq(workflowDeploymentVersion.workflowId, id),
52-
eq(workflowDeploymentVersion.isActive, true)
53-
)
54-
)
55-
.orderBy(desc(workflowDeploymentVersion.createdAt))
56-
.limit(1)
45+
let deployedState = null
46+
try {
47+
const data = await loadDeployedWorkflowState(id)
48+
deployedState = {
49+
blocks: data.blocks,
50+
edges: data.edges,
51+
loops: data.loops,
52+
parallels: data.parallels,
53+
variables: data.variables,
54+
}
55+
} catch {
56+
deployedState = null
57+
}
5758

58-
const response = createSuccessResponse({
59-
deployedState: active?.state || null,
60-
})
59+
const response = createSuccessResponse({ deployedState })
6160
return addNoCacheHeaders(response)
6261
} catch (error: any) {
6362
logger.error(`[${requestId}] Error fetching deployed state: ${id}`, error)

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,16 @@ export async function loadDeployedWorkflowState(workflowId: string): Promise<Dep
100100

101101
const state = active.state as WorkflowState & { variables?: Record<string, unknown> }
102102

103-
const { blocks: migratedBlocks } = await migrateCredentialIds(state.blocks || {})
103+
const [wfRow] = await db
104+
.select({ workspaceId: workflow.workspaceId })
105+
.from(workflow)
106+
.where(eq(workflow.id, workflowId))
107+
.limit(1)
108+
109+
const { blocks: migratedBlocks } = await migrateCredentialIds(
110+
state.blocks || {},
111+
wfRow?.workspaceId ?? undefined
112+
)
104113

105114
return {
106115
blocks: migratedBlocks,
@@ -196,7 +205,8 @@ const CREDENTIAL_SUBBLOCK_IDS = new Set(['credential', 'triggerCredentials'])
196205
* Also migrates `tool.params.credential` in agent block tool arrays.
197206
*/
198207
async function migrateCredentialIds(
199-
blocks: Record<string, BlockState>
208+
blocks: Record<string, BlockState>,
209+
workspaceId?: string
200210
): Promise<{ blocks: Record<string, BlockState>; migrated: boolean }> {
201211
const potentialLegacyIds = new Set<string>()
202212

@@ -227,10 +237,15 @@ async function migrateCredentialIds(
227237
return { blocks, migrated: false }
228238
}
229239

240+
const conditions = [inArray(credential.accountId, [...potentialLegacyIds])]
241+
if (workspaceId) {
242+
conditions.push(eq(credential.workspaceId, workspaceId))
243+
}
244+
230245
const rows = await db
231246
.select({ id: credential.id, accountId: credential.accountId })
232247
.from(credential)
233-
.where(inArray(credential.accountId, [...potentialLegacyIds]))
248+
.where(and(...conditions))
234249

235250
if (rows.length === 0) {
236251
return { blocks, migrated: false }
@@ -287,11 +302,15 @@ export async function loadWorkflowFromNormalizedTables(
287302
workflowId: string
288303
): Promise<NormalizedWorkflowData | null> {
289304
try {
290-
// Load all components in parallel
291-
const [blocks, edges, subflows] = await Promise.all([
305+
const [blocks, edges, subflows, [workflowRow]] = await Promise.all([
292306
db.select().from(workflowBlocks).where(eq(workflowBlocks.workflowId, workflowId)),
293307
db.select().from(workflowEdges).where(eq(workflowEdges.workflowId, workflowId)),
294308
db.select().from(workflowSubflows).where(eq(workflowSubflows.workflowId, workflowId)),
309+
db
310+
.select({ workspaceId: workflow.workspaceId })
311+
.from(workflow)
312+
.where(eq(workflow.id, workflowId))
313+
.limit(1),
295314
])
296315

297316
// If no blocks found, assume this workflow hasn't been migrated yet
@@ -334,7 +353,7 @@ export async function loadWorkflowFromNormalizedTables(
334353

335354
// Migrate legacy account.id → credential.id in OAuth subblocks
336355
const { blocks: credMigratedBlocks, migrated: credentialsMigrated } =
337-
await migrateCredentialIds(migratedBlocks)
356+
await migrateCredentialIds(migratedBlocks, workflowRow?.workspaceId ?? undefined)
338357

339358
if (credentialsMigrated) {
340359
Promise.resolve().then(async () => {

0 commit comments

Comments
 (0)