| ... | ... | @@ -10,7 +10,11 @@ import * as Effect from "effect/Effect"; |
| 10 | 10 | import * as Layer from "effect/Layer"; |
| 11 | 11 | import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| 12 | 12 | |
| 13 | | import { SqlitePersistenceMemory, makeSqlitePersistenceLive } from "./Sqlite.ts"; |
| 13 | import { |
| 14 | SqlitePersistenceMemory, |
| 15 | WAL_SIZE_LIMIT_BYTES, |
| 16 | makeSqlitePersistenceLive, |
| 17 | } from "./Sqlite.ts"; |
| 14 | 18 | |
| 15 | 19 | const lockHolderSource = ` |
| 16 | 20 | const { DatabaseSync } = require("node:sqlite"); |
| ... | ... | @@ -57,6 +61,32 @@ it.effect("waits out a concurrent writer instead of failing with SQLITE_BUSY", ( |
| 57 | 61 | ); |
| 58 | 62 | }); |
| 59 | 63 | |
| 64 | it.effect("shrinks the WAL file back to the size limit after a large write", () => { |
| 65 | const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-wal-")); |
| 66 | const dbPath = NodePath.join(tempDir, "state.sqlite"); |
| 67 | const walFileSize = () => NodeFS.statSync(`${dbPath}-wal`).size; |
| 68 | // About 25% more 4 KB rows than the limit holds, in one transaction. |
| 69 | const rowCount = Math.ceil((WAL_SIZE_LIMIT_BYTES * 1.25) / 4000); |
| 70 | |
| 71 | return Effect.gen(function* () { |
| 72 | const sql = yield* SqlClient.SqlClient; |
| 73 | yield* sql`CREATE TABLE wal_probe(payload BLOB)`; |
| 74 | yield* sql` |
| 75 | WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${rowCount}) |
| 76 | INSERT INTO wal_probe(payload) SELECT randomblob(4000) FROM n |
| 77 | `; |
| 78 | assert.isAbove(walFileSize(), WAL_SIZE_LIMIT_BYTES); |
| 79 | |
| 80 | // The auto-checkpoint after the large commit copied every frame into the |
| 81 | // database, so the next commit restarts the WAL and cuts the file back. |
| 82 | yield* sql`INSERT INTO wal_probe(payload) VALUES (x'00')`; |
| 83 | assert.isAtMost(walFileSize(), WAL_SIZE_LIMIT_BYTES); |
| 84 | }).pipe( |
| 85 | Effect.provide(makeSqlitePersistenceLive(dbPath).pipe(Layer.provide(NodeServices.layer))), |
| 86 | Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))), |
| 87 | ); |
| 88 | }); |
| 89 | |
| 60 | 90 | it.effect("applies busy_timeout in the shared persistence setup", () => |
| 61 | 91 | Effect.gen(function* () { |
| 62 | 92 | const sql = yield* SqlClient.SqlClient; |