Match the pg query logging

This commit is contained in:
Owen
2026-08-10 14:55:31 -04:00
parent a357f42c48
commit e76377d3c7
+4 -34
View File
@@ -5,39 +5,7 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import { APP_PATH } from "@server/lib/consts"; import { APP_PATH } from "@server/lib/consts";
import { existsSync, mkdirSync } from "fs"; import { existsSync, mkdirSync } from "fs";
import logger from "@server/logger";
// Temporary diagnostic trace for the random better-sqlite3 native aborts
// (Statement::~Statement -> RemoveEnvironmentCleanupHook assertion). That
// abort is a hard SIGABRT from native code, so it bypasses uncaughtException/
// unhandledRejection and can outrun winston's async file transport. This
// writes every statement text synchronously (fsync'd via appendFileSync) so
// the statements immediately preceding a crash survive it. better-sqlite3's
// `verbose` hook fires for BEGIN/SAVEPOINT/RELEASE/COMMIT/ROLLBACK too, since
// those are just prepared statements under the hood - so this also lets us
// see if two "transactions" ever overlap on the shared connection.
// Enable with SQL_TRACE=true; remove once the crash is root-caused.
function sqlTraceVerbose():
| ((message: unknown, ...args: unknown[]) => void)
| undefined {
if (process.env.SQL_TRACE !== "true") {
return undefined;
}
const traceLogDir = path.join(APP_PATH, "logs");
if (!existsSync(traceLogDir)) {
mkdirSync(traceLogDir, { recursive: true });
}
const traceLogPath = path.join(traceLogDir, "sql-trace.log");
let seq = 0;
return (message: unknown) => {
seq += 1;
const line = `${new Date().toISOString()} pid=${process.pid} #${seq} ${String(message).replace(/\s+/g, " ").trim()}\n`;
try {
fs.appendFileSync(traceLogPath, line);
} catch {
// best-effort diagnostic logging only
}
};
}
export const location = path.join(APP_PATH, "db", "db.sqlite"); export const location = path.join(APP_PATH, "db", "db.sqlite");
export const exists = checkFileExists(location); export const exists = checkFileExists(location);
@@ -46,7 +14,9 @@ bootstrapVolume();
function createDb() { function createDb() {
const verbose = const verbose =
process.env.QUERY_LOGGING == "true" ? sqlTraceVerbose() : undefined; process.env.QUERY_LOGGING == "true"
? (message: unknown) => logger.debug(String(message))
: undefined;
const sqlite = new Database(location, { verbose }); const sqlite = new Database(location, { verbose });
if (process.env.ENABLE_SQLITE_WAL_MODE == "true") { if (process.env.ENABLE_SQLITE_WAL_MODE == "true") {