Skip to content

Commit b67b0f1

Browse files
icecrasher321Vikhyath Mondreti
andauthored
fix(deployState): deploy not persisting bug (#518)
* fix(undeploy-bug): fix deployment persistence failing bug * fix lint --------- Co-authored-by: Vikhyath Mondreti <vikhyathmondreti@Vikhyaths-MacBook-Air.local>
1 parent e81afa5 commit b67b0f1

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

apps/sim/app/api/workflows/sync/route.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ export async function POST(req: NextRequest) {
428428
color: clientWorkflow.color,
429429
state: stateToSave,
430430
marketplaceData: clientWorkflow.marketplaceData || null,
431+
// Include deployment status from client state
432+
isDeployed: clientWorkflow.state.isDeployed || false,
433+
deployedAt: clientWorkflow.state.deployedAt || null,
434+
// deployedState is null for new workflows - only set during deployment
435+
deployedState: null,
431436
lastSynced: now,
432437
createdAt: now,
433438
updatedAt: now,
@@ -485,6 +490,32 @@ export async function POST(req: NextRequest) {
485490
needsUpdate = true
486491
}
487492

493+
// Check deployment status changes - ensure dual-write consistency
494+
const clientIsDeployed = clientWorkflow.state.isDeployed || false
495+
const clientDeployedAt = clientWorkflow.state.deployedAt || null
496+
497+
if (dbWorkflow.isDeployed !== clientIsDeployed) {
498+
updateData.isDeployed = clientIsDeployed
499+
needsUpdate = true
500+
}
501+
502+
// Handle deployedAt comparison (convert to comparable format)
503+
const dbDeployedAtTime = dbWorkflow.deployedAt
504+
? new Date(dbWorkflow.deployedAt).getTime()
505+
: null
506+
const clientDeployedAtTime = clientDeployedAt
507+
? new Date(clientDeployedAt).getTime()
508+
: null
509+
510+
if (dbDeployedAtTime !== clientDeployedAtTime) {
511+
updateData.deployedAt = clientDeployedAt
512+
needsUpdate = true
513+
}
514+
515+
// CRITICAL: Always preserve deployedState - it should only be modified by deploy/undeploy operations
516+
// The sync operation should never touch deployedState to prevent data loss
517+
// Note: We don't add deployedState to updateData because we want to preserve the existing value
518+
488519
if (needsUpdate) {
489520
updateData.lastSynced = now
490521
updateData.updatedAt = now

apps/sim/stores/workflows/registry/store.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,21 @@ export const useWorkflowRegistry = create<WorkflowRegistry>()(
630630
// Set the workflow state in the store
631631
useWorkflowStore.setState(workflowState)
632632

633+
// CRITICAL: Set deployment status in registry when switching to workflow
634+
if (workflowData?.isDeployed || workflowData?.deployedAt) {
635+
set((state) => ({
636+
deploymentStatuses: {
637+
...state.deploymentStatuses,
638+
[id]: {
639+
isDeployed: workflowData.isDeployed || false,
640+
deployedAt: workflowData.deployedAt ? new Date(workflowData.deployedAt) : undefined,
641+
apiKey: workflowData.apiKey || undefined,
642+
needsRedeployment: false, // Default to false when loading from DB
643+
},
644+
},
645+
}))
646+
}
647+
633648
// Update the active workflow ID
634649
set({ activeWorkflowId: id, error: null })
635650

apps/sim/stores/workflows/sync.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export async function fetchWorkflowsFromDB(): Promise<void> {
198198

199199
// Process workflows
200200
const registryWorkflows: Record<string, WorkflowMetadata> = {}
201+
const deploymentStatuses: Record<string, any> = {}
201202

202203
data.forEach((workflow) => {
203204
const {
@@ -210,6 +211,9 @@ export async function fetchWorkflowsFromDB(): Promise<void> {
210211
marketplaceData,
211212
workspaceId,
212213
folderId,
214+
isDeployed,
215+
deployedAt,
216+
apiKey,
213217
} = workflow
214218

215219
// Skip if workflow doesn't belong to active workspace
@@ -229,6 +233,16 @@ export async function fetchWorkflowsFromDB(): Promise<void> {
229233
folderId: folderId || null,
230234
}
231235

236+
// CRITICAL: Extract deployment status from database and add to registry
237+
if (isDeployed || deployedAt) {
238+
deploymentStatuses[id] = {
239+
isDeployed: isDeployed || false,
240+
deployedAt: deployedAt ? new Date(deployedAt) : undefined,
241+
apiKey: apiKey || undefined,
242+
needsRedeployment: false, // Default to false when loading from DB
243+
}
244+
}
245+
232246
// Initialize subblock values
233247
const subblockValues: Record<string, Record<string, any>> = {}
234248
if (state?.blocks) {
@@ -251,9 +265,10 @@ export async function fetchWorkflowsFromDB(): Promise<void> {
251265
}))
252266
})
253267

254-
// Update registry with loaded workflows
268+
// Update registry with loaded workflows and deployment statuses
255269
useWorkflowRegistry.setState({
256270
workflows: registryWorkflows,
271+
deploymentStatuses: deploymentStatuses,
257272
isLoading: false,
258273
error: null,
259274
})

0 commit comments

Comments
 (0)