aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core/Data/Migrations
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-04-05 16:55:54 -0500
committerJP Dillingham <jp@dillingham.me>2025-04-05 16:55:54 -0500
commit836934203dc314e08b3e351f60bbf70f14a1d1fc (patch)
tree8383f6257ef1fa766753a3b08ab3f7a2cba6c619 /src/slskd/Core/Data/Migrations
parenta21903811d39a978e2c6acc275bba670abee5574 (diff)
downloadslskd-836934203dc314e08b3e351f60bbf70f14a1d1fc.tar.gz
more wip
Diffstat (limited to 'src/slskd/Core/Data/Migrations')
-rw-r--r--src/slskd/Core/Data/Migrations/Migration.cs82
-rw-r--r--src/slskd/Core/Data/Migrations/TEMP_SeedTransfers.cs56
-rw-r--r--src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs15
3 files changed, 62 insertions, 91 deletions
diff --git a/src/slskd/Core/Data/Migrations/Migration.cs b/src/slskd/Core/Data/Migrations/Migration.cs
deleted file mode 100644
index 02f87a9c..00000000
--- a/src/slskd/Core/Data/Migrations/Migration.cs
+++ /dev/null
@@ -1,82 +0,0 @@
-// <copyright file="Migration.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.Collections.Generic;
-using System.IO;
-using Microsoft.Data.Sqlite;
-
-public record ColumnInfo(long Cid, string Name, string Type, bool NotNull, object DefaultValue, bool PrimaryKey);
-
-public abstract class Migration
-{
- public Migration(string databaseName)
- {
- DatabaseName = databaseName;
- }
-
- public string DatabaseName { get; }
- public string DatabaseFileName => Path.Combine(Program.DataDirectory, DatabaseName);
- public string ConnectionString => $"Data Source={DatabaseFileName}.db";
-
- public abstract void Apply();
-
- protected Dictionary<string, IEnumerable<ColumnInfo>> GetDatabaseSchema()
- {
- try
- {
- using var connection = new SqliteConnection(ConnectionString);
- connection.Open();
-
- var dict = new Dictionary<string, IEnumerable<ColumnInfo>>();
-
- using var tableCommand = new SqliteCommand("SELECT name FROM sqlite_master WHERE type='table';", connection);
- using var tableReader = tableCommand.ExecuteReader();
-
- while (tableReader.Read())
- {
- var table = tableReader.GetString(0);
-
- var columns = new List<ColumnInfo>();
-
- using var columnCommand = new SqliteCommand($"PRAGMA table_info({table});", connection);
- using var cr = columnCommand.ExecuteReader();
-
- while (cr.Read())
- {
- columns.Add(new ColumnInfo(
- Cid: cr.GetInt64(cr.GetOrdinal("cid")),
- Name: cr.GetString(cr.GetOrdinal("name")),
- Type: cr.GetString(cr.GetOrdinal("type")),
- NotNull: cr.GetInt64(cr.GetOrdinal("notnull")) > 0,
- DefaultValue: cr["dflt_value"],
- PrimaryKey: cr.GetInt64(cr.GetOrdinal("pk")) > 0));
- }
-
- dict[table] = columns;
- }
-
- return dict;
- }
- catch (Exception ex)
- {
- throw new SlskdException($"Failed to retrieve schema information for database '{DatabaseName}'. The database might be corrupt or in use by another application; if the problem persists the file ({DatabaseFileName}) may need to be deleted", ex);
- }
- }
-} \ No newline at end of file
diff --git a/src/slskd/Core/Data/Migrations/TEMP_SeedTransfers.cs b/src/slskd/Core/Data/Migrations/TEMP_SeedTransfers.cs
new file mode 100644
index 00000000..9f973023
--- /dev/null
+++ b/src/slskd/Core/Data/Migrations/TEMP_SeedTransfers.cs
@@ -0,0 +1,56 @@
+// <copyright file="TransferStateMigration_04012025.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.IO;
+using Microsoft.Data.Sqlite;
+using Serilog;
+
+public class TEMP_SeedTransfers : IMigration
+{
+ private ILogger Log { get; } = Serilog.Log.ForContext<TransferStateMigration_04012025>();
+
+ public void Apply()
+ {
+ using var connection = new SqliteConnection($"Data Source={Path.Combine(Program.DataDirectory, "transfers.db")}");
+ connection.Open();
+
+ Log.Warning("Staring seed");
+
+ for (long i = 0; i < 10_000; i++)
+ {
+ string s(Guid id) => @$"
+ INSERT INTO Transfers (Id, Username, Direction, Filename, Size, StartOffset, State, RequestedAt, EnqueuedAt, StartedAt, EndedAt, BytesTransferred, AverageSpeed, PlaceInQueue, Removed)
+ VALUES ('{id}', 'Username', 'Download', 'Foo.mp3', 42, 0, 'Completed, Succeeded', '1/1/1970 0:0:0', '1/1/1970 0:0:0', '1/1/1970 0:0:0', '1/1/1970 0:0:0', 42, 0, 0, 1);
+ ";
+
+ string sql = "";
+
+ for (int j = 0; j < 5_000; j++)
+ {
+ sql += s(Guid.NewGuid());
+ }
+
+ using var cmd = new SqliteCommand(sql, connection);
+
+ cmd.ExecuteNonQuery();
+ Log.Warning("Completed {i}", i * 5000);
+ }
+ }
+}
diff --git a/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs b/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs
index a3da5435..6d8b4cfc 100644
--- a/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs
+++ b/src/slskd/Core/Data/Migrations/TransferStateMigration_04012025.cs
@@ -38,20 +38,17 @@ using Soulseek;
/// 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 TransferStateMigration_04012025 : Migration
+public class TransferStateMigration_04012025 : IMigration
{
- public TransferStateMigration_04012025(string databasName)
- : base(databasName)
- {
- }
-
private ILogger Log { get; } = Serilog.Log.ForContext<TransferStateMigration_04012025>();
+ private string DatabaseName { get; } = "transfers";
+ private string ConnectionString => $"Data Source={Path.Combine(Program.DataDirectory, $"{DatabaseName}.db")}";
- public override void Apply()
+ public void Apply()
{
// first, check the existing database to see if the StatusDescription column exists
// if it does, the migration has already been applied
- var schema = GetDatabaseSchema();
+ var schema = Migrator.GetDatabaseSchema(ConnectionString);
var txfers = schema["Transfers"];
if (txfers.Any(c => c.Name == "StatusDescription"))
@@ -60,7 +57,7 @@ public class TransferStateMigration_04012025 : Migration
return;
}
- using var connection = new SqliteConnection($"Data Source={Path.Combine(Program.DataDirectory, "transfers.db")}");
+ using var connection = new SqliteConnection(ConnectionString);
connection.Open();
// get a distinct list of states from the existing data and translate each into the corresponding integer