Operational scripts that move cold rows out of the hot tables (bolagsverket_companies, scb_foundations) into *_archive siblings. All four live in scripts/ and were introduced during the 2026-04-06 archive day — see History Archive Era for the commit-by-commit story.
Every script writes an archive_audit_log row inside the same transaction, so every archive / restore is auditable.
archive-inactive.ts
Moves inactive companies. Inactive means:
- Bolagsverket:
is_active = false - SCB:
status IS NULL OR status != 1
Both halves run in a single BEGIN / COMMIT per table. The script verifies archive_count >= inactive_count before deleting from the source table, then sanity-checks that the active count did not change — exits non-zero if any active row was lost.
bun run scripts/archive-inactive.tsWarning
Destructive: actually deletes from the hot tables. The destructive lines are
DELETE FROM ${tableName} WHERE is_active = false(line 58) andDELETE FROM ${tableName} WHERE status IS NULL OR status != 1(line 133). Always run backup-database first.
archive-non-ab.ts
Moves non-AB (non-Aktiebolag) companies. Non-AB means:
- Bolagsverket:
legal_form IS NULL OR legal_form != 'AB' - SCB:
legal_form IS NULL OR legal_form != 49
Same transactional pattern as archive-inactive.ts, with archive_reason = 'non_ab' stamped on the archive rows so the two archive cohorts can be distinguished.
bun run scripts/archive-non-ab.tsWarning
Destructive. The hot-table deletes are at lines 64 and 151. Same backup advice as above.
verify-archive.ts
Read-only sanity check. Reports per-table counts: active rows in main, total archived rows, sample of five active companies. Two modes:
bun run scripts/verify-archive.ts— verify mode, reports current counts.bun run scripts/verify-archive.ts --baseline— captures baselinearchive_audit_logrows before an archive run, so the diff afterwards is unambiguous.
Use this around every archive run: capture baseline, archive, verify.
restore-from-archive.ts
The inverse. Re-inserts every row from *_archive back into the hot tables with ON CONFLICT (org_nr) DO NOTHING, so it is safe to re-run.
bun run scripts/restore-from-archive.ts # restore only
bun run scripts/restore-from-archive.ts --clear-archive # also empty the archive tablesWarning
--clear-archiverunsDELETE FROM bolagsverket_companies_archiveandDELETE FROM scb_foundations_archive(lines 43–44). After that, the archive is gone — a second restore will not find anything. Only pass the flag after you have confirmed the restore succeeded.
Recommended runbook
bun run scripts/backup-database.ts— see Backup Scripts.bun run scripts/verify-archive.ts --baselinebun run scripts/archive-inactive.ts(orarchive-non-ab.ts)bun run scripts/verify-archive.ts- Compare baseline vs final counts in
archive_audit_log.
If counts diverge, restore: bun run scripts/restore-from-archive.ts.
See also
History Archive Era, Backup Scripts, Database Schema, Schema Migrations.