From 6751f5bb1a34d030e6ce5078b20e57b6ea0053a5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 1 Mar 2026 18:52:02 -0600 Subject: rename GroupId to BatchId --- ...26_02_17_TransferAttemptsAndBatchIdMigration.cs | 109 +++++++++++++++++++++ ...26_02_17_TransferAttemptsAndGroupIdMigration.cs | 109 --------------------- src/slskd/Core/Data/Migrator.cs | 2 +- src/slskd/Transfers/Extensions.cs | 1 + src/slskd/Transfers/TransfersDbContext.cs | 4 +- src/slskd/Transfers/Types/Transfer.cs | 2 +- 6 files changed, 114 insertions(+), 113 deletions(-) create mode 100644 src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndBatchIdMigration.cs delete mode 100644 src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs (limited to 'src') diff --git a/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndBatchIdMigration.cs b/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndBatchIdMigration.cs new file mode 100644 index 00000000..66083a83 --- /dev/null +++ b/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndBatchIdMigration.cs @@ -0,0 +1,109 @@ +// +// 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.Linq; +using Microsoft.Data.Sqlite; +using Serilog; +using slskd.Transfers; + +/// +/// Updates the Transfers table to add Attempts and BatchId columns. +/// +public class Z2026_02_17_TransferAttemptsAndBatchIdMigration : IMigration +{ + public Z2026_02_17_TransferAttemptsAndBatchIdMigration(ConnectionStringDictionary connectionStrings) + { + ConnectionString = connectionStrings[Database.Transfers]; + } + + private ILogger Log { get; } = Serilog.Log.ForContext(); + private string ConnectionString { get; } + + public bool NeedsToBeApplied() + { + var schema = SchemaInspector.GetDatabaseSchema(ConnectionString); + var idxes = SchemaInspector.GetDatabaseIndexes(ConnectionString); + + var columns = schema["Transfers"]; + var indexes = idxes["Transfers"]; + + // check to see if the Attempts and BatchId columns exist + if (columns.Any(c => c.Name == nameof(Transfer.Attempts)) + && columns.Any(c => c.Name == nameof(Transfer.BatchId)) + && indexes.Any(i => i.Name.Equals("IDX_Transfers_BatchId", StringComparison.OrdinalIgnoreCase))) + { + return false; + } + + return true; + } + + public void Apply() + { + if (!NeedsToBeApplied()) + { + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2026_02_17_TransferAttemptsAndBatchIdMigration)); + return; + } + + var columns = SchemaInspector.GetDatabaseSchema(ConnectionString)["Transfers"]; + + using var connection = new SqliteConnection(ConnectionString); + connection.Open(); + + using var transaction = connection.BeginTransaction(); + + try + { + void Exec(string sql) + { + using var command = new SqliteCommand(sql, connection, transaction); + command.ExecuteNonQuery(); + } + + Log.Information("> Adding BatchId and Attempts columns to the Transfers table..."); + + if (!columns.Any(c => c.Name == nameof(Transfer.BatchId))) + { + Exec("ALTER TABLE Transfers ADD COLUMN BatchId TEXT NULL;"); + } + + if (!columns.Any(c => c.Name == nameof(Transfer.Attempts))) + { + Exec("ALTER TABLE Transfers ADD COLUMN Attempts INTEGER NOT NULL DEFAULT 0;"); + } + + Log.Information("> New columns added"); + + Log.Information("> Adding missing index(es) on the Transfers table..."); + + Exec("CREATE INDEX IF NOT EXISTS IDX_Transfers_BatchId ON Transfers (BatchId)"); + + Log.Information("> Index(es) created"); + transaction.Commit(); + Log.Information("> Done!"); + } + catch (Exception) + { + transaction.Rollback(); + throw; + } + } +} diff --git a/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs b/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs deleted file mode 100644 index dfdd68eb..00000000 --- a/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs +++ /dev/null @@ -1,109 +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.Linq; -using Microsoft.Data.Sqlite; -using Serilog; -using slskd.Transfers; - -/// -/// Updates the Transfers table to add Attempts and GroupId columns. -/// -public class Z2026_02_17_TransferAttemptsAndGroupIdMigration : IMigration -{ - public Z2026_02_17_TransferAttemptsAndGroupIdMigration(ConnectionStringDictionary connectionStrings) - { - ConnectionString = connectionStrings[Database.Transfers]; - } - - private ILogger Log { get; } = Serilog.Log.ForContext(); - private string ConnectionString { get; } - - public bool NeedsToBeApplied() - { - var schema = SchemaInspector.GetDatabaseSchema(ConnectionString); - var idxes = SchemaInspector.GetDatabaseIndexes(ConnectionString); - - var columns = schema["Transfers"]; - var indexes = idxes["Transfers"]; - - // check to see if the Attempts and GroupId columns exist - if (columns.Any(c => c.Name == nameof(Transfer.Attempts)) - && columns.Any(c => c.Name == nameof(Transfer.GroupId)) - && indexes.Any(i => i.Name.Equals("IDX_Transfers_GroupId", StringComparison.OrdinalIgnoreCase))) - { - return false; - } - - return true; - } - - public void Apply() - { - if (!NeedsToBeApplied()) - { - Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2026_02_17_TransferAttemptsAndGroupIdMigration)); - return; - } - - var columns = SchemaInspector.GetDatabaseSchema(ConnectionString)["Transfers"]; - - using var connection = new SqliteConnection(ConnectionString); - connection.Open(); - - using var transaction = connection.BeginTransaction(); - - try - { - void Exec(string sql) - { - using var command = new SqliteCommand(sql, connection, transaction); - command.ExecuteNonQuery(); - } - - Log.Information("> Adding GroupId and Attempts columns to the Transfers table..."); - - if (!columns.Any(c => c.Name == nameof(Transfer.GroupId))) - { - Exec("ALTER TABLE Transfers ADD COLUMN GroupId TEXT NULL;"); - } - - if (!columns.Any(c => c.Name == nameof(Transfer.Attempts))) - { - Exec("ALTER TABLE Transfers ADD COLUMN Attempts INTEGER NOT NULL DEFAULT 0;"); - } - - Log.Information("> New columns added"); - - Log.Information("> Adding missing index(es) on the Transfers table..."); - - Exec("CREATE INDEX IF NOT EXISTS IDX_Transfers_GroupId ON Transfers (GroupId)"); - - Log.Information("> Index(es) created"); - 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 a5096143..c1089090 100644 --- a/src/slskd/Core/Data/Migrator.cs +++ b/src/slskd/Core/Data/Migrator.cs @@ -44,7 +44,7 @@ public class Migrator { nameof(Z2025_07_06_TransferIndexesMigration), new Z2025_07_06_TransferIndexesMigration(connectionStrings: Databases) }, { nameof(Z2025_07_26_PrivateMessageWasReplayedMigration), new Z2025_07_26_PrivateMessageWasReplayedMigration(connectionStrings: Databases) }, { nameof(Z2025_12_28_AdditionalTransferIndexesMigration), new Z2025_12_28_AdditionalTransferIndexesMigration(connectionStrings: Databases) }, - { nameof(Z2026_02_17_TransferAttemptsAndGroupIdMigration), new Z2026_02_17_TransferAttemptsAndGroupIdMigration(connectionStrings: Databases) }, + { nameof(Z2026_02_17_TransferAttemptsAndBatchIdMigration), new Z2026_02_17_TransferAttemptsAndBatchIdMigration(connectionStrings: Databases) }, }; } diff --git a/src/slskd/Transfers/Extensions.cs b/src/slskd/Transfers/Extensions.cs index 7ec1b55c..e2295e10 100644 --- a/src/slskd/Transfers/Extensions.cs +++ b/src/slskd/Transfers/Extensions.cs @@ -23,6 +23,7 @@ namespace slskd.Transfers { return new Transfer() { + BatchId = transfer.BatchId, Id = transfer.Id, Username = transfer.Username, Direction = transfer.Direction, diff --git a/src/slskd/Transfers/TransfersDbContext.cs b/src/slskd/Transfers/TransfersDbContext.cs index 79171809..d82259e7 100644 --- a/src/slskd/Transfers/TransfersDbContext.cs +++ b/src/slskd/Transfers/TransfersDbContext.cs @@ -83,8 +83,8 @@ namespace slskd.Transfers modelBuilder .Entity() - .HasIndex(t => t.GroupId) - .HasDatabaseName("IDX_Transfers_GroupId"); + .HasIndex(t => t.BatchId) + .HasDatabaseName("IDX_Transfers_BatchId"); // covers the check for existing records when enqueueing uploads and downloads modelBuilder diff --git a/src/slskd/Transfers/Types/Transfer.cs b/src/slskd/Transfers/Types/Transfer.cs index e2ef6d5d..56f6b333 100644 --- a/src/slskd/Transfers/Types/Transfer.cs +++ b/src/slskd/Transfers/Types/Transfer.cs @@ -25,7 +25,7 @@ namespace slskd.Transfers public class Transfer { - public Guid? GroupId { get; init; } = null; + public Guid? BatchId { get; init; } = null; [Key] public Guid Id { get; init; } -- cgit v1.2.3