Files
wekan/models/recoveryEvents.js
T
Lauri Ojansivu 63c77a27f4 Automatic recovery/remediation for the SQLite text data + Admin Panel Recovery report
Completes the #6492 data-safety system on the WeKan side (FerretDB detection/repair/cap
landed in the fork):

- Decision brain: models/lib/recoveryPlan.js decideRecovery() picks the least-invasive
  recovery when the database is KNOWN corrupt (latest good backup -> previous backup ->
  re-migrate from MongoDB -> manual). It never acts on a healthy/unknown database and
  never restores a known-bad backup.
- Startup restore/re-migrate: every FerretDB launch path (snap, bundled release, Docker)
  can restore the text data BEFORE FerretDB opens the files, on request
  (WEKAN_FORCE_RESTORE env or a RESTORE_REQUESTED marker). It copies a known-good backup
  INTO the live database (dropping stale WAL side-files); backups are never deleted and
  the main wekan.sqlite is only overwritten, never removed. remigrate is delegated to
  migration-control. Attachments/avatars on the filesystem are untouched.
- Recovery events: the scripts append JSON lines to recovery-events.jsonl; the server
  (server/recovery.js) imports them into the recoveryEvents collection on startup, plus
  an admin-only recordRecoveryEvent method.
- Admin Panel / Problems / Recovery: a new admin-only report
  (server/publications/recoveryReport.js, wired in adminReports.js/.jade + i18n) lists
  the remediation history newest-first, readies up front and fetches directly (no hang).
- Docs: docs/Features/Admin-Panel/Problems/Recovery.md.

Tests (wired into npm run test:unit:node), verified by mirror since there is no Node
runtime here to execute them:
  - recoveryPlan.test.cjs (decision logic, positive + negatives)
  - recoveryEventsJsonl.test.cjs (parser: skips junk, normalizes severity, never throws)
  - recoveryReportQuery.test.cjs (search selector escapes regex metacharacters)
  - recoveryReportWiring.test.cjs (report wired; publication/count/method admin-gated)
  - ferretdbTextDataBackup.test.cjs extended: restore copies from a backup, is
    request-gated, and NEVER deletes the live text data or a backup copy

Thanks to bluetopaz1204, mueschel and xet7 !

Fixes #6492,
2026-07-21 10:06:40 +03:00

87 lines
2.5 KiB
JavaScript

import { Mongo } from 'meteor/mongo';
const { SimpleSchema } = require('/imports/simpleSchema');
// #6492 Recovery / Remediation audit log.
//
// An append-only record of the automatic data-safety actions taken for the SQLite
// text-data database — corruption detected, a backup created/verified, a restore from
// a backup copy, a re-migration of text data from MongoDB, an automatic VACUUM of a
// bloated file, etc. Shown to admins in Admin Panel / Problems / Recovery so the
// remediation history is visible in the logical, generally-used place.
const RecoveryEvents = new Mongo.Collection('recoveryEvents');
// Known event types (kept as plain strings so external tools can record their own).
RecoveryEvents.types = {
CORRUPTION_DETECTED: 'corruption-detected',
BACKUP_CREATED: 'backup-created',
BACKUP_VERIFIED: 'backup-verified',
RESTORE_BACKUP: 'restore-backup',
RESTORE_PREV: 'restore-prev',
REMIGRATE: 'remigrate',
BLOAT_REPAIRED: 'bloat-repaired',
INTEGRITY_OK: 'integrity-ok',
MANUAL_REQUIRED: 'manual-required',
};
RecoveryEvents.attachSchema(
new SimpleSchema({
type: {
// one of RecoveryEvents.types (or any string an external tool records)
type: String,
},
db: {
// the affected database, e.g. 'wekan'
type: String,
optional: true,
},
detail: {
// human-readable description of what happened / what was decided
type: String,
optional: true,
},
severity: {
// 'info' | 'warning' | 'error' — drives how the row is shown
type: String,
allowedValues: ['info', 'warning', 'error'],
optional: true,
},
source: {
// where the event came from: 'server' | 'startup' | 'ferretdb' | 'manual'
type: String,
optional: true,
},
createdAt: {
type: Date,
optional: true,
},
}),
);
// record appends one recovery event. Server-only and BEST-EFFORT: an audit-log write
// must never throw into (or block) the safety action it is recording.
RecoveryEvents.record = async function record(type, opts = {}) {
if (!Meteor.isServer || !type) {
return null;
}
try {
return await RecoveryEvents.insertAsync({
type,
db: opts.db,
detail: opts.detail,
severity: opts.severity || 'info',
source: opts.source || 'server',
createdAt: new Date(),
});
} catch (e) {
if (process.env.DEBUG === 'true') {
// eslint-disable-next-line no-console
console.error('RecoveryEvents.record failed:', e && e.message);
}
return null;
}
};
export default RecoveryEvents;