//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ 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, version 3.
// │
// │ 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/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
namespace slskd.Migrations;
using System;
using System.Linq;
using Microsoft.Data.Sqlite;
using Serilog;
///
/// Adds additional indexes to the Transfers table for Removed, Username/Filename, and user upload statistics.
///
public class Z2025_12_28_AdditionalTransferIndexesMigration : IMigration
{
public Z2025_12_28_AdditionalTransferIndexesMigration(ConnectionStringDictionary connectionStrings)
{
ConnectionString = connectionStrings[Database.Transfers];
}
private ILogger Log { get; } = Serilog.Log.ForContext();
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 removedExists = txfers.Any(c => c.Name.Equals("IDX_Transfers_Removed", StringComparison.OrdinalIgnoreCase));
var usernameFilenameExists = txfers.Any(c => c.Name.Equals("IDX_Transfers_UsernameFilename", StringComparison.OrdinalIgnoreCase));
var statsExist = txfers.Any(c => c.Name.Equals("IDX_Transfers_UserUploadStatistics", StringComparison.OrdinalIgnoreCase));
if (removedExists && usernameFilenameExists && statsExist)
{
return false;
}
return true;
}
public void Apply()
{
if (!NeedsToBeApplied())
{
Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2025_12_28_AdditionalTransferIndexesMigration));
return;
}
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 missing index(es) on the Transfers table...");
Exec("CREATE INDEX IF NOT EXISTS IDX_Transfers_Removed ON Transfers (Removed)");
Exec("CREATE INDEX IF NOT EXISTS IDX_Transfers_UsernameFilename ON Transfers (Username, Filename)");
Exec("CREATE INDEX IF NOT EXISTS IDX_Transfers_UserUploadStatistics ON Transfers (Username, Direction, EndedAt, StartedAt, State, Size)");
Log.Information("> Index(es) created");
transaction.Commit();
Log.Information("> Done!");
}
catch (Exception)
{
transaction.Rollback();
throw;
}
}
}