From 276857f172849f934ffce57194c070f560ff89be Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Nov 2025 15:27:30 -0600 Subject: create and set pragmas for databases during initialization --- src/slskd/Program.cs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/slskd/Program.cs') 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>() .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) -- cgit v1.2.3 From 4e826f74bfb40a8741c78b2a41d4afdf9a34699c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Nov 2025 15:46:57 -0600 Subject: add note about synchronous --- src/slskd/Program.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index b59fca83..639d3a88 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1229,6 +1229,10 @@ namespace slskd /* set (and validate) our desired PRAGMAs + + note: synchronous mode wants to default to 2, and setting it here may not actually do anything + an alternative would be to set it at the start of each new connection, but i'm unsure if that + would have the intended effect. revisit later. */ ctx.Database.OpenConnection(); var conn = ctx.Database.GetDbConnection(); -- cgit v1.2.3 From 70f16315cf407bdaa5487e9b818acd98704714cf Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Nov 2025 18:34:28 -0600 Subject: use an interceptor to set synchronous pragma for each connection --- src/slskd/Core/Data/SqlitePragmaInterceptor.cs | 55 ++++++++++++++++++++++++++ src/slskd/Program.cs | 5 +-- 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/slskd/Core/Data/SqlitePragmaInterceptor.cs (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Core/Data/SqlitePragmaInterceptor.cs b/src/slskd/Core/Data/SqlitePragmaInterceptor.cs new file mode 100644 index 00000000..b32982b1 --- /dev/null +++ b/src/slskd/Core/Data/SqlitePragmaInterceptor.cs @@ -0,0 +1,55 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd +{ + using System.Data.Common; + using System.Threading; + using System.Threading.Tasks; + using Microsoft.Data.Sqlite; + using Microsoft.EntityFrameworkCore.Diagnostics; + + /// + /// Intercepts SQLite database connections to set PRAGMAs immediately after opening. + /// + public class SqliteConnectionOpenedInterceptor : DbConnectionInterceptor + { + public override void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData) + { + if (connection is SqliteConnection) + { + using var cmd = connection.CreateCommand(); + cmd.CommandText = "PRAGMA synchronous=1;"; + cmd.ExecuteNonQuery(); + } + + base.ConnectionOpened(connection, eventData); + } + + public override async Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = default) + { + if (connection is SqliteConnection) + { + using var cmd = connection.CreateCommand(); + cmd.CommandText = "PRAGMA synchronous=1;"; + await cmd.ExecuteNonQueryAsync(cancellationToken); + } + + await base.ConnectionOpenedAsync(connection, eventData, cancellationToken); + } + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 639d3a88..ee9e2d0c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1209,6 +1209,7 @@ namespace slskd services.AddDbContextFactory(options => { options.UseSqlite(connectionString); + options.AddInterceptors(new SqliteConnectionOpenedInterceptor()); if (OptionsAtStartup.Debug && OptionsAtStartup.Flags.LogSQL) { @@ -1230,9 +1231,7 @@ namespace slskd /* set (and validate) our desired PRAGMAs - note: synchronous mode wants to default to 2, and setting it here may not actually do anything - an alternative would be to set it at the start of each new connection, but i'm unsure if that - would have the intended effect. revisit later. + synchronous mode is also set upon every connection via SqliteConnectionOpenedInterceptor. */ ctx.Database.OpenConnection(); var conn = ctx.Database.GetDbConnection(); -- cgit v1.2.3