mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
feat(backup): declare what a backup carries, and force a verdict on the rest
The mirror of the wipe plan, for the same reason and against a worse failure: a list that was right when written and stops being right every time a model is added. On the delete side that leaves data behind; here it means data does not come back, and nothing says so until someone is restoring a backup because something already went wrong. The list is longer than the wipe list by sixteen models, and that gap is the point. A wipe may lean on the database — deleting a medication cascades to its schedules, dose changes and inventory events — but a restore has no cascade, so every child has to be named here. Every exclusion carries its reason in prose, and the guard enforces a length floor on it. That floor is not pedantry: the first draft of this file said 'Same as WithingsConnection' four times, which is a pointer rather than a reason, and a pointer silently inherits whatever the thing it points at becomes. Writing the decision where it is made is the only way it stays reviewable. The guard found two things in its own author's work on the first run: a model with no verdict at all, and a reason too thin to be one. The restore side of the guard — that every carried model also has a restore branch — lands with the payload work it will police.
This commit is contained in:
1 parent
8e9da490a9
commit
a2e9587781
2 files changed
+389
No files matched your search
@@ -0,0 +1,128 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
AUTH_MODELS_OUT_OF_SCOPE,
|
||||
BACKED_UP_MODELS,
|
||||
DERIVED_MODELS,
|
||||
NOT_IN_BACKUP_MODELS,
|
||||
backupVerdict,
|
||||
} from "@/lib/export/backup-plan";
|
||||
|
||||
/**
|
||||
* Every model in the schema has a written verdict about the backup.
|
||||
*
|
||||
* This is the first half of the backup guard: classification. The second half
|
||||
* — that every `BACKED_UP` model actually has a payload reader AND a restore
|
||||
* branch — lives beside it, because a model that is classified as carried and
|
||||
* then never restored is the defect, not the fix.
|
||||
*
|
||||
* The failure this prevents: a model is added to `schema.prisma`, it is
|
||||
* user-scoped from birth, and it is outside the backup by default. Nothing
|
||||
* notices, and the gap is only discovered by someone restoring a backup and
|
||||
* finding a part of their record missing — which is the worst possible moment
|
||||
* and the worst possible messenger.
|
||||
*/
|
||||
|
||||
const SCHEMA_PATH = resolve(__dirname, "../../prisma/schema.prisma");
|
||||
|
||||
/** Models declared in the schema, minus `User` itself (the account row). */
|
||||
function schemaModels(): string[] {
|
||||
const source = readFileSync(SCHEMA_PATH, "utf8");
|
||||
return [...source.matchAll(/^model\s+(\w+)\s*\{/gm)]
|
||||
.map((m) => m[1])
|
||||
.filter((name) => name !== "User");
|
||||
}
|
||||
|
||||
/**
|
||||
* Models with no `userId` and no user relation are instance-scoped (queue
|
||||
* tables, rate-limit buckets, invite codes the operator issues). They are not
|
||||
* an account's data and the wipe plan already declares them as such.
|
||||
*/
|
||||
function isUserScoped(model: string): boolean {
|
||||
const source = readFileSync(SCHEMA_PATH, "utf8");
|
||||
const block = new RegExp(
|
||||
`^model\\s+${model}\\s*\\{([\\s\\S]*?)^\\}`,
|
||||
"m",
|
||||
).exec(source);
|
||||
if (!block) return false;
|
||||
const body = block[1];
|
||||
return /\buserId\b/.test(body) || /\buser\s+User\b/.test(body);
|
||||
}
|
||||
|
||||
describe("every schema model has a backup verdict", () => {
|
||||
const models = schemaModels();
|
||||
|
||||
it("reads a schema with models in it (no vacuous pass)", () => {
|
||||
expect(models.length).toBeGreaterThan(50);
|
||||
});
|
||||
|
||||
it("classifies every user-scoped model", () => {
|
||||
const unclassified = models
|
||||
.filter(isUserScoped)
|
||||
.filter((m) => backupVerdict(m) === null);
|
||||
|
||||
if (unclassified.length > 0) {
|
||||
throw new Error(
|
||||
`${unclassified.length} user-scoped model(s) have no backup verdict:\n\n` +
|
||||
unclassified.map((m) => ` ❌ ${m}`).join("\n") +
|
||||
"\n\nAdd each to src/lib/export/backup-plan.ts: to BACKED_UP_MODELS if an " +
|
||||
"account would lose something real without it, to DERIVED_MODELS with what " +
|
||||
"rebuilds it, or to NOT_IN_BACKUP_MODELS with why it must not travel. " +
|
||||
"A new model is user-scoped from birth and outside the backup by default, " +
|
||||
"which is exactly the silence this test exists to break.",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("carries no verdict for a model the schema no longer has", () => {
|
||||
const known = new Set(models);
|
||||
const stale = [
|
||||
...BACKED_UP_MODELS,
|
||||
...Object.keys(DERIVED_MODELS),
|
||||
...Object.keys(NOT_IN_BACKUP_MODELS),
|
||||
...AUTH_MODELS_OUT_OF_SCOPE,
|
||||
].filter((m) => !known.has(m));
|
||||
|
||||
expect(
|
||||
stale,
|
||||
`verdicts for models that no longer exist: ${stale.join(", ")}`,
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("gives each model exactly one verdict", () => {
|
||||
const seen = new Map<string, string[]>();
|
||||
const add = (model: string, list: string) => {
|
||||
seen.set(model, [...(seen.get(model) ?? []), list]);
|
||||
};
|
||||
for (const m of BACKED_UP_MODELS) add(m, "BACKED_UP");
|
||||
for (const m of Object.keys(DERIVED_MODELS)) add(m, "DERIVED");
|
||||
for (const m of Object.keys(NOT_IN_BACKUP_MODELS)) add(m, "NOT_IN_BACKUP");
|
||||
for (const m of AUTH_MODELS_OUT_OF_SCOPE) add(m, "AUTH");
|
||||
|
||||
const doubled = [...seen.entries()].filter(([, lists]) => lists.length > 1);
|
||||
expect(
|
||||
doubled.map(([m, lists]) => `${m}: ${lists.join(" + ")}`),
|
||||
"a model classified twice means two readers can each believe the other handles it",
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("gives every excluded model a reason, not an empty string", () => {
|
||||
const empty = [
|
||||
...Object.entries(DERIVED_MODELS),
|
||||
...Object.entries(NOT_IN_BACKUP_MODELS),
|
||||
]
|
||||
// 20 characters let "Same as WithingsConnection." through, which is a
|
||||
// pointer rather than a reason: if the entry it points at changes, this
|
||||
// one silently inherits something else. 60 forces the decision to be
|
||||
// written where it is made.
|
||||
.filter(([, reason]) => reason.trim().length < 60)
|
||||
.map(([model]) => model);
|
||||
expect(
|
||||
empty,
|
||||
"a one-word reason is how a decision stops being reviewable",
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user