Skip to content

fix(api): stop workspace import dropping every workflow variable - #6002

Merged
waleedlatif1 merged 1 commit into
stagingfrom
workflow-import-followup
Jul 28, 2026
Merged

fix(api): stop workspace import dropping every workflow variable#6002
waleedlatif1 merged 1 commit into
stagingfrom
workflow-import-followup

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #5999, which extracted the shared import helpers. This applies them to the last path that was still hand-rolling its own — and that path had a real data-loss bug.

  • Workspace import silently dropped every workflow variable. It guarded its variable write on Array.isArray, but parseWorkflowVariables — what the matching workspace export emits — returns the record form. So export a workspace, import it back, and all variables were gone. Restore path, no warning, no error. Now uses the shared normalizeImportedVariables, which accepts both shapes.
  • Workspace import also skipped the normalization pipeline that the editor, the v1 import API and the single-workflow admin import already share. It wrote raw parsed state, so a dangling edge tripped the workflow_edges foreign key and failed the restore, and blocks missing their backfilled columns could land unopenable — the same class feat(api): add workflow export and import endpoints to the public v1 API #5999 closed elsewhere. Now calls prepareWorkflowStateForPersistence.
  • Repointed persistImportedWorkflow (the in-app importer) at the same helper, removing the last inline copy. All four import paths now normalize variables identically.

Type of Change

  • Bug fix

Testing

First tests for these helpers — 15 cases covering both shapes, JSON-string column values, missing/padded/non-string ids, unrecognized type, a __proto__-keyed entry (built via JSON.parse, since an object literal's __proto__ sets the prototype instead), and the export → import round trip that pins the record form the bug dropped.

1273 tests pass across lib/workflows/, app/api/v1/workflows/ and app/api/workflows/. Typecheck, lint, check:api-validation:strict and the monorepo boundary check all pass.

Not exercised against a live server — no real workspace was exported and re-imported end to end.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

The workspace importer guarded its variable write on `Array.isArray`, but
`parseWorkflowVariables` — what the matching workspace export emits — returns
the record form. Every variable was silently dropped, so exporting a workspace
and importing it back lost all of them. It now uses the shared
`normalizeImportedVariables`, which accepts both shapes.

Also runs the workspace importer through `prepareWorkflowStateForPersistence`,
the pipeline the editor, the v1 import API and the single-workflow admin import
already share. Without it this route wrote raw parsed state, so a dangling edge
tripped the `workflow_edges` foreign key and failed the restore, and blocks
missing their backfilled columns could land unopenable.

Repoints `persistImportedWorkflow` at the same helper, removing the last
inline copy — all four import paths now normalize variables identically.

Adds the first tests for these helpers, including the export -> import round
trip that pins the record form the bug dropped.
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Jul 28, 2026 6:48am

Request Review

@cursor

cursor Bot commented Jul 28, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes bulk admin restore and shared import persistence; bugs here cause silent variable loss or failed imports, but scope is limited to import paths with new regression tests.

Overview
Fixes silent data loss and failed restores on admin workspace import by aligning that path with the shared import pipeline used elsewhere.

Workspace import now runs workflow state through prepareWorkflowStateForPersistence before saveWorkflowToNormalizedTables (avoids dangling-edge FK failures and unopenable blocks from missing backfilled columns). Variables are persisted via normalizeImportedVariables instead of an Array.isArray guard that dropped the record-shaped variables workspace export actually emits.

In-app import (persistImportedWorkflow) uses the same variable helper, removing duplicate inline normalization so all import paths behave consistently.

Adds Vitest coverage for parseWorkflowVariables / normalizeImportedVariables, including the export → import round trip for the record form.

Reviewed by Cursor Bugbot for commit d70825a. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes workspace import silently dropping workflow variables and skipping shared state normalization.

  • Admin workspace import now runs prepareWorkflowStateForPersistence before save (aligned with editor/v1/admin single-workflow import).
  • Variable persistence uses shared normalizeImportedVariables so both record and array export shapes survive restore.
  • In-app persistImportedWorkflow switches to the same helper; adds unit tests for parse/normalize and export→import round trip.

Confidence Score: 5/5

Safe to merge; the change closes a real silent data-loss path and matches established import pipelines without introducing a new failure mode.

Shared helpers already used by other import paths; prepare-state does not own variables, and normalizeImportedVariables accepts the record form exports emit while hardening types and ids.

Important Files Changed

Filename Overview
apps/sim/app/api/v1/admin/workspaces/[id]/import/route.ts Applies shared prepare-state and variable normalization so workspace restore keeps variables and valid graph state.
apps/sim/lib/workflows/operations/import-export.ts Replaces inline variable conversion in persistImportedWorkflow with normalizeImportedVariables.
apps/sim/lib/workflows/variables/parse.test.ts New tests cover both variable shapes, edge cases, and the export→import regression.

Reviews (1): Last reviewed commit: "fix(api): stop workspace import dropping..." | Re-trigger Greptile

@waleedlatif1
waleedlatif1 merged commit b148a52 into staging Jul 28, 2026
20 checks passed
@waleedlatif1
waleedlatif1 deleted the workflow-import-followup branch July 28, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant