Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): collect SQLite planner statistics when the database opens #18471

Mergedotavio(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/18471/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

Showing 2 changed files with 34 additions and 0 deletions.

x
0 of 2 viewed, 2 left
30 additions and 0 deletionsapps/server/src/persistence/Sqlite.test.tsModified
View file
@@ -84,6 +84,36 @@
);
});
​
+it.effect("collects planner statistics for an existing database on open", () => {
+ const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-stats-"));
+ const dbPath = NodePath.join(tempDir, "state.sqlite");
+ const persistence = () =>
+ SqlitePersistence.layerFromPath(dbPath).pipe(Layer.provide(NodeServices.layer));
+​
+ return Effect.gen(function* () {
+ yield* Effect.gen(function* () {
+ const sql = yield* SqlClient.SqlClient;
+ yield* sql`CREATE TABLE stats_probe(id INTEGER PRIMARY KEY, thread_id TEXT NOT NULL)`;
+ yield* sql`CREATE INDEX stats_probe_thread_idx ON stats_probe(thread_id)`;
+ yield* sql`
+ WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 1000)
+ INSERT INTO stats_probe(thread_id) SELECT 'thread-' || (i % 10) FROM n
+ `;
+ }).pipe(Effect.provide(persistence()));
+​
+ // A database written before statistics existed gets them on the next open.
+ yield* Effect.gen(function* () {
+ const sql = yield* SqlClient.SqlClient;
+ const rows = yield* sql<{ readonly stat: string }>`
+ SELECT stat FROM sqlite_stat1 WHERE idx = 'stats_probe_thread_idx'
+ `;
+ assert.deepEqual([...rows], [{ stat: "1000 100" }]);
+ }).pipe(Effect.provide(persistence()));
+ }).pipe(