Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as SqlClient from "effect/sql/SqlClient";
9
10import * as DatabaseMaintenance from "./DatabaseMaintenance.ts";
11import * as Sqlite from "./Sqlite.ts";
12import * as EventStore from "../orchestration-v2/EventStore.ts";
13import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts";
14import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
15
16it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
17 "DatabaseMaintenance",
18 (it) => {
19 it.effect("reports an unreadable database as an inspection failure", () =>
20 Effect.gen(function* () {
21 const fs = yield* FileSystem.FileSystem;
22 const path = yield* Path.Path;
23 const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance;
24 const dbPath = path.join(
25 yield* fs.makeTempDirectoryScoped({ prefix: "t3-inspect-" }),
26 "statev2.sqlite",
27 );
28 yield* fs.writeFileString(dbPath, "not a database");
29 const failure = yield* Effect.flip(maintenance.inspect(dbPath));
30 assert.equal(failure._tag, "DatabaseInspectError");
31 }).pipe(Effect.scoped),
32 );
33
34 it.effect("compacts an existing database offline only once its server has stopped", () =>
35 Effect.gen(function* () {
36 const fs = yield* FileSystem.FileSystem;
37 const path = yield* Path.Path;
38 const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance;
39 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-compact-" });
40 const dbPath = path.join(baseDir, "userdata", "statev2.sqlite");
41 // A migrated database created before incremental auto_vacuum, with free pages.
42 yield* Effect.flatMap(
43 ProjectionMaintenance.ProjectionMaintenanceV2,
44 (projections) => projections.rebuild,
45 ).pipe(
46 Effect.provide(
47 ProjectionMaintenance.layer.pipe(
48 Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
49 Layer.provideMerge(Sqlite.layerFromPath(dbPath)),
50 ),
51 ),
52 );
53 yield* Effect.gen(function* () {
54 const sql = yield* SqlClient.SqlClient;
55 yield* sql`PRAGMA auto_vacuum = NONE`;
56 yield* sql`VACUUM`;
57 yield* sql`CREATE TABLE retained_data (value TEXT)`;
58 yield* sql`INSERT INTO retained_data VALUES ('keep')`;
59 yield* sql`CREATE TABLE discarded_data (value BLOB)`;
60 yield* sql`INSERT INTO discarded_data VALUES (zeroblob(4194304))`;
61 yield* sql`DROP TABLE discarded_data`;
62 yield* sql`PRAGMA wal_checkpoint(TRUNCATE)`;
63 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
64
65 // A server that merely has the database open, like an idle one, is enough to refuse.
66 const sizeBefore = (yield* fs.stat(dbPath)).size;
67 const refused = yield* Effect.gen(function* () {
68 const sql = yield* SqlClient.SqlClient;
69 yield* sql`SELECT value FROM retained_data`;
70 return yield* maintenance.compact(dbPath).pipe(Effect.flip);
71 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
72 assert.equal(refused._tag, "DatabaseInUseError");
73 assert.equal((yield* fs.stat(dbPath)).size, sizeBefore);
74
75 const result = yield* maintenance.compact(dbPath);
76 // The 4 MiB of free pages are returned to the filesystem.
77 assert.isBelow(result.afterBytes, result.beforeBytes - 3 * 1024 * 1024);
78 yield* Effect.gen(function* () {
79 const sql = yield* SqlClient.SqlClient;
80 assert.deepEqual(yield* sql`PRAGMA auto_vacuum`, [{ auto_vacuum: 2 }]);
81 assert.deepEqual(yield* sql`SELECT value FROM retained_data`, [{ value: "keep" }]);
82 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true })));
83 }).pipe(Effect.scoped),
84 );
85 },
86);