diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-02-17 21:44:42 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-03-14 19:15:39 -0500 |
| commit | 3de4ecd75e033328874323043f662964e0f800ac (patch) | |
| tree | 43243a9c2ce482451eb41ee911c6fabbf7e8f85d /src | |
| parent | cca8a2d11aadfb311beaa9c6369791e4dea0cc63 (diff) | |
| download | slskd-3de4ecd75e033328874323043f662964e0f800ac.tar.gz | |
add Attempts and GroupId to Transfer
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs | 109 | ||||
| -rw-r--r-- | src/slskd/Core/Data/Migrator.cs | 9 | ||||
| -rw-r--r-- | src/slskd/Transfers/TransfersDbContext.cs | 10 | ||||
| -rw-r--r-- | src/slskd/Transfers/Types/Transfer.cs | 4 |
4 files changed, 128 insertions, 4 deletions
diff --git a/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs b/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs new file mode 100644 index 00000000..dfdd68eb --- /dev/null +++ b/src/slskd/Core/Data/Migrations/Z2026_02_17_TransferAttemptsAndGroupIdMigration.cs @@ -0,0 +1,109 @@ +// <copyright file="Z2026_02_17_TransferAttemptsAndGroupIdMigration.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 GroupId columns. +/// </summary> +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<Z2026_02_17_TransferAttemptsAndGroupIdMigration>(); + 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 5561de4b..a5096143 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_TransferAttemptsAndGroupIdMigration), new Z2026_02_17_TransferAttemptsAndGroupIdMigration(connectionStrings: Databases) }, }; } diff --git a/src/slskd/Transfers/TransfersDbContext.cs b/src/slskd/Transfers/TransfersDbContext.cs index 422595e9..79171809 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.GroupId) + .HasDatabaseName("IDX_Transfers_GroupId"); + // 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..e2ef6d5d 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? GroupId { get; init; } = null; + [Key] public Guid Id { get; init; } public string Username { get; init; } @@ -65,6 +67,8 @@ namespace slskd.Transfers public int? PlaceInQueue { get; set; } public string Exception { get; set; } + public int Attempts { get; set; } = 0; + [JsonIgnore] public bool Removed { get; set; } |
