diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-16 15:27:30 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-16 15:27:30 -0600 |
| commit | 276857f172849f934ffce57194c070f560ff89be (patch) | |
| tree | 5f1873c14d32cc417dfad9f5e8e473811c971a0a /src | |
| parent | 27aaa276ae2365f336ac2c963856727bb19750bb (diff) | |
| download | slskd-276857f172849f934ffce57194c070f560ff89be.tar.gz | |
create and set pragmas for databases during initialization
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Program.cs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 742ee51e..b59fca83 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1216,11 +1216,48 @@ 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 + */ + 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) |
