Skip to content

Commit 5d8758b

Browse files
fix(server): remove abandoned v1 database copies on startup
Co-authored-by: Matěj Suchánek <[email protected]>
1 parent 04a21bb commit 5d8758b

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎apps/server/src/persistence/initializeV2Database.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeChildProcess from "node:child_process";
23
import * as NodeFS from "node:fs";
34
import * as NodeOS from "node:os";
45
import * as NodePath from "node:path";
@@ -182,3 +183,28 @@ it.effect("starts fresh without V1 and never imports over existing V2 state", ()
182183
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true }))),
183184
);
184185
});
186+
187+
it.effect("removes copies left by a killed start, but not one that is still running", () => {
188+
const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-v2-abandoned-"));
189+
const killed = NodeChildProcess.spawnSync(process.execPath, ["-e", ""]).pid;
190+
const running = `.v2-import-${process.pid}-Kq81Zx`;
191+
const unrelated = ".v2-import-not-a-copy";
192+
return Effect.gen(function* () {
193+
for (const name of [`.v2-import-${killed}-Wm3nPa`, ".v2-import-Q3xZ9a", running]) {
194+
NodeFS.mkdirSync(NodePath.join(directory, name));
195+
NodeFS.writeFileSync(NodePath.join(directory, name, "snapshot.sqlite"), "partial copy");
196+
}
197+
NodeFS.mkdirSync(NodePath.join(directory, unrelated));
198+
const destinationPath = NodePath.join(directory, "statev2.sqlite");
199+
NodeFS.writeFileSync(destinationPath, "existing V2 database");
200+
yield* initializeV2Database(destinationPath);
201+
assert.deepEqual(
202+
NodeFS.readdirSync(directory).sort(),
203+
[running, unrelated, "statev2.sqlite"].sort(),
204+
);
205+
assert.equal(NodeFS.readFileSync(destinationPath, "utf8"), "existing V2 database");
206+
}).pipe(
207+
Effect.provide(NodeServices.layer),
208+
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true }))),
209+
);
210+
});

‎apps/server/src/persistence/initializeV2Database.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ export class V2DatabaseImportError extends Schema.TaggedError<V2DatabaseImportEr
1414
}
1515
}
1616

17+
const isProcessAlive = (pid: number): boolean => {
18+
if (!Number.isInteger(pid) || pid <= 0) return false;
19+
try {
20+
process.kill(pid, 0);
21+
return true;
22+
} catch (error) {
23+
return error instanceof Error && "code" in error && error.code === "EPERM";
24+
}
25+
};
26+
27+
// Legacy names have no PID; newer copies stay while their process is alive.
28+
const removeAbandonedCopies = Effect.fn("removeAbandonedCopies")(function* (directory: string) {
29+
const fs = yield* FileSystem.FileSystem;
30+
const path = yield* Path.Path;
31+
const entries = yield* fs.readDirectory(directory).pipe(
32+
Effect.catchIf(
33+
(error) => error.reason._tag === "NotFound",
34+
() => Effect.succeed([]),
35+
),
36+
);
37+
for (const entry of entries) {
38+
const copy = /^\.v2-import-(?:(\d+)-)?[A-Za-z0-9]{6}$/.exec(entry);
39+
if (copy === null || (copy[1] !== undefined && isProcessAlive(Number(copy[1])))) continue;
40+
yield* fs.remove(path.join(directory, entry), { recursive: true });
41+
}
42+
});
43+
1744
/** Its copied legacy tables remain the source for lazy transcript import. */
1845
export const initializeV2Database = Effect.fn("initializeV2Database")(function* (
1946
destinationPath: string,
@@ -22,12 +49,13 @@ export const initializeV2Database = Effect.fn("initializeV2Database")(function*
2249
const path = yield* Path.Path;
2350
const directory = path.dirname(destinationPath);
2451
const sourcePath = path.join(directory, "state.sqlite");
52+
yield* removeAbandonedCopies(directory).pipe(Effect.ignore({ log: true }));
2553
yield* Effect.gen(function* () {
2654
if (yield* fs.exists(destinationPath)) return;
2755
if (!(yield* fs.exists(sourcePath))) return;
2856
const temporaryDirectory = yield* fs.makeTempDirectoryScoped({
2957
directory,
30-
prefix: ".v2-import-",
58+
prefix: `.v2-import-${process.pid}-`,
3159
});
3260
const snapshotPath = path.join(temporaryDirectory, "snapshot.sqlite");
3361
yield* Effect.tryPromise(async () => {

0 commit comments

Comments
 (0)