fix(data): probe PRAGMA compile_options next to the SQLite version check
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
3 files changed
+70
-2
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>Graphics adapter, video encoders, WIC decoders per format and missing Store extensions.</summary>
|
||||
private static string Codecs(IEncoderInventory encoders)
|
||||
|
||||
@@ -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<IReadOnlyList<string>> ProbedCompileOptions = new(ProbeCompileOptions);
|
||||
|
||||
private readonly string _filename;
|
||||
private readonly bool _memory;
|
||||
@@ -84,9 +85,24 @@ public sealed class SqliteDatabase : IDatabase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The library's <c>PRAGMA compile_options</c> (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.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<string> 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<string> ProbeCompileOptions()
|
||||
{
|
||||
var options = new List<string>();
|
||||
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
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user