From 871999808e44c60a6939aa2361204a7be03f0cd1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 27 Jul 2025 13:50:26 -0500 Subject: add migration for WasReplayed --- .../Z07262025_MessagingColumnsMigration.cs | 90 ---------------------- ...Z07262025_PrivateMessageWasReplayedMigration.cs | 81 +++++++++++++++++++ 2 files changed, 81 insertions(+), 90 deletions(-) delete mode 100644 src/slskd/Core/Data/Migrations/Z07262025_MessagingColumnsMigration.cs create mode 100644 src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs (limited to 'src') diff --git a/src/slskd/Core/Data/Migrations/Z07262025_MessagingColumnsMigration.cs b/src/slskd/Core/Data/Migrations/Z07262025_MessagingColumnsMigration.cs deleted file mode 100644 index ecd3f29f..00000000 --- a/src/slskd/Core/Data/Migrations/Z07262025_MessagingColumnsMigration.cs +++ /dev/null @@ -1,90 +0,0 @@ -// -// 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.Migrations; - -using System; -using System.IO; -using System.Linq; -using Microsoft.Data.Sqlite; -using Serilog; - -/// -/// Updates the Transfers table to add indexes on the Direction and State columns. -/// -public class Z07262025_MessagingColumnsMigration : IMigration -{ - // private ILogger Log { get; } = Serilog.Log.ForContext(); - // private string DatabaseName { get; } = "transfers"; - // 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 idxes = SchemaInspector.GetDatabaseIndexes(ConnectionString); - // var txfers = idxes["Transfers"]; - - // var directionExists = txfers.Any(c => c.Name == "IDX_Transfers_Direction"); - // var stateExists = txfers.Any(c => c.Name == "IDX_Transfers_State"); - - // if (directionExists && stateExists) - // { - // return false; - // } - - // return true; - // } - - // public void Apply() - // { - // if (!NeedsToBeApplied()) - // { - // Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z07062025_TransferIndexesMigration)); - // return; - // } - - // using var connection = new SqliteConnection(ConnectionString); - // connection.Open(); - - // using var transaction = connection.BeginTransaction(); - - // try - // { - // Log.Information("> Adding missing index(es) on the Transfers table..."); - - // var directionCommand = new SqliteCommand(@" - // CREATE INDEX IF NOT EXISTS IDX_Transfers_Direction ON Transfers (Direction) - // ", connection, transaction); - // directionCommand.ExecuteNonQuery(); - - // var stateCommand = new SqliteCommand(@" - // CREATE INDEX IF NOT EXISTS IDX_Transfers_State ON Transfers (State) - // ", connection, transaction); - // stateCommand.ExecuteNonQuery(); - - // Log.Information("> Index(es) created"); - // transaction.Commit(); - // Log.Information("> Done!"); - // } - // catch (Exception) - // { - // transaction.Rollback(); - // throw; - // } - // } -} 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 (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.Migrations; + +using System; +using System.IO; +using System.Linq; +using Microsoft.Data.Sqlite; +using Serilog; +using slskd.Messaging; + +/// +/// Updates the Transfers table to add indexes on the Direction and State columns. +/// +public class Z07262025_PrivateMessageWasReplayedMigration : IMigration +{ + private ILogger Log { get; } = Serilog.Log.ForContext(); + 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; + } + } +} -- cgit v1.2.3