Repository navigation
fix(platform): retire the schedules and rows of deleted organizations - #3550
Merged
Merged
Conversation
A deletion before 0.5.9 (#3240) left every automation row of the organization behind, and the minutely scan went on claiming and starting a run for each due occurrence of its enabled schedules — for a tenant that no longer existed, forever. Each page row of the scan now says whether an `organization` row still carries its org id. A schedule whose organization is gone is never claimed: the page disables it with one conditional UPDATE (still enabled, organization still missing, re-checked at the write) and the scan names what that write disabled in one line. A disabled schedule never enters a page again, and an overlapping scan that lost the write reports nothing, so each one is logged exactly once. `orphaned` counts them on the scan result. Proved on real Postgres by `checkDeletedOrgSchedules`: a deployed, due schedule of a non-existent organization starts no run, ends up disabled and unclaimed, is named in one line across two scans, and the live schedule beside it is still claimed. Without the fix the same lane reads runs=1, enabled=true. Refs #3022
Until #3240 (0.5.9) deleting an organization removed a handful of tables and left every other org-keyed app row behind. Migration 0125 removes them the way the teardown cascade would have: the same catalog walk `listOrgOwnedTables` does (every app base table with an org_id column), child tables before the tables they reference, in the byte order `orderChildrenFirst` sorts by, and the governance ledger kept (`ORG_TEARDOWN_KEEPS`: audit_logs, audit_chain_heads, audit_integrity_progress, organization_tombstones). A row is stranded when no "organization" row carries its org_id. A deleted organization with an active legal hold keeps every row: the teardown refuses such a deletion since 0.5.8 (#3162), but on 0.5.0–0.5.7 an organization could be deleted under a hold. Guarded for a fresh database (app migrations run before Better Auth creates its tables), idempotent, and rolling-deploy safe. At boot on a fresh database the guard returns before any DELETE, so `checkOrphanedOrgRowsBackfill` re-applies the file over a planted fixture inside a rolled-back transaction: the dead organization's rows go along a cascade, SET NULL and NO ACTION foreign key, the audit row and tombstone stay, a held dead organization and the live one keep every row, and a second pass changes nothing. The ledger rows in backend/MIGRATION.md and the manual automation register record it. Closes #3022
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Organization deletions before #3240 (0.5.0–0.5.8) removed a few tables and left every other org-keyed app row behind. That included the organization's automations and their enabled schedules. The minutely scan kept claiming each due occurrence and started a run for an organization that no longer existed, and it would do so indefinitely (#3022). This PR fixes it in two layers:
backend/domains/automations/triggers.ts).orgMissingflag (NOT EXISTS "organization").UPDATE … SET enabled = false. The write re-checks that the row is still enabled and the organization still missing, and it returns the rows it changed.ScheduleScanResult.orphanedcounts them.0125_rows_of_deleted_organizations.sqlremoves what those deletions left behind. It works the way the teardown cascade does:listOrgOwnedTables: everyappbase table with anorg_idcolumn.orderChildrenFirst's (checked on a scratch copy).ORG_TEARDOWN_KEEPS(audit_logs,audit_chain_heads,audit_integrity_progress,organization_tombstones)."organization"row carries itsorg_id.to_regclass('"organization"')for a fresh database, the 0120 pattern. App migrations run before Better Auth creates its tables.backend/MIGRATION.md(organizations row) andtests/manual/reference/automation.md(a new automated row) record both.Closes #3022
Decisions for the reviewer
NOT EXISTSclause to drop.ORG_TEARDOWN_KEEPSas of this PR.0123is taken on main (feat(platform): fan out @mentions in task descriptions #3519) and0124is claimed by the open feat(platform): pause a schedule after five permanent failures in a row #3533 (refactor(platform): drop the retired sandbox tables #3532 also still claims0123), so this PR takes0125. Please re-check at merge time. The integration lane finds the file by its slug, so renumbering needs no code change."organization", which means a sequential pass over the large tables. This is a one-time cost at the boot that applies it, the same kind of cost as the0076/0077dedups.org_idholds only organization ids. Before relying on "noorganizationrow means stranded", I swept every write into the 96 tables, in current code and in all v0.5.x tags. Every INSERT bindsorg_idto an organization id resolved from a session, REST key, token or existing row. No table is deployment-wide, and no migration seeds rows.Verification
bunx vitest run --project serverontriggers.scan.test.ts,triggers.webhook.test.ts,backend/jobs/andbackend/domains/organizations/: 12 files, 95 tests passed. After rebasing onto the current main, the scan, webhook and organizations files passed again: 5 files, 68 tests. The two new scan cases and the updated result shapes fail against the unfixed scan (5 of 10 red).bunx oxlint --type-awareandoxfmt --checkon the changed TypeScript files: clean. Opengrep (tools/opengrep/run.sh) on the changed files: 0 findings.bun run lint:manual: ok.tscover the changed files (except the 54k-lineintegration-check.ts, whose only change is two imports and two lane entries): exit 0.backend:integration). Two new lanes, both registered:checkDeletedOrgSchedules: a deployed, due schedule of a non-existent organizationcheckOrphanedOrgRowsBackfill: re-applies the file inside a rolled-back transaction. At boot the guard returns before any DELETE, so this lane is the only place the DELETEs run. Results:ITEST_LANES=checkAutomations,checkAutomationTriggerDelivery,checkDeletedOrgSchedules,checkOrphanedOrgRowsBackfill: 36/36 checks passed across 4/4 lanes. This filtered run includes the existing fairness (205 schedules), overlapping-scan and schedule-fire probes.triggers.tsreverted,checkDeletedOrgSchedulesreadsruns=1 (want 0), enabled=true (want false), claimed=true.checkOrganizationLifecyclefails 6 checks with the same output onorigin/main. The lane renames org A to "Life A renamed" and later confirms the deletion with "Life A", so the teardown answers400 ORG_CONFIRM_NAME_MISMATCH, and every check after it that assumes the deletion happened fails. It needs its own fix.Not in scope
app_realtime.outbox. The teardown clears it, butlistOrgOwnedTablesdoes not walk it, and hints older than an hour are reclaimed anyway.