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') 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 30889200a22de1364ffdf1824fa209ec4144ef1b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Nov 2025 15:28:04 -0600 Subject: remove redundant database creation --- src/slskd/Events/EventsDbContext.cs | 1 - src/slskd/Messaging/MessagingDbContext.cs | 1 - src/slskd/Search/SearchDbContext.cs | 1 - src/slskd/Transfers/TransfersDbContext.cs | 1 - 4 files changed, 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Events/EventsDbContext.cs b/src/slskd/Events/EventsDbContext.cs index a86f5d1c..8b45a803 100644 --- a/src/slskd/Events/EventsDbContext.cs +++ b/src/slskd/Events/EventsDbContext.cs @@ -25,7 +25,6 @@ public class EventsDbContext : DbContext public EventsDbContext(DbContextOptions options) : base(options) { - Database.EnsureCreated(); } public DbSet Events { get; set; } diff --git a/src/slskd/Messaging/MessagingDbContext.cs b/src/slskd/Messaging/MessagingDbContext.cs index 5acb94ae..c6352c6e 100644 --- a/src/slskd/Messaging/MessagingDbContext.cs +++ b/src/slskd/Messaging/MessagingDbContext.cs @@ -25,7 +25,6 @@ namespace slskd.Messaging public MessagingDbContext(DbContextOptions options) : base(options) { - Database.EnsureCreated(); } public DbSet Conversations { get; set; } diff --git a/src/slskd/Search/SearchDbContext.cs b/src/slskd/Search/SearchDbContext.cs index bc6ee147..1d710e42 100644 --- a/src/slskd/Search/SearchDbContext.cs +++ b/src/slskd/Search/SearchDbContext.cs @@ -25,7 +25,6 @@ namespace slskd.Search public SearchDbContext(DbContextOptions options) : base(options) { - Database.EnsureCreated(); } public DbSet Searches { get; set; } diff --git a/src/slskd/Transfers/TransfersDbContext.cs b/src/slskd/Transfers/TransfersDbContext.cs index ea138bcd..eac4935b 100644 --- a/src/slskd/Transfers/TransfersDbContext.cs +++ b/src/slskd/Transfers/TransfersDbContext.cs @@ -26,7 +26,6 @@ namespace slskd.Transfers public TransfersDbContext(DbContextOptions options) : base(options) { - Database.EnsureCreated(); } public DbSet Transfers { get; set; } -- cgit v1.2.3 From 83a64887d3ef6fea35e4752ebcf80f10a4a6eaca Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Nov 2025 15:28:24 -0600 Subject: set synchronous pragma for shares to off --- src/slskd/Shares/SqliteShareRepository.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index f457adba..711bb21e 100644 --- a/src/slskd/Shares/SqliteShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -157,7 +157,9 @@ namespace slskd.Shares { using var conn = GetConnection(); - conn.ExecuteNonQuery("PRAGMA journal_mode=WAL"); + // warning: with synchronous=0, an operating system crash can leave the database corrupted. + // if this occurs we should generate a new one, so this is _probably_ ok + conn.ExecuteNonQuery("PRAGMA journal_mode=WAL; PRAGMA synchronous=0;"); if (discardExisting) { -- 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') 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') 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