aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Core/Data/Migrations/Z2025_04_01_TransferStateMigration.cs
blob: 1a155f180c1e6dc701dea64b4206aa0f1c939b18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// <copyright file="Z2025_04_01_TransferStateMigration.cs" company="JP Dillingham">
//           ▄▄▄▄     ▄▄▄▄     ▄▄▄▄
//     ▄▄▄▄▄▄█  █▄▄▄▄▄█  █▄▄▄▄▄█  █
//     █__ --█  █__ --█    ◄█  -  █
//     █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
//   ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━  ━┉   ┉     ┉
//   │ 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
//   ╰───────────────────────────────────────────╶──── ─ ─── ─  ── ──┈  ┈
// </copyright>

namespace slskd.Migrations;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Data.Sqlite;
using Serilog;
using slskd.Transfers;

/// <summary>
///     Updates the Transfers table to:
///
///     * Add a new StateDescription (TEXT) column
///     * Copy the current (string) contents of the State column to StateDescription
///     * Change the type of the State column to INTEGER
///     * Set the contents of the State column to the numeric representation of the existing State
///
///     This is necessary because Entity Framework doesn't work with [Flags] enums that are stored
///     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 Z2025_04_01_TransferStateMigration : IMigration
{
    public Z2025_04_01_TransferStateMigration(ConnectionStringDictionary connectionStrings)
    {
        ConnectionString = connectionStrings[Database.Transfers];
    }

    private ILogger Log { get; } = Serilog.Log.ForContext<Z2025_04_01_TransferStateMigration>();
    private string ConnectionString { get; }

    public bool NeedsToBeApplied()
    {
        // first, check the existing database to see if the StateDescription column exists
        // if it does, the migration has already been applied, or the database has been recreated
        var schema = SchemaInspector.GetDatabaseSchema(ConnectionString);
        var txfers = schema["Transfers"];

#pragma warning disable CS0618 // Type or member is obsolete
        return !txfers.Any(c => c.Name == nameof(Transfer.StateDescription));
#pragma warning restore CS0618 // Type or member is obsolete

    }

    public void Apply()
    {
        if (!NeedsToBeApplied())
        {
            Log.Information("> Migration {Name} is not necessary or has already been applied", nameof(Z2025_04_01_TransferStateMigration));
            return;
        }

        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
        var stateMapping = GetStateMapping(connection);

        ModifyTableAndMassageData(connection, stateMapping);
    }

    private Dictionary<string, int> GetStateMapping(SqliteConnection connection)
    {
        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())
        {
            var state = reader.GetString(0);
            dict[state] = (int)Enum.Parse(typeof(Z04012025_TransferStateMigration_TransferStates), state);
        }

        Log.Debug("State -> int map: {Map}", dict);
        return dict;
    }

    private void ModifyTableAndMassageData(SqliteConnection connection, Dictionary<string, int> stateMapping)
    {
        using var transaction = connection.BeginTransaction();

        try
        {
            /*
                rename old table so we can create the new one with the layout we want
                while preserving the existing data
            */
            var rename = @"
                ALTER TABLE Transfers RENAME TO Transfers_old;
            ";

            using var renameCommand = new SqliteCommand(rename, connection, transaction);
            renameCommand.ExecuteNonQuery();

            /*
                create the new table with StateDescription appearing after State
            */
            var create = @"
                CREATE TABLE Transfers (
                    Id TEXT NOT NULL,
                    Username TEXT,
                    Direction TEXT NOT NULL,
                    Filename TEXT,
                    Size INTEGER NOT NULL,
                    StartOffset INTEGER NOT NULL,
                    State INTEGER NOT NULL,
                    StateDescription TEXT, -- new column
                    RequestedAt TEXT NOT NULL,
                    EnqueuedAt TEXT,
                    StartedAt TEXT,
                    EndedAt TEXT,
                    BytesTransferred INTEGER NOT NULL,
                    AverageSpeed REAL NOT NULL,
                    PlaceInQueue INTEGER,
                    Exception TEXT,
                    Removed INTEGER NOT NULL,
                    CONSTRAINT PK_Transfers PRIMARY KEY(Id)
                );
            ";

            using var createCommand = new SqliteCommand(create, connection, transaction);
            createCommand.ExecuteNonQuery();

            Log.Information("> Schema changes applied, copying data...");

            /*
                copy the existing data into the new table
            */
            var copy = @"
                INSERT INTO Transfers (
                    Id, Username, Direction, Filename, Size, StartOffset, State, 
                    RequestedAt, EnqueuedAt, StartedAt, EndedAt, BytesTransferred, 
                    AverageSpeed, PlaceInQueue, Exception, Removed
                )
                SELECT 
                    Id, Username, Direction, Filename, Size, StartOffset, State, 
                    RequestedAt, EnqueuedAt, StartedAt, EndedAt, BytesTransferred, 
                    AverageSpeed, PlaceInQueue, Exception, Removed
                FROM Transfers_old;";

            using var copyCommand = new SqliteCommand(copy, connection, transaction);
            copyCommand.ExecuteNonQuery();

            Log.Information("> Data copied, adjusting columns...");

            /*
                we no longer need the old table, so drop it
            */
            using var dropCommand = new SqliteCommand("DROP TABLE Transfers_old;", connection, transaction);
            dropCommand.ExecuteNonQuery();

            /*
                copy the existing (string) State value from State to StateDescription
            */
            var copyColumnCommand = new SqliteCommand("UPDATE Transfers SET StateDescription = State", connection, transaction);
            copyColumnCommand.ExecuteNonQuery();

            /*
                for each of the distinct State values that exist in the data, convert the string
                representation into the numeric

                there surely exist better ways to do this, but the reality is that 99% of the records
                will be "Completed, Succeeded" (48), with a few "Completed, Errored" or "Completed, Cancelled"
                sprinkled in. this should run in reasonable time for large collections
            */
            var sw = new Stopwatch();
            var step = 1;

            foreach (var state in stateMapping)
            {
                sw.Reset();
                Log.Information("> Updating column {Step} of {Total}. This may take some time...", step++, stateMapping.Count);

                Log.Debug("Setting {String} to {Int}", state.Key, state.Value);

                var mapCommand = new SqliteCommand($"UPDATE Transfers SET State = {state.Value} WHERE State = '{state.Key}';", connection, transaction);
                mapCommand.ExecuteNonQuery();

                Log.Debug("{String} to {Int} in {Duration}", state.Key, state.Value, sw.ElapsedMilliseconds);
            }

            Log.Debug("Committing transation...");
            transaction.Commit();
            Log.Debug("Transaction committed!");
            Log.Information("> Done!");
        }
        catch (Exception)
        {
            transaction.Rollback();
            throw;
        }
    }

#pragma warning disable SA1201 // Elements should appear in the correct order
    /// <summary>
    ///    This is a copy of the TransferStates enum as it was at the time this migration was written. This exists to ensure
    ///    that the migration can be applied to any version of the database, even if the enum changes in the future.
    /// </summary>
    [Flags]
    private enum Z04012025_TransferStateMigration_TransferStates
    {
        None = 0,
        Requested = 1,
        Queued = 2,
        Initializing = 4,
        InProgress = 8,
        Completed = 16,
        Succeeded = 32,
        Cancelled = 64,
        TimedOut = 128,
        Errored = 256,
        Rejected = 512,
        Aborted = 1024,
        Locally = 2048,
        Remotely = 4096,
    }
#pragma warning restore SA1201 // Elements should appear in the correct order
}