Make Cards database restores reliable and schedule backups
A restore left the replaced database's write-ahead log in place, so SQLite replayed it and the restored file silently kept later writes. Restores now remove it, backups stay single files, and a systemd timer keeps 14 daily copies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d72dd38317
commit
115e62d5b2
5 changed files with 118 additions and 8 deletions
|
|
@ -1,10 +1,11 @@
|
|||
// Rôle : vérifie les règles, la persistance et les mutations protégées de Sokko TCG.
|
||||
import assert from "node:assert/strict";
|
||||
import { access, readdir, readFile } from "node:fs/promises";
|
||||
import { access, readdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { test } from "node:test";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { assertVersionedCatalogScope, buildPublishedCatalog, prepareVersionedCatalog, validateCardsCatalog, withGeneratedFinishes } from "../services/cards/catalog.js";
|
||||
import { INITIAL_COLLECTION_STATES, INITIAL_DROP_RATES, INITIAL_RECHARGE_SETTING, INITIAL_SHOP_SETTING } from "../services/cards/admin-seed.js";
|
||||
import { planRecycling } from "../services/cards/stardust.js";
|
||||
|
|
@ -14,7 +15,7 @@ import { CardsDatabase } from "../services/cards/database.js";
|
|||
import { calculateRecharge, drawPack, drawShopPack } from "../services/cards/game-rules.js";
|
||||
import { CardsService, DEV_DISCORD_ID } from "../services/cards/service.js";
|
||||
import { visibleCollections } from "../website/src/features/cards/collectionState.js";
|
||||
import { backupCardsDatabase, restoreCardsDatabase, verifyCardsDatabase } from "../services/cards/backup.js";
|
||||
import { backupCardsDatabase, restoreCardsDatabase, runScheduledBackup, verifyCardsDatabase } from "../services/cards/backup.js";
|
||||
|
||||
// Catalogue versionné (finitions générées, sans variation mythique) et catalogue publié avec une variation de test.
|
||||
const rawCatalog = JSON.parse(await readFile("website/public/data/cards/catalog.json", "utf8"));
|
||||
|
|
@ -653,6 +654,56 @@ test("database backup passes integrity checks and restores ownership", async ()
|
|||
}
|
||||
});
|
||||
|
||||
test("restoring replaces the database even when the stopped service left a write-ahead log", async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "sokko-cards-restore-wal-"));
|
||||
const live = join(directory, "live.sqlite");
|
||||
const backup = join(directory, "backup.sqlite");
|
||||
const countPlayers = (database) => database.db.prepare("SELECT COUNT(*) AS count FROM players").get().count;
|
||||
try {
|
||||
const database = new CardsDatabase(live);
|
||||
new CardsService({ database, catalog: baseCatalog }).upsertPlayer({ discordId: "before-backup", displayName: "Avant" });
|
||||
database.close();
|
||||
await backupCardsDatabase(live, backup);
|
||||
// Un process tué sans fermer la base laisse ses écritures dans le WAL : la restauration ne doit pas les rejouer.
|
||||
const killedWriter = `import { DatabaseSync } from "node:sqlite";
|
||||
const db = new DatabaseSync(${JSON.stringify(live)});
|
||||
db.exec("PRAGMA journal_mode = WAL");
|
||||
db.prepare("INSERT INTO players(id, discord_id, display_name, created_at) VALUES ('after', 'after', 'Après', 0)").run();
|
||||
process.kill(process.pid, "SIGKILL");`;
|
||||
const writer = spawnSync(process.execPath, ["--no-warnings", "--input-type=module", "-e", killedWriter]);
|
||||
assert.equal(writer.signal, "SIGKILL", String(writer.stderr));
|
||||
await access(`${live}-wal`);
|
||||
restoreCardsDatabase(backup, live);
|
||||
assert.deepEqual((await readdir(directory)).sort(), ["backup.sqlite", "live.sqlite"]);
|
||||
const restored = new CardsDatabase(live);
|
||||
assert.equal(countPlayers(restored), 1);
|
||||
restored.close();
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("scheduled backups are dated, verified and pruned without touching other files", async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "sokko-cards-scheduled-"));
|
||||
const source = join(directory, "source.sqlite");
|
||||
const backups = join(directory, "backups");
|
||||
try {
|
||||
const database = new CardsDatabase(source);
|
||||
database.close();
|
||||
const written = [];
|
||||
for (const day of ["2026-11-01", "2026-11-02", "2026-11-03", "2026-11-03"]) written.push(await runScheduledBackup(source, backups, { keep: 2, now: new Date(`${day}T04:00:00Z`) }));
|
||||
assert.equal(written.at(-1), join(backups, "nebula-cards-2026-11-03.sqlite"));
|
||||
assert.deepEqual((await readdir(backups)).sort(), ["nebula-cards-2026-11-02.sqlite", "nebula-cards-2026-11-03.sqlite"]);
|
||||
await writeFile(join(backups, "nebula-cards-before-cleanup.sqlite"), "");
|
||||
await runScheduledBackup(source, backups, { keep: 1, now: new Date("2026-11-04T04:00:00Z") });
|
||||
assert.deepEqual((await readdir(backups)).sort(), ["nebula-cards-2026-11-04.sqlite", "nebula-cards-before-cleanup.sqlite"]);
|
||||
assert.equal(verifyCardsDatabase(join(backups, "nebula-cards-2026-11-04.sqlite")), true);
|
||||
await assert.rejects(runScheduledBackup(source, backups, { keep: 0 }), /keep/);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("published catalog adds mythic variations and opening settings stored in the database", () => {
|
||||
const legendary = baseCatalog.editions.find((edition) => edition.cardId === "starvoid-dragon" && edition.rarity === "legendary");
|
||||
const published = buildPublishedCatalog(baseCatalog, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue