Skip to content

Commit 8de177c

Browse files
fix(server): keep the database from before 0.1.0 while an older version uses it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 844a508 commit 8de177c

3 files changed

Lines changed: 56 additions & 24 deletions

File tree

‎apps/server/src/cli/doctor.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ export const runDoctor = Effect.fn("runDoctor")(function* (
117117
);
118118
}
119119
if (selected.includes("legacy")) {
120-
// Only versions before 0.1.0 open this file, so the service keeps running.
120+
// Only versions before 0.1.0 open this file, so the service keeps running;
121+
// the removal itself refuses while one of those still has it open.
121122
yield* maintenance.removeLegacyDatabase(databasePath);
122123
yield* Console.log("\nDeleted the database from before 0.1.0.");
123124
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,22 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
4545
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
4646
assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes);
4747

48-
yield* fs.writeFileString(legacy, "V1 data");
49-
yield* fs.writeFileString(`${legacy}-wal`, "V1 log");
50-
assert.equal((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes, 7);
48+
const openLegacy = NodeSqliteClient.layer({ filename: legacy });
49+
yield* Effect.gen(function* () {
50+
const sql = yield* SqlClient.SqlClient;
51+
yield* sql`PRAGMA journal_mode = WAL`;
52+
yield* sql`CREATE TABLE v1_data (value TEXT)`;
53+
}).pipe(Effect.provide(openLegacy));
54+
assert.isAbove((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes ?? 0, 0);
55+
56+
// A version before 0.1.0 still running against it keeps it.
57+
const refused = yield* Effect.gen(function* () {
58+
const sql = yield* SqlClient.SqlClient;
59+
yield* sql`SELECT value FROM v1_data`;
60+
return yield* Effect.flip(maintenance.removeLegacyDatabase(dbPath));
61+
}).pipe(Effect.provide(openLegacy));
62+
assert.equal(refused._tag, "DatabaseInUseError");
63+
assert.isTrue(yield* fs.exists(legacy));
5164

5265
yield* maintenance.removeLegacyDatabase(dbPath);
5366
assert.isFalse(yield* fs.exists(legacy));

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class DatabaseInUseError extends Schema.TaggedError<DatabaseInUseError>()
2626
{ path: Schema.String, cause: Schema.Defect() },
2727
) {
2828
override get message() {
29-
return "Stop the T3 Code server and desktop app before compacting the database.";
29+
return "Stop the T3 Code server and desktop app, including any version before 0.1.0, before repairing the database.";
3030
}
3131
}
3232

@@ -99,7 +99,7 @@ export class DatabaseMaintenance extends Context.Service<
9999
*/
100100
readonly removeLegacyDatabase: (
101101
databasePath: string,
102-
) => Effect.Effect<void, LegacyDatabaseRemoveError>;
102+
) => Effect.Effect<void, DatabaseInUseError | LegacyDatabaseRemoveError>;
103103
}
104104
>()("t3/persistence/DatabaseMaintenance") {}
105105

@@ -121,6 +121,26 @@ const readStorage = Effect.gen(function* () {
121121
};
122122
});
123123

124+
/**
125+
* Holds an exclusive lock on the connection's database. In WAL mode this is
126+
* refused while any other connection has the file open, even an idle one,
127+
* which is how a running server or desktop app is detected.
128+
*/
129+
const lockExclusively = (databasePath: string) =>
130+
Effect.gen(function* () {
131+
const sql = yield* SqlClient.SqlClient;
132+
yield* sql`PRAGMA busy_timeout = 0`;
133+
yield* sql`PRAGMA locking_mode = EXCLUSIVE`;
134+
yield* sql`BEGIN EXCLUSIVE`.pipe(
135+
Effect.mapError((cause) =>
136+
isSqlError(cause) && cause.reason._tag === "LockTimeoutError"
137+
? new DatabaseInUseError({ path: databasePath, cause })
138+
: cause,
139+
),
140+
);
141+
yield* sql`COMMIT`;
142+
});
143+
124144
const make = Effect.gen(function* () {
125145
const fs = yield* FileSystem.FileSystem;
126146
const path = yield* Path.Path;
@@ -159,19 +179,8 @@ const make = Effect.gen(function* () {
159179
const beforeBytes = Number((yield* fs.stat(databasePath)).size);
160180
const result = yield* Effect.gen(function* () {
161181
const sql = yield* SqlClient.SqlClient;
162-
// Hold an exclusive lock through compaction and VACUUM. In WAL mode this
163-
// is refused while any other connection has the file open, even an idle
164-
// one, which is how a running server or desktop app is detected.
165-
yield* sql`PRAGMA busy_timeout = 0`;
166-
yield* sql`PRAGMA locking_mode = EXCLUSIVE`;
167-
yield* sql`BEGIN EXCLUSIVE`.pipe(
168-
Effect.mapError((cause) =>
169-
isSqlError(cause) && cause.reason._tag === "LockTimeoutError"
170-
? new DatabaseInUseError({ path: databasePath, cause })
171-
: cause,
172-
),
173-
);
174-
yield* sql`COMMIT`;
182+
// Held through compaction and VACUUM.
183+
yield* lockExclusively(databasePath);
175184
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
176185
if (!(yield* maintenance.verify).valid) {
177186
return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
@@ -213,14 +222,23 @@ const make = Effect.gen(function* () {
213222
const removeLegacyDatabase = Effect.fn("DatabaseMaintenance.removeLegacyDatabase")(
214223
function* (databasePath: string) {
215224
const legacy = legacyPath(databasePath);
216-
for (const suffix of ["-wal", "-shm", ""]) {
217-
yield* fs.remove(legacy + suffix, { force: true });
218-
}
225+
if (!(yield* fs.exists(legacy))) return;
226+
// A version before 0.1.0 may still be running against it. Keep the lock
227+
// while deleting, and delete the database before its WAL so a failure
228+
// never leaves it without committed transactions.
229+
yield* Effect.gen(function* () {
230+
yield* lockExclusively(legacy);
231+
for (const suffix of ["", "-wal", "-shm"]) {
232+
yield* fs.remove(legacy + suffix, { force: true });
233+
}
234+
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: legacy })));
219235
},
220236
(effect, databasePath) =>
221237
effect.pipe(
222-
Effect.mapError(
223-
(cause) => new LegacyDatabaseRemoveError({ path: legacyPath(databasePath), cause }),
238+
Effect.mapError((cause) =>
239+
cause._tag === "DatabaseInUseError"
240+
? cause
241+
: new LegacyDatabaseRemoveError({ path: legacyPath(databasePath), cause }),
224242
),
225243
),
226244
);

0 commit comments

Comments
 (0)