Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DatabaseMaintenance.test.ts

125 lines6.1 KB

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("reports and deletes only the database from before 0.1.0", () =>
35 Effect.gen(function* () {
36 const fs = yield* FileSystem.FileSystem;
37 const path = yield* Path.Path;
38 const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance;
39 const userdata = yield* fs.makeTempDirectoryScoped({ prefix: "t3-legacy-" });
40 const dbPath = path.join(userdata, "statev2.sqlite");
41 const legacy = path.join(userdata, "state.sqlite");
42 yield* Effect.gen(function* () {
43 const sql = yield* SqlClient.SqlClient;
44 yield* sql`CREATE TABLE retained_data (value TEXT)`;
45 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
46 assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes);
47​
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));
64​
65 yield* maintenance.removeLegacyDatabase(dbPath);
66 assert.isFalse(yield* fs.exists(legacy));
67 assert.isFalse(yield* fs.exists(`${legacy}-wal`));
68 assert.isTrue(yield* fs.exists(dbPath));
69 assert.isNull((yield* maintenance.inspect(dbPath)).legacyDatabaseBytes);
70 }).pipe(Effect.scoped),
71 );
72​
73 it.effect("compacts an existing database offline only once its server has stopped", () =>
74 Effect.gen(function* () {
75 const fs = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance;
78 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-compact-" });
79 const dbPath = path.join(baseDir, "userdata", "statev2.sqlite");
80 // A migrated database created before incremental auto_vacuum, with free pages.
81 yield* Effect.flatMap(
82 ProjectionMaintenance.ProjectionMaintenanceV2,
83 (projections) => projections.rebuild,
84 ).pipe(
85 Effect.provide(
86 ProjectionMaintenance.layer.pipe(
87 Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
88 Layer.provideMerge(Sqlite.layerFromPath(dbPath)),
89 ),
90 ),
91 );
92 yield* Effect.gen(function* () {
93 const sql = yield* SqlClient.SqlClient;
94 yield* sql`PRAGMA auto_vacuum = NONE`;
95 yield* sql`VACUUM`;
96 yield* sql`CREATE TABLE retained_data (value TEXT)`;
97 yield* sql`INSERT INTO retained_data VALUES ('keep')`;
98 yield* sql`CREATE TABLE discarded_data (value BLOB)`;
99 yield* sql`INSERT INTO discarded_data VALUES (zeroblob(4194304))`;
100 yield* sql`DROP TABLE discarded_data`;
101 yield* sql`PRAGMA wal_checkpoint(TRUNCATE)`;
102 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
103​
104 // A server that merely has the database open, like an idle one, is enough to refuse.
105 const sizeBefore = (yield* fs.stat(dbPath)).size;
106 const refused = yield* Effect.gen(function* () {
107 const sql = yield* SqlClient.SqlClient;
108 yield* sql`SELECT value FROM retained_data`;
109 return yield* maintenance.compact(dbPath).pipe(Effect.flip);
110 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
111 assert.equal(refused._tag, "DatabaseInUseError");
112 assert.equal((yield* fs.stat(dbPath)).size, sizeBefore);
113​
114 const result = yield* maintenance.compact(dbPath);
115 // The 4 MiB of free pages are returned to the filesystem.
116 assert.isBelow(result.afterBytes, result.beforeBytes - 3 * 1024 * 1024);
117 yield* Effect.gen(function* () {
118 const sql = yield* SqlClient.SqlClient;
119 assert.deepEqual(yield* sql`PRAGMA auto_vacuum`, [{ auto_vacuum: 2 }]);
120 assert.deepEqual(yield* sql`SELECT value FROM retained_data`, [{ value: "keep" }]);
121 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath, readonly: true })));
122 }).pipe(Effect.scoped),
123 );
124 },
125);