@devries-cameron20 / t3code-1 / commits / 1d626d02d9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(cli): close legacy database before deleting it on Windows (#27629)

Utkarsh Patil committed Nov 1, 2026 at 20:54 UTC 1d626d02d95f9a2343553f6498037ee82290d304
2 files changed +53 -9
apps/server/src/persistence/DatabaseMaintenance.test.ts
+35 -2
@@ -1,5 +1,6 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 +import * as HostProcess from "@t3tools/shared/HostProcess";
4 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
5 import * as Clock from "effect/Clock";
6 import * as Duration from "effect/Duration";
@@ -44,6 +45,7 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
45 yield* Effect.gen(function* () {
46 const sql = yield* SqlClient.SqlClient;
47 yield* sql`CREATE TABLE retained_data (value TEXT)`;
48 + yield* sql`INSERT INTO retained_data VALUES ('keep')`;
49 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
50 assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes);
51
@@ -52,21 +54,52 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
54 const sql = yield* SqlClient.SqlClient;
55 yield* sql`PRAGMA journal_mode = WAL`;
56 yield* sql`CREATE TABLE v1_data (value TEXT)`;
57 + yield* sql`INSERT INTO v1_data VALUES ('rollback')`;
58 }).pipe(Effect.provide(openLegacy));
59 assert.isAbove((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes ?? 0, 0);
60
61 const refused = yield* Effect.gen(function* () {
62 const sql = yield* SqlClient.SqlClient;
63 yield* sql`SELECT value FROM v1_data`;
61 - return yield* Effect.flip(maintenance.removeLegacyDatabase(dbPath));
64 + const failure = yield* Effect.flip(maintenance.removeLegacyDatabase(dbPath));
65 + assert.deepEqual(yield* sql`SELECT value FROM v1_data`, [{ value: "rollback" }]);
66 + return failure;
67 }).pipe(Effect.provide(openLegacy));
68 assert.equal(refused._tag, "DatabaseInUseError");
69 assert.isTrue(yield* fs.exists(legacy));
70
66 - yield* maintenance.removeLegacyDatabase(dbPath);
71 + yield* Effect.flatMap(DatabaseMaintenance.DatabaseMaintenance, (service) =>
72 + service.removeLegacyDatabase(dbPath),
73 + ).pipe(
74 + Effect.provide(Layer.fresh(DatabaseMaintenance.layer)),
75 + Effect.provideService(HostProcess.Platform, "win32"),
76 + Effect.provideService(FileSystem.FileSystem, {
77 + ...fs,
78 + remove: (target, options) =>
79 + Effect.gen(function* () {
80 + if (target === legacy) {
81 + yield* Effect.gen(function* () {
82 + const sql = yield* SqlClient.SqlClient;
83 + yield* sql`PRAGMA busy_timeout = 0`;
84 + yield* sql`BEGIN EXCLUSIVE`;
85 + yield* sql`COMMIT`;
86 + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: legacy })));
87 + // Create sidecars after the probe closes so only Doctor can remove them.
88 + yield* fs.writeFileString(`${legacy}-wal`, "leftover WAL");
89 + yield* fs.writeFileString(`${legacy}-shm`, "leftover SHM");
90 + }
91 + yield* fs.remove(target, options);
92 + }),
93 + }),
94 + );
95 assert.isFalse(yield* fs.exists(legacy));
96 assert.isFalse(yield* fs.exists(`${legacy}-wal`));
97 + assert.isFalse(yield* fs.exists(`${legacy}-shm`));
98 assert.isTrue(yield* fs.exists(dbPath));
99 + yield* Effect.gen(function* () {
100 + const sql = yield* SqlClient.SqlClient;
101 + assert.deepEqual(yield* sql`SELECT value FROM retained_data`, [{ value: "keep" }]);
102 + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true })));
103 assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes);
104 }).pipe(Effect.scoped),
105 );
apps/server/src/persistence/DatabaseMaintenance.ts
+18 -7
@@ -1,3 +1,4 @@
1 +import * as HostProcess from "@t3tools/shared/HostProcess";
2 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
@@ -220,13 +221,23 @@ const make = Effect.gen(function* () {
221 function* (databasePath: string) {
222 const legacy = legacyPath(databasePath);
223 if (!(yield* fs.exists(legacy))) return;
223 - // Older servers may use this file; hold its lock and delete the database before WAL to preserve commits on failure.
224 - yield* Effect.gen(function* () {
225 - yield* lockExclusively(legacy);
226 - for (const suffix of ["", "-wal", "-shm"]) {
227 - yield* fs.remove(legacy + suffix, { force: true });
228 - }
229 - }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: legacy })));
224 + // Delete the database before WAL to preserve commits if unlink fails.
225 + const removeFiles = Effect.forEach(
226 + ["", "-wal", "-shm"],
227 + (suffix) => fs.remove(legacy + suffix, { force: true }),
228 + { discard: true },
229 + );
230 + const connection = NodeSqliteClient.layer({ filename: legacy });
231 + if ((yield* HostProcess.Platform) === "win32") {
232 + // Windows blocks unlink while any SQLite handle is open, including our own.
233 + yield* lockExclusively(legacy).pipe(Effect.provide(connection));
234 + yield* removeFiles;
235 + } else {
236 + yield* lockExclusively(legacy).pipe(
237 + Effect.andThen(removeFiles),
238 + Effect.provide(connection),
239 + );
240 + }
241 },
242 (effect, databasePath) =>
243 effect.pipe(