From 54ac8fa919826482ac4eb0571291050a0223fe97 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 17 Jan 2023 19:53:21 +0100 Subject: [PATCH] sqlite.cc: Add SQL tracing Set environment variable NIX_DEBUG_SQLITE_TRACES=1 to log all sql statements. (cherry picked from commit 8a0ef5d58e0bb59eda0b8fd5622f2d731016f7a3) --- src/libstore/sqlite.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libstore/sqlite.cc b/src/libstore/sqlite.cc index 2090beabd..d238960b1 100644 --- a/src/libstore/sqlite.cc +++ b/src/libstore/sqlite.cc @@ -36,6 +36,15 @@ SQLiteError::SQLiteError(const char *path, int errNo, int extendedErrNo, hintfor throw SQLiteError(path, err, exterr, std::move(hf)); } +static void traceSQL(void * x, const char * sql) +{ + // wacky delimiters: + // so that we're quite unambiguous without escaping anything + // notice instead of trace: + // so that this can be enabled without getting the firehose in our face. + notice("SQL<[%1%]>", sql); +}; + SQLite::SQLite(const Path & path, bool create) { // useSQLiteWAL also indicates what virtual file system we need. Using @@ -49,6 +58,11 @@ SQLite::SQLite(const Path & path, bool create) if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK) SQLiteError::throw_(db, "setting timeout"); + if (getEnv("NIX_DEBUG_SQLITE_TRACES") == "1") { + // To debug sqlite statements; trace all of them + sqlite3_trace(db, &traceSQL, nullptr); + } + exec("pragma foreign_keys = 1"); }