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 | |
| parent | e763715bd89cbf8c10537fc72905d4358caf8215 (diff) | |
| parent | 3d4661d3eec83d9c42932501c340abe27f201f8a (diff) | |
| download | slskd-e80a47cd2c7ff2bbae682f64a448eaafbff59128.tar.gz | |
Merge branch 'master' into unix-socket-validation
Diffstat (limited to 'src/slskd')
| -rw-r--r-- | src/slskd/Core/Data/SqlitePragmaInterceptor.cs | 55 | ||||
| -rw-r--r-- | src/slskd/Events/EventsDbContext.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Messaging/MessagingDbContext.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 40 | ||||
| -rw-r--r-- | src/slskd/Search/SearchDbContext.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Shares/SqliteShareRepository.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Transfers/TransfersDbContext.cs | 1 |
7 files changed, 98 insertions, 5 deletions
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 file="SqlitePragmaInterceptor.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd +{ + using System.Data.Common; + using System.Threading; + using System.Threading.Tasks; + using Microsoft.Data.Sqlite; + using Microsoft.EntityFrameworkCore.Diagnostics; + + /// <summary> + /// Intercepts SQLite database connections to set PRAGMAs immediately after opening. + /// </summary> + 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/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<EventsDbContext> options) : base(options) { - Database.EnsureCreated(); } public DbSet<EventRecord> 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<MessagingDbContext> options) : base(options) { - Database.EnsureCreated(); } public DbSet<Conversation> Conversations { get; set; } 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) 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<SearchDbContext> options) : base(options) { - Database.EnsureCreated(); } public DbSet<Search> Searches { get; set; } 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) { 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<TransfersDbContext> options) : base(options) { - Database.EnsureCreated(); } public DbSet<Transfer> Transfers { get; set; } |
