Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions apps/server/src/persistence/Layers/Sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,30 @@ it.effect("applies busy_timeout in the shared persistence setup", () =>
assert.equal(rows[0]?.timeout, 5000);
}).pipe(Effect.provide(SqlitePersistenceMemory)),
);

it.effect("truncates the WAL back to the size limit after a checkpoint", () => {
const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-wal-"));
const dbPath = NodePath.join(tempDir, "state.sqlite");
const walSize = () => NodeFS.statSync(`${dbPath}-wal`).size;
const limit = 16 * 1024 * 1024;

return Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`CREATE TABLE wal_probe(data BLOB)`;
yield* sql.withTransaction(
Effect.forEach(
Array.from({ length: 24 }),
() => sql`INSERT INTO wal_probe(data) VALUES (${new Uint8Array(1024 * 1024)})`,
),
);
assert.isAbove(walSize(), limit);

// The next writes cross the autocheckpoint threshold, reset the WAL, and truncate it.
yield* sql`DELETE FROM wal_probe`;
yield* sql`INSERT INTO wal_probe(data) VALUES (${new Uint8Array(1)})`;
assert.isAtMost(walSize(), limit);
}).pipe(
Effect.provide(makeSqlitePersistenceLive(dbPath).pipe(Layer.provide(NodeServices.layer))),
Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
);
});
2 changes: 2 additions & 0 deletions apps/server/src/persistence/Layers/Sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const setup = Layer.effectDiscard(
yield* sql`PRAGMA busy_timeout = 5000;`;
yield* sql`PRAGMA foreign_keys = ON;`;
yield* sql`PRAGMA journal_mode = WAL;`;
// WAL files never shrink on their own; truncate back to 16 MiB after each checkpoint reset.
yield* sql`PRAGMA journal_size_limit = 16777216;`;
yield* runMigrations();
}),
);
Expand Down
Loading