aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-04-04 19:50:31 -0500
committerJP Dillingham <jp@dillingham.me>2025-04-04 19:50:31 -0500
commit6b1e7dbb741b284a788f97fb7d08991e11ef8c50 (patch)
treea0a5c13c8f32968f993557925738ae65504b58bb /src
parent534d8824c4a3f1aae0448a17f734b57ea395c59f (diff)
downloadslskd-6b1e7dbb741b284a788f97fb7d08991e11ef8c50.tar.gz
more wip
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs108
1 files changed, 61 insertions, 47 deletions
diff --git a/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs b/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs
index 37171a77..ae9ada21 100644
--- a/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs
+++ b/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs
@@ -24,6 +24,7 @@ using System.Linq;
using System.Text.Json;
using Microsoft.Data.Sqlite;
using Serilog;
+using Soulseek;
/// <summary>
/// Updates the Transfers table to:
@@ -44,7 +45,6 @@ public class TransferStateMigration_04012025 : Migration
{
}
- private string DbName => "transfers";
private ILogger Log { get; } = Serilog.Log.ForContext<TransferStateMigration_04012025>();
public override void Apply()
@@ -60,35 +60,46 @@ public class TransferStateMigration_04012025 : Migration
return;
}
- AddStateDescriptionColumn(Path.Combine(Program.DataDirectory, "transfers.db"));
+ using var connection = new SqliteConnection($"Data Source={Path.Combine(Program.DataDirectory, "transfers.db")}");
+ connection.Open();
+
+ // get a distinct list of states from the existing data and translate each into the corresponding integer
+ var stateMapping = GetStateMapping(connection);
+
+ AddStateDescriptionColumn(connection, stateMapping);
}
- static void AddStateDescriptionColumn(string dbPath)
+ private Dictionary<string, int> GetStateMapping(SqliteConnection connection)
{
- if (!File.Exists(dbPath))
+ Log.Debug("Fetching states and mapping to integers...");
+
+ using var command = new SqliteCommand("SELECT DISTINCT State FROM Transfers;", connection);
+ using var reader = command.ExecuteReader();
+
+ var dict = new Dictionary<string, int>();
+
+ while (reader.Read())
{
- Console.WriteLine("Database file not found.");
- return;
+ var state = reader.GetString(0);
+ dict[state] = (int)Enum.Parse(typeof(TransferStates), state);
}
- string connectionString = $"Data Source={dbPath};";
+ Log.Debug("State -> int map: {Map}", dict);
+ return dict;
+ }
- using (var connection = new SqliteConnection(connectionString))
+ private void AddStateDescriptionColumn(SqliteConnection connection, Dictionary<string, int> stateMapping)
+ {
+ using (var transaction = connection.BeginTransaction())
{
- connection.Open();
-
- using (var transaction = connection.BeginTransaction())
+ try
{
- try
- {
- // Rename old table
- using (var renameCommand = new SqliteCommand("ALTER TABLE Transfers RENAME TO Transfers_old;", connection))
- {
- renameCommand.ExecuteNonQuery();
- }
-
- // Create new table with StateDescription after State
- string createNewTableSQL = @"
+ // rename old table
+ var renameCommand = new SqliteCommand("ALTER TABLE Transfers RENAME TO Transfers_old;", connection, transaction))
+ renameCommand.ExecuteNonQuery();
+
+ // create new table with StateDescription after State
+ var createCommand = new SqliteCommand(@"
CREATE TABLE Transfers (
Id TEXT NOT NULL,
Username TEXT,
@@ -97,7 +108,7 @@ public class TransferStateMigration_04012025 : Migration
Size INTEGER NOT NULL,
StartOffset INTEGER NOT NULL,
State INTEGER NOT NULL,
- StateDescription TEXT, -- New column
+ StateDescription TEXT, -- new column
RequestedAt TEXT NOT NULL,
EnqueuedAt TEXT,
StartedAt TEXT,
@@ -108,15 +119,11 @@ public class TransferStateMigration_04012025 : Migration
Exception TEXT,
Removed INTEGER NOT NULL,
CONSTRAINT PK_Transfers PRIMARY KEY(Id)
- );";
-
- using (var createCommand = new SqliteCommand(createNewTableSQL, connection))
- {
- createCommand.ExecuteNonQuery();
- }
+ );", connection, transaction);
+ createCommand.ExecuteNonQuery();
- // Copy data into the new table
- string copyDataSQL = @"
+ // copy the old data into the new table
+ var copyCommand = new SqliteCommand(@"
INSERT INTO Transfers (
Id, Username, Direction, Filename, Size, StartOffset, State,
RequestedAt, EnqueuedAt, StartedAt, EndedAt, BytesTransferred,
@@ -126,30 +133,37 @@ public class TransferStateMigration_04012025 : Migration
Id, Username, Direction, Filename, Size, StartOffset, State,
RequestedAt, EnqueuedAt, StartedAt, EndedAt, BytesTransferred,
AverageSpeed, PlaceInQueue, Exception, Removed
- FROM Transfers_old;";
+ FROM Transfers_old;",
+ connection, transaction);
+ copyCommand.ExecuteNonQuery();
- using (var copyCommand = new SqliteCommand(copyDataSQL, connection))
- {
- copyCommand.ExecuteNonQuery();
- }
+ // copy the status from Status to StatusDescription
+ var copyColumnCommand = new SqliteCommand("UPDATE Transfers SET StateDescription = State", connection, transaction);
+ copyColumnCommand.ExecuteNonQuery();
- // Drop the old table
- using (var dropCommand = new SqliteCommand("DROP TABLE Transfers_old;", connection))
- {
- dropCommand.ExecuteNonQuery();
- }
+ // todo: set Status to the integer representation of the column
+ foreach (var state in stateMapping)
+ {
+ Log.Debug("Set {String} to {Int}", state.Key, state.Value);
- transaction.Commit();
- Console.WriteLine("Column 'StateDescription' added successfully.");
+ var mapCommand = new SqliteCommand($"UPDATE Transfers SET State = {state.Value} WHERE State = {state.Key}");
+ mapCommand.ExecuteNonQuery();
}
- catch (Exception ex)
+
+ // Drop the old table
+ using (var dropCommand = new SqliteCommand("DROP TABLE Transfers_old;", connection, transaction))
{
- transaction.Rollback();
- Console.WriteLine($"Error: {ex.Message}");
+ dropCommand.ExecuteNonQuery();
}
- }
- connection.Close();
+ transaction.Commit();
+ Console.WriteLine("Column 'StateDescription' added successfully.");
+ }
+ catch (Exception ex)
+ {
+ transaction.Rollback();
+ Console.WriteLine($"Error: {ex.Message}");
+ }
}
}
}