Files
twenty/packages
Thomas Trompette 0379b537dd feat(workflow): repair orphan core workflow versions via upgrade command (#23739)
## Context

Part of the workflow + workflowVersion in core migration. Before
enabling `IS_WORKFLOW_VERSION_IN_CORE_ENABLED` on real workspaces,
legacy drift between the workspace source-of-truth and the core mirror
must be repaired. The dangerous category is orphan **ACTIVE** core
`workflowVersion` rows: once dispatch reads core (later step), they
become phantom triggers.

## The problem

Some workspaces carry orphan `core."workflowVersion"` rows: core
versions that no workspace version references via
`coreWorkflowVersionId`. On one production workspace this was 57 rows, 5
of them ACTIVE `DATABASE_EVENT`.

Root cause is pre-2.25 residue:
- The v2.22 `backfill-workflow-version-core-links` command minted a core
row per active workspace version, copying `status` verbatim.
- The workflow delete/destroy cascade did not clean core until #23356
(first in v2.25.0): there was no `deleteCoreVersionsByWorkflowIds` and
the deactivate-on-delete status flip was not mirrored.
- Workflows deleted then destroyed in that window left their core rows
behind, still ACTIVE, with every workspace referrer gone.

Current code (>= v2.25) cleans core transactionally on delete/destroy,
so this cannot recur. This command clears the historical residue.

## What this does

A `@RegisteredWorkspaceCommand('2.28.0')` that, per provisioned
workspace:
- Deletes `core."workflowVersion"` rows with no workspace referrer, then
`invalidateAndRecompute`s the automated trigger map.
- `NOT EXISTS` does not filter `deletedAt`, so a soft-deleted
(restorable) workspace version still protects its core row.
- Scoped to `applicationId = workspaceCustomApplicationId OR NULL`, so
future app-owned core versions are never touched.
- Supports `--dry-run` and is idempotent (re-run is a no-op).

## Testing

Run through the real upgrade harness on a dev instance:
- Injected 2 synthetic orphans (1 ACTIVE `DATABASE_EVENT`, 1 ARCHIVED).
`--dry-run` reported `Would delete 2 (1 ACTIVE)`; real run reported
`Deleted 2 (1 ACTIVE)`.
- The 5 legit linked core versions were untouched (referrer guard
verified). Orphans remaining: 0.
- Re-run logged `No orphan core workflowVersion rows` (idempotent).
- typecheck, oxlint, oxfmt all clean.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/23739?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-08-04 14:55:09 +00:00
..