@@ -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 */
198207async 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