Files
OmniRoute/src/lib/db/stats.ts
Demiurge The Single ed0d14604d fix(db): tolerate unavailable virtual table modules in stats (#7313)
* chore(ci): add .mergify.yml to main — Mergify only reads config from the default branch (#7168)

* fix(ci): add the auto-enqueue pull_request_rule to the Mergify config (queue_conditions alone are eligibility-only) (#7179)

* fix(ci): migrate Mergify auto-enqueue to merge_protections_settings.auto_merge_conditions (rules-based path is EOL 2026-07-16) (#7216)

* fix(ci): drop Mergify batch settings (batching is a paid-tier feature; free plan queue is serial) (#7220)

* fix(ci): merge queue tolerates the advisory dast-smoke failure (its GH-hosted build hang dequeued every attempt) (#7225)

* fix(db): tolerate unavailable virtual table modules in stats

* test(db): cover missing count rows

* test(ci): make the #6634 selfref guard hermetic — main's copy hard-fails every PR (#7341)

main's copy of this test still does git I/O inside a unit test:

    const baseSrc = git(['show', 'origin/main:' + FILE]);

Runners check out a shallow single ref, so origin/main does not resolve and the
test dies with 'fatal: invalid object name origin/main'. Every PR into main
fails Unit Tests (7/8) on it — today that is #7313, #7315, #7316, #7334, #7336
and #7337, six PRs red on a defect none of them introduced. #7313 has no other
red at all.

release/v3.8.49 already carries a fix (2e42b8efc, #7174: try/catch, fetch
origin/main on demand, t.skip() when unreachable), but it only reaches main at
release time — so main stays broken for the whole cycle. Cherry-picking it would
also import a new problem: PR Test Policy classifies t.skip() as a silenced
assertion, which we watched it correctly catch on #7300 today.

This is the hermetic version instead (ported from #7327, which does the same for
the release branch): read the file straight off disk, compare against an empty
base so baseTaut/baseExtTaut are 0 — the strictest possible comparison point —
and call evaluateMasking() directly. No git ref, no fetch, no skip, nothing the
runner's checkout depth can break.

The #6634 regression stays covered: the guard's logic lives in
SELF_TEST_FIXTURE_RE (check-test-masking.mjs:337), not in the test. Proven both
ways on main before committing — neutralise SELF_TEST_FIXTURE_RE to /$^/ and
the test FAILS; restore it and it passes 2/2, with check-test-masking.mjs left
byte-identical.

Co-authored-by: growab <nekron@icloud.com>

* chore(quality): tighten main's coverage baseline to the CI's real numbers (#7347)

main's ratchet had been failing --require-tighten on every PR: 11 metrics
improved but the baseline was never tightened. Same class as the #6634
selfref guard — an infra fix that lands only on the release branch leaves
main red for the whole cycle, and every PR into main pays for it.

Values are the merged-coverage numbers from a run on main itself (a local
run measures ~68% vs CI's ~80%; the baseline's own note warns about that
gap). Only the 11 coverage values change — gitleaks and semgrepFindings
keep main's own state.

No changelog fragment: #7326 carries it on release/v3.8.49, and a second
one here would double the entry at release time.

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
Co-authored-by: roomhacker <roomhacker@bezrabotnyi.com>
Co-authored-by: growab <nekron@icloud.com>
2026-07-18 21:18:26 -03:00

79 lines
2.1 KiB
TypeScript

/**
* Database Statistics Module
*
* Provides functions to retrieve database statistics including size, table counts, and performance metrics.
*/
import type { SqliteAdapter } from "./adapters/types";
import { getDbInstance } from "./core";
export interface DatabaseStats {
totalSize: number;
pageSize: number;
pageCount: number;
tables: Array<{
name: string;
rowCount: number;
size: number;
}>;
indexes: Array<{
name: string;
tableName: string;
}>;
walSize?: number;
cacheSize: number;
}
export function getDatabaseStats(db: SqliteAdapter = getDbInstance()): DatabaseStats {
const pageSize = db.pragma("page_size", { simple: true }) as number;
const pageCount = db.pragma("page_count", { simple: true }) as number;
const cacheSize = db.pragma("cache_size", { simple: true }) as number;
const totalSize = pageSize * pageCount;
const tables = db
.prepare(
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`
)
.all() as Array<{ name: string }>;
const tableStats = tables.map((table) => {
let rowCount = 0;
try {
const quotedName = `"${table.name.replaceAll('"', '""')}"`;
const row = db.prepare(`SELECT COUNT(*) as count FROM ${quotedName}`).get() as
{ count: number } | undefined;
rowCount = row?.count ?? 0;
} catch (error) {
if (!(error instanceof Error) || !error.message.startsWith("no such module:")) {
throw error;
}
// Optional virtual-table modules may be unavailable on this connection.
}
const tableSize = db
.prepare(`SELECT SUM(pgsize) as size FROM dbstat WHERE name = ?`)
.get(table.name) as { size: number | null };
return {
name: table.name,
rowCount,
size: tableSize?.size || 0,
};
});
const indexes = db
.prepare(
`SELECT name, tbl_name as tableName FROM sqlite_master WHERE type='index' AND name NOT LIKE 'sqlite_%' ORDER BY name`
)
.all() as Array<{ name: string; tableName: string }>;
return {
totalSize,
pageSize,
pageCount,
tables: tableStats,
indexes,
cacheSize,
};
}