aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Program.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-11-16 18:38:04 -0600
committerGitHub <noreply@github.com>2025-11-16 18:38:04 -0600
commit3d4661d3eec83d9c42932501c340abe27f201f8a (patch)
tree66fd158fd75639b317ec48561f00102f0064088b /src/slskd/Program.cs
parent27aaa276ae2365f336ac2c963856727bb19750bb (diff)
parent70f16315cf407bdaa5487e9b818acd98704714cf (diff)
downloadslskd-3d4661d3eec83d9c42932501c340abe27f201f8a.tar.gz
Merge pull request #1529 from slskd/sqlite-again
Ensure SQLite PRAGMAs journal_mode=wal and synchronous=1 (NORMAL) are set during initialization
Diffstat (limited to 'src/slskd/Program.cs')
-rw-r--r--src/slskd/Program.cs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs
index 742ee51e..ee9e2d0c 100644
--- a/src/slskd/Program.cs
+++ b/src/slskd/Program.cs
@@ -1209,6 +1209,7 @@ namespace slskd
services.AddDbContextFactory<T>(options =>
{
options.UseSqlite(connectionString);
+ options.AddInterceptors(new SqliteConnectionOpenedInterceptor());
if (OptionsAtStartup.Debug && OptionsAtStartup.Flags.LogSQL)
{
@@ -1216,11 +1217,50 @@ namespace slskd
}
});
+ /*
+ instantiate the DbContext and make sure it is created
+ */
using var ctx = services
.BuildServiceProvider()
.GetRequiredService<IDbContextFactory<T>>()
.CreateDbContext();
+ Log.Debug("Ensuring {Contex} is created", typeof(T).Name);
+ ctx.Database.EnsureCreated();
+
+ /*
+ set (and validate) our desired PRAGMAs
+
+ synchronous mode is also set upon every connection via SqliteConnectionOpenedInterceptor.
+ */
+ ctx.Database.OpenConnection();
+ var conn = ctx.Database.GetDbConnection();
+
+ Log.Debug("Setting PRAGMAs for {Contex}", typeof(T).Name);
+ using var initCommand = conn.CreateCommand();
+ initCommand.CommandText = "PRAGMA journal_mode=WAL; PRAGMA synchronous=1; PRAGMA optimize;";
+ initCommand.ExecuteNonQuery();
+
+ using var journalCmd = conn.CreateCommand();
+ journalCmd.CommandText = "PRAGMA journal_mode;";
+ var journalMode = journalCmd.ExecuteScalar()?.ToString();
+
+ if (!journalMode.Equals("WAL", StringComparison.OrdinalIgnoreCase))
+ {
+ Log.Warning("Failed to set database {Type} journal_mode PRAGMA to WAL; performance may be reduced", typeof(T).Name);
+ }
+
+ using var syncCmd = conn.CreateCommand();
+ syncCmd.CommandText = "PRAGMA synchronous;";
+ var sync = syncCmd.ExecuteScalar()?.ToString();
+
+ if (!sync.Equals("1", StringComparison.OrdinalIgnoreCase))
+ {
+ Log.Warning("Failed to set database {Type} synchronous PRAGMA to 1; performance may be reduced", typeof(T).Name);
+ }
+
+ Log.Debug("PRAGMAs for {Context}: journal_mode={JournalMode}, synchronous={Synchronous}", typeof(T).Name, journalMode, sync);
+
return services;
}
catch (Exception ex)