diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-16 18:38:40 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-16 18:38:40 -0600 |
| commit | e80a47cd2c7ff2bbae682f64a448eaafbff59128 (patch) | |
| tree | 7fe63e661e8a1be00f455b67b3277f1487c71830 /src/slskd/Program.cs | |
| parent | e763715bd89cbf8c10537fc72905d4358caf8215 (diff) | |
| parent | 3d4661d3eec83d9c42932501c340abe27f201f8a (diff) | |
| download | slskd-e80a47cd2c7ff2bbae682f64a448eaafbff59128.tar.gz | |
Merge branch 'master' into unix-socket-validation
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 40 |
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) |
