diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-28 18:44:56 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-12-28 18:44:56 -0600 |
| commit | d29e74c2eafa0f97d12c33be1e22e444a7618bd4 (patch) | |
| tree | 400169344a3ef1efbc709a788a21b341b6b804a9 /src | |
| parent | b082e89226234d00b73dcd753b301a70e2ac58bb (diff) | |
| download | slskd-d29e74c2eafa0f97d12c33be1e22e444a7618bd4.tar.gz | |
stub migration for new indexes
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Data/Migrations/Z12282025_AdditionalTransferIndexesMigration.cs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/slskd/Core/Data/Migrations/Z12282025_AdditionalTransferIndexesMigration.cs b/src/slskd/Core/Data/Migrations/Z12282025_AdditionalTransferIndexesMigration.cs new file mode 100644 index 00000000..6beec9d3 --- /dev/null +++ b/src/slskd/Core/Data/Migrations/Z12282025_AdditionalTransferIndexesMigration.cs @@ -0,0 +1,98 @@ +// <copyright file="Z12282025_AdditionalTransferIndexesMigration.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; + +/// <summary> +/// Updates the Transfers table to add indexes on the Direction and State columns. +/// </summary> +public class Z12282025_AdditionalTransferIndexesMigration : IMigration +{ + public Z12282025_AdditionalTransferIndexesMigration(ConnectionStringDictionary connectionStrings) + { + ConnectionString = connectionStrings[Database.Transfers]; + } + + private ILogger Log { get; } = Serilog.Log.ForContext<Z12282025_AdditionalTransferIndexesMigration>(); + 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 idxes = SchemaInspector.GetDatabaseIndexes(ConnectionString); + var txfers = idxes["Transfers"]; + + var usernameExists = txfers.Any(c => c.Name.Equals("IDX_Transfers_Username", StringComparison.OrdinalIgnoreCase)); + var removedExists = txfers.Any(c => c.Name.Equals("IDX_Transfers_Removed", StringComparison.OrdinalIgnoreCase)); + var statsExist = txfers.Any(c => c.Name.Equals("IDX_Transfers_UserUploadStatistics", StringComparison.OrdinalIgnoreCase)); + + if (usernameExists && removedExists && statsExist) + { + return false; + } + + return true; + } + + public void Apply() + { + if (!NeedsToBeApplied()) + { + Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z12282025_AdditionalTransferIndexesMigration)); + 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 usernameCommand = new SqliteCommand(@" + CREATE INDEX IF NOT EXISTS IDX_Transfers_Username ON Transfers (Username) + ", connection, transaction); + usernameCommand.ExecuteNonQuery(); + + var removedCommand = new SqliteCommand(@" + CREATE INDEX IF NOT EXISTS IDX_Transfers_Removed ON Transfers (Removed) + ", connection, transaction); + removedCommand.ExecuteNonQuery(); + + var statsCommand = new SqliteCommand(@" + CREATE INDEX IF NOT EXISTS IDX_Transfers_UserUploadStatistics ON Transfers (Username, Direction, EndedAt, StartedAt, State, Size) + ", connection, transaction); + statsCommand.ExecuteNonQuery(); + + Log.Information("> Index(es) created"); + transaction.Commit(); + Log.Information("> Done!"); + } + catch (Exception) + { + transaction.Rollback(); + throw; + } + } +} |
