diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-03-15 11:06:42 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-15 11:06:42 -0500 |
| commit | 0165b5f6cbae759acfe337170f831a11a45634e9 (patch) | |
| tree | 45d5ac0737b9ef1301131e06c728f90380d0f465 /src | |
| parent | 99c3632d24fccae01352c623affd6da10d0f6016 (diff) | |
| parent | 01fa711484e120f91d7d21e0957f98c0842551ad (diff) | |
| download | slskd-0165b5f6cbae759acfe337170f831a11a45634e9.tar.gz | |
Merge pull request #1670 from slskd/migrations
Add BatchId and Attempts to Transfers database, change migration naming scheme
Diffstat (limited to 'src')
10 files changed, 158 insertions, 26 deletions
diff --git a/src/slskd/Core/Data/Migrations/README.md b/src/slskd/Core/Data/Migrations/README.md index 2b5fd6f4..46da7ded 100644 --- a/src/slskd/Core/Data/Migrations/README.md +++ b/src/slskd/Core/Data/Migrations/README.md @@ -11,7 +11,7 @@ The `Migrator` creates a backup of each database prior to running any migrations Each migration must be created as a class that implements the `IMigration` interface. The naming convention for migration classes is: ``` -Z<MMDDYYYY>_<ShortDescription>Migration +Z<YYYY_MM_DD>_<ShortDescription>Migration ``` Migrations MUST: diff --git a/src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs b/src/slskd/Core/Data/Migrations/Z2025_04_01_TransferStateMigration.cs index c6e3d2f1..146541dd 100644 --- a/src/slskd/Core/Data/Migrations/Z04012025_TransferStateMigration.cs +++ b/src/slskd/Core/Data/Migrations/Z2025_04_01_TransferStateMigration.cs @@ -1,4 +1,4 @@ -// <copyright file="Z04012025_TransferStateMigration.cs" company="slskd Team"> +// <copyright file="Z2025_04_01_TransferStateMigration.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -37,14 +37,14 @@ using slskd.Transfers; /// as strings; it tries to use bitwise operators to apply HasFlags(), and these obviously don't /// work against strings. Not sure why EF didn't complain about this, but here we are. /// </summary> -public class Z04012025_TransferStateMigration : IMigration +public class Z2025_04_01_TransferStateMigration : IMigration { - public Z04012025_TransferStateMigration(ConnectionStringDictionary connectionStrings) + public Z2025_04_01_TransferStateMigration(ConnectionStringDictionary connectionStrings) { ConnectionString = connectionStrings[Database.Transfers]; } - private ILogger Log { get; } = Serilog.Log.ForContext<Z04012025_TransferStateMigration>(); + private ILogger Log { get; } = Serilog.Log.ForContext<Z2025_04_01_TransferStateMigration>(); private string ConnectionString { get; } public bool NeedsToBeApplied() @@ -61,7 +61,7 @@ public class Z04012025_TransferStateMigration : IMigration { if (!NeedsToBeApplied()) { - Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z04012025_TransferStateMigration)); + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2025_04_01_TransferStateMigration)); return; } diff --git a/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs b/src/slskd/Core/Data/Migrations/Z2025_07_06_TransferIndexesMigration.cs index 8ae13734..df93f685 100644 --- a/src/slskd/Core/Data/Migrations/Z07062025_TransferIndexesMigration.cs +++ b/src/slskd/Core/Data/Migrations/Z2025_07_06_TransferIndexesMigration.cs @@ -1,4 +1,4 @@ -// <copyright file="Z07062025_TransferIndexesMigration.cs" company="slskd Team"> +// <copyright file="Z2025_07_06_TransferIndexesMigration.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -25,14 +25,14 @@ using Serilog; /// <summary> /// Updates the Transfers table to add indexes on the Direction and State columns. /// </summary> -public class Z07062025_TransferIndexesMigration : IMigration +public class Z2025_07_06_TransferIndexesMigration : IMigration { - public Z07062025_TransferIndexesMigration(ConnectionStringDictionary connectionStrings) + public Z2025_07_06_TransferIndexesMigration(ConnectionStringDictionary connectionStrings) { ConnectionString = connectionStrings[Database.Transfers]; } - private ILogger Log { get; } = Serilog.Log.ForContext<Z07062025_TransferIndexesMigration>(); + private ILogger Log { get; } = Serilog.Log.ForContext<Z2025_07_06_TransferIndexesMigration>(); private string ConnectionString { get; } public bool NeedsToBeApplied() @@ -56,7 +56,7 @@ public class Z07062025_TransferIndexesMigration : IMigration { if (!NeedsToBeApplied()) { - Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z07062025_TransferIndexesMigration)); + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2025_07_06_TransferIndexesMigration)); return; } diff --git a/src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs b/src/slskd/Core/Data/Migrations/Z2025_07_26_PrivateMessageWasReplayedMigration.cs index a458a756..c3f3664b 100644 --- a/src/slskd/Core/Data/Migrations/Z07262025_PrivateMessageWasReplayedMigration.cs +++ b/src/slskd/Core/Data/Migrations/Z2025_07_26_PrivateMessageWasReplayedMigration.cs @@ -1,4 +1,4 @@ -// <copyright file="Z07262025_PrivateMessageWasReplayedMigration.cs" company="slskd Team"> +// <copyright file="Z2025_07_26_PrivateMessageWasReplayedMigration.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -26,19 +26,18 @@ using slskd.Messaging; /// <summary> /// Updates the Transfers table to add indexes on the Direction and State columns. /// </summary> -public class Z07262025_PrivateMessageWasReplayedMigration : IMigration +public class Z2025_07_26_PrivateMessageWasReplayedMigration : IMigration { - public Z07262025_PrivateMessageWasReplayedMigration(ConnectionStringDictionary connectionStrings) + public Z2025_07_26_PrivateMessageWasReplayedMigration(ConnectionStringDictionary connectionStrings) { ConnectionString = connectionStrings[Database.Messaging]; } - private ILogger Log { get; } = Serilog.Log.ForContext<Z07262025_PrivateMessageWasReplayedMigration>(); + private ILogger Log { get; } = Serilog.Log.ForContext<Z2025_07_26_PrivateMessageWasReplayedMigration>(); private string ConnectionString { get; } 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"]; @@ -54,7 +53,7 @@ public class Z07262025_PrivateMessageWasReplayedMigration : IMigration { if (!NeedsToBeApplied()) { - Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z07262025_PrivateMessageWasReplayedMigration)); + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2025_07_26_PrivateMessageWasReplayedMigration)); return; } diff --git a/src/slskd/Core/Data/Migrations/Z12282025_AdditionalTransferIndexesMigration.cs b/src/slskd/Core/Data/Migrations/Z2025_12_28_AdditionalTransferIndexesMigration.cs index 2deffeb3..e66cc59d 100644 --- a/src/slskd/Core/Data/Migrations/Z12282025_AdditionalTransferIndexesMigration.cs +++ b/src/slskd/Core/Data/Migrations/Z2025_12_28_AdditionalTransferIndexesMigration.cs @@ -1,4 +1,4 @@ -// <copyright file="Z12282025_AdditionalTransferIndexesMigration.cs" company="slskd Team"> +// <copyright file="Z2025_12_28_AdditionalTransferIndexesMigration.cs" company="slskd Team"> // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -25,14 +25,14 @@ using Serilog; /// <summary> /// Adds additional indexes to the Transfers table for Removed, Username/Filename, and user upload statistics. /// </summary> -public class Z12282025_AdditionalTransferIndexesMigration : IMigration +public class Z2025_12_28_AdditionalTransferIndexesMigration : IMigration { - public Z12282025_AdditionalTransferIndexesMigration(ConnectionStringDictionary connectionStrings) + public Z2025_12_28_AdditionalTransferIndexesMigration(ConnectionStringDictionary connectionStrings) { ConnectionString = connectionStrings[Database.Transfers]; } - private ILogger Log { get; } = Serilog.Log.ForContext<Z12282025_AdditionalTransferIndexesMigration>(); + private ILogger Log { get; } = Serilog.Log.ForContext<Z2025_12_28_AdditionalTransferIndexesMigration>(); private string ConnectionString { get; } public bool NeedsToBeApplied() @@ -57,7 +57,7 @@ public class Z12282025_AdditionalTransferIndexesMigration : IMigration { if (!NeedsToBeApplied()) { - Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z12282025_AdditionalTransferIndexesMigration)); + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2025_12_28_AdditionalTransferIndexesMigration)); return; } 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..5965f7b7 --- /dev/null +++ b/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndBatchIdMigration.cs @@ -0,0 +1,115 @@ +// <copyright file="Z2026_02_17_TransferAttemptsAndBatchIdMigration.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.Linq; +using Microsoft.Data.Sqlite; +using Serilog; +using slskd.Transfers; + +/// <summary> +/// Updates the Transfers table to add Attempts and BatchId columns. +/// </summary> +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<Z2026_02_17_TransferAttemptsAndBatchIdMigration>(); + 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.NextAttemptAt)) + && 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, Attempts, and NextAttemptAt 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;"); + } + + if (!columns.Any(c => c.Name == nameof(Transfer.NextAttemptAt))) + { + Exec("ALTER TABLE Transfers ADD COLUMN NextAttemptAt TEXT NULL;"); + } + + 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/Migrator.cs b/src/slskd/Core/Data/Migrator.cs index 5561de4b..c1089090 100644 --- a/src/slskd/Core/Data/Migrator.cs +++ b/src/slskd/Core/Data/Migrator.cs @@ -40,10 +40,11 @@ public class Migrator Migrations = new() { - { nameof(Z04012025_TransferStateMigration), new Z04012025_TransferStateMigration(connectionStrings: Databases) }, - { nameof(Z07062025_TransferIndexesMigration), new Z07062025_TransferIndexesMigration(connectionStrings: Databases) }, - { nameof(Z07262025_PrivateMessageWasReplayedMigration), new Z07262025_PrivateMessageWasReplayedMigration(connectionStrings: Databases) }, - { nameof(Z12282025_AdditionalTransferIndexesMigration), new Z12282025_AdditionalTransferIndexesMigration(connectionStrings: Databases) }, + { nameof(Z2025_04_01_TransferStateMigration), new Z2025_04_01_TransferStateMigration(connectionStrings: Databases) }, + { 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_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 422595e9..d82259e7 100644 --- a/src/slskd/Transfers/TransfersDbContext.cs +++ b/src/slskd/Transfers/TransfersDbContext.cs @@ -76,6 +76,16 @@ namespace slskd.Transfers .HasIndex(t => t.Removed) .HasDatabaseName("IDX_Transfers_Removed"); + modelBuilder + .Entity<Transfer>() + .Property(e => e.Attempts) + .HasDefaultValue(0); // force EF to match the migration + + modelBuilder + .Entity<Transfer>() + .HasIndex(t => t.BatchId) + .HasDatabaseName("IDX_Transfers_BatchId"); + // covers the check for existing records when enqueueing uploads and downloads modelBuilder .Entity<Transfer>() diff --git a/src/slskd/Transfers/Types/Transfer.cs b/src/slskd/Transfers/Types/Transfer.cs index 338aba38..7facb3ee 100644 --- a/src/slskd/Transfers/Types/Transfer.cs +++ b/src/slskd/Transfers/Types/Transfer.cs @@ -25,6 +25,8 @@ namespace slskd.Transfers public class Transfer { + public Guid? BatchId { get; init; } = null; + [Key] public Guid Id { get; init; } public string Username { get; init; } @@ -65,6 +67,10 @@ namespace slskd.Transfers public int? PlaceInQueue { get; set; } public string Exception { get; set; } + public int Attempts { get; set; } = 0; + + public DateTime? NextAttemptAt { get; set; } = null; + [JsonIgnore] public bool Removed { get; set; } |
