aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core/Data
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-07-30 20:44:58 -0500
committerGitHub <noreply@github.com>2025-07-30 20:44:58 -0500
commitda71a74850b837ce6e831b9212714e510e72ca5e (patch)
tree523dc96ed715cf8098758365c6c9277c79b89fa1 /src/slskd/Core/Data
parent1fade356f1b8b38e269408aeec89e96e5fae5cb6 (diff)
parent5beeea03a1bc4f0c05467a7668c7f8a104f69206 (diff)
downloadslskd-da71a74850b837ce6e831b9212714e510e72ca5e.tar.gz
Merge pull request #1399 from slskd/msg-events
Add chat events for webhooks and scripts
Diffstat (limited to 'src/slskd/Core/Data')
-rw-r--r--src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs3
-rw-r--r--src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs81
-rw-r--r--src/slskd/Core/Data/Migrator.cs5
3 files changed, 88 insertions, 1 deletions
diff --git a/src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs b/src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs
index f7af80f5..0d540565 100644
--- a/src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs
+++ b/src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs
@@ -24,6 +24,7 @@ using System.IO;
using System.Linq;
using Microsoft.Data.Sqlite;
using Serilog;
+using slskd.Transfers;
/// <summary>
/// Updates the Transfers table to:
@@ -50,7 +51,7 @@ public class Z04012025_TransferStateMigration : IMigration
var schema = SchemaInspector.GetDatabaseSchema(ConnectionString);
var txfers = schema["Transfers"];
- return !txfers.Any(c => c.Name == "StateDescription");
+ return !txfers.Any(c => c.Name == nameof(Transfer.StateDescription));
}
public void Apply()
diff --git a/src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs b/src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs
new file mode 100644
index 00000000..2f587f9e
--- /dev/null
+++ b/src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs
@@ -0,0 +1,81 @@
+// <copyright file="Z07262025_PrivateMessageWasReplayedMigration.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.Migrations;
+
+using System;
+using System.IO;
+using System.Linq;
+using Microsoft.Data.Sqlite;
+using Serilog;
+using slskd.Messaging;
+
+/// <summary>
+/// Updates the Transfers table to add indexes on the Direction and State columns.
+/// </summary>
+public class Z07262025_PrivateMessageWasReplayedMigration : IMigration
+{
+ private ILogger Log { get; } = Serilog.Log.ForContext<Z07262025_PrivateMessageWasReplayedMigration>();
+ private string DatabaseName { get; } = "messaging";
+ private string ConnectionString => $"Data Source={Path.Combine(Program.DataDirectory, $"{DatabaseName}.db")}";
+
+ public bool NeedsToBeApplied()
+ {
+ // check to see if *BOTH* of the indexes are in place. if one or both are missing, we must apply
+ var schema = SchemaInspector.GetDatabaseSchema(ConnectionString);
+ var pms = schema["PrivateMessages"];
+
+ if (pms.Any(r => r.Name == nameof(PrivateMessage.WasReplayed)))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ public void Apply()
+ {
+ if (!NeedsToBeApplied())
+ {
+ Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z07262025_PrivateMessageWasReplayedMigration));
+ return;
+ }
+
+ using var connection = new SqliteConnection(ConnectionString);
+ connection.Open();
+
+ using var transaction = connection.BeginTransaction();
+
+ try
+ {
+ Log.Information("> Adding missing column WasReplayed to PrivateMessages table...");
+
+ var addColumnCommand = new SqliteCommand(@"
+ ALTER TABLE PrivateMessages ADD COLUMN WasReplayed INTEGER NOT NULL DEFAULT 0
+ ", connection, transaction);
+ addColumnCommand.ExecuteNonQuery();
+
+ transaction.Commit();
+ Log.Information("> Done!");
+ }
+ catch (Exception)
+ {
+ transaction.Rollback();
+ throw;
+ }
+ }
+}
diff --git a/src/slskd/Core/Data/Migrator.cs b/src/slskd/Core/Data/Migrator.cs
index 645f4575..9fe54024 100644
--- a/src/slskd/Core/Data/Migrator.cs
+++ b/src/slskd/Core/Data/Migrator.cs
@@ -45,10 +45,15 @@ public class Migrator
/// <summary>
/// A map of all migrations, to be applied in the order they are specified (descending).
/// </summary>
+ /// <remarks>
+ /// This could be done with reflection (e.g. find all instances of IMigration) but it's useful during development
+ /// to selectively add/comment out lines. Slight risk that something is missed, so this should be append only.
+ /// </remarks>
private Dictionary<string, IMigration> Migrations { get; } = new()
{
{ nameof(Z04012025_TransferStateMigration), new Z04012025_TransferStateMigration() },
{ nameof(Z07062025_TransferIndexesMigration), new Z07062025_TransferIndexesMigration() },
+ { nameof(Z07262025_PrivateMessageWasReplayedMigration), new Z07262025_PrivateMessageWasReplayedMigration() },
};
/// <summary>