From 61d913baa9fd95a739ef2a0e39e452bf11a9772c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 09:58:26 +0000 Subject: [PATCH] fix(data): probe PRAGMA compile_options next to the SQLite version check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BRIEF §6.2 asks for the probe at startup. The options are read once per process from an in-memory connection, logged with the library version and added to the desktop diagnostics bundle; nothing depends on them (no FTS or JSON use). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- .../Infrastructure/DiagnosticsBundle.cs | 12 ++++- src/Titano.Data/Sqlite/SqliteDatabase.cs | 47 +++++++++++++++++++ .../Sqlite/SqliteBindingTests.cs | 13 +++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Titano.App/Infrastructure/DiagnosticsBundle.cs b/src/Titano.App/Infrastructure/DiagnosticsBundle.cs index bb00d67c..084b1819 100644 --- a/src/Titano.App/Infrastructure/DiagnosticsBundle.cs +++ b/src/Titano.App/Infrastructure/DiagnosticsBundle.cs @@ -8,6 +8,7 @@ using Titano.Core.Export; using Titano.Core.Model; using Titano.Core.Windows.Codecs; using Titano.Data; +using Titano.Data.Sqlite; namespace Titano.App.Infrastructure; @@ -73,8 +74,15 @@ public static class DiagnosticsBundle return sb.ToString(); } - private static string DatabaseLine(TitanoDatabase? database) => - database is null ? "Index not open\n" : string.Create(CultureInfo.InvariantCulture, $"Index schema {database.SchemaVersion}\n"); + private static string DatabaseLine(TitanoDatabase? database) + { + if (database is null) return "Index not open\n"; + var line = string.Create(CultureInfo.InvariantCulture, $"Index schema {database.SchemaVersion}\n"); + // The SQLite build's options (BRIEF §6.2) explain behaviour differences between Windows versions. + return database.Database is SqliteDatabase + ? line + $"SQLite {SqliteDatabase.LibraryVersion}; compile options: {string.Join(' ', SqliteDatabase.CompileOptions)}\n" + : line; + } /// Graphics adapter, video encoders, WIC decoders per format and missing Store extensions. private static string Codecs(IEncoderInventory encoders) diff --git a/src/Titano.Data/Sqlite/SqliteDatabase.cs b/src/Titano.Data/Sqlite/SqliteDatabase.cs index f7dc78e9..f9b56736 100644 --- a/src/Titano.Data/Sqlite/SqliteDatabase.cs +++ b/src/Titano.Data/Sqlite/SqliteDatabase.cs @@ -37,6 +37,7 @@ public sealed class SqliteDatabase : IDatabase internal const int BusyTimeoutMs = 5000; private const string LogCategory = "Data.Sqlite"; private static readonly TimeSpan WriteLockTimeout = TimeSpan.FromSeconds(60); + private static readonly Lazy> ProbedCompileOptions = new(ProbeCompileOptions); private readonly string _filename; private readonly bool _memory; @@ -84,9 +85,24 @@ public sealed class SqliteDatabase : IDatabase } } + /// + /// The library's PRAGMA compile_options (BRIEF §6.2), probed and logged once per process; empty when the + /// probe fails. Informational only: Titano uses no optional feature (FTS, JSON), so nothing depends on it; it goes to + /// the log and the diagnostics bundle. + /// + public static IReadOnlyList CompileOptions + { + get + { + EnsureLibrary(); + return ProbedCompileOptions.Value; + } + } + internal static SqliteDatabase Open(SqliteSettings settings) { EnsureLibrary(); + _ = ProbedCompileOptions.Value; var database = new SqliteDatabase(settings); // The first connection validates the path, switches the file to WAL and keeps a memory database alive. database._idle.Push(database.CreateConnection()); @@ -259,6 +275,37 @@ public sealed class SqliteDatabase : IDatabase throw new NotSupportedException($"SQLite {version / 1_000_000}.{version / 1000 % 1000}.{version % 1000} is too old; 3.31 or newer is required."); } + private static IReadOnlyList ProbeCompileOptions() + { + var options = new List(); + try + { + using var connection = SqliteConnection.Open(":memory:", isMemory: true, BusyTimeoutMs); + var statement = connection.Rent("PRAGMA compile_options"); + try + { + var columns = statement.ReadColumns(); + int rc; + while ((rc = statement.Step()) == SQLITE_ROW) + { + if (statement.ReadRow(columns).GetStringOrNull(0) is { Length: > 0 } option) options.Add(option); + } + if (rc != SQLITE_DONE) throw connection.Error(rc); + } + finally + { + connection.Return(statement); + } + } + catch (SqliteException ex) + { + Log.Warning(LogCategory, "PRAGMA compile_options could not be read", ex); + return []; + } + Log.Info(LogCategory, $"SQLite {LibraryVersion}; compile options: {string.Join(' ', options)}"); + return options; + } + private static void Validate(string backupPath) { try diff --git a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs index f2d2b576..4fe19f6e 100644 --- a/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs +++ b/tests/Titano.Data.Tests/Sqlite/SqliteBindingTests.cs @@ -15,6 +15,19 @@ public sealed class SqliteBindingTests(TestContext context) : DatabaseTest(conte Assert.True(parts[0] > 3 || (parts[0] == 3 && parts[1] >= 31), $"SQLite {SqliteDatabase.LibraryVersion}"); } + [Test] + public async Task CompileOptionsAreProbedOnceAndMatchTheLibrary() + { + await using var database = await OpenDatabaseAsync(); + var options = SqliteDatabase.CompileOptions; + // Informational and probed once: the same list on every call, and what any connection reports. + Assert.Same(options, SqliteDatabase.CompileOptions); + await using var session = await database.OpenAsync(Ct); + Assert.SequenceEqual(await session.QueryListAsync("PRAGMA compile_options", [], r => r.GetString(0), Ct), options); + // Linux libraries keep the diagnostics (a Windows build may omit them, which leaves the list empty). + if (!OperatingSystem.IsWindows()) Assert.Contains(options, o => o.StartsWith("THREADSAFE=", StringComparison.Ordinal)); + } + [Test] public async Task ConnectionsUseTheBriefPragmas() {