From 9d2050a21996df047e20ca4a5da5da346d560008 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 May 2025 09:10:37 -0500 Subject: final tweaks? --- src/slskd/Core/Data/Migrations/README.md | 4 +++- src/slskd/Core/Data/Migrator.cs | 24 ++++++++---------------- 2 files changed, 11 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Data/Migrations/README.md b/src/slskd/Core/Data/Migrations/README.md index 0cafbe6a..cae058b2 100644 --- a/src/slskd/Core/Data/Migrations/README.md +++ b/src/slskd/Core/Data/Migrations/README.md @@ -13,7 +13,7 @@ The `Migrator` creates a backup of each database prior to running any migrations Each migration must be created as a class that implements the `IMigration` interface. The naming convention for migration classes is: ``` -z_Migration +Z_Migration ``` Migrations MUST: @@ -24,6 +24,8 @@ and the name(s) of the table(s) that need to be migrated. * Use transactions when performing database operations and using/try catch where appropriate to avoid partially applied migrations. * Log progress, so users can see that the application is performing work while records are updated. +Review the `IMigration` interface to determine implementation details. + The new `IMigration` implementation must be added to the dictionary in the `Migrations` property of the `Migrator` in the desired order so that it can be run. ### Above all else, each and every migration **MUST** be idempotent. diff --git a/src/slskd/Core/Data/Migrator.cs b/src/slskd/Core/Data/Migrator.cs index 9f28d747..02b65c0c 100644 --- a/src/slskd/Core/Data/Migrator.cs +++ b/src/slskd/Core/Data/Migrator.cs @@ -33,6 +33,9 @@ public interface IMigration /// /// Determines whether the migration needs to be applied. /// + /// + /// This method MUST be read-only and should contain no side effects. + /// /// A value indicating whether the migration needs to be applied. bool NeedsToBeApplied(); @@ -56,13 +59,6 @@ public class Migrator Databases = databases; } - private enum MigrationDisposition - { - AlreadyAppliedPerHistoryFile, - ApplicationNotNeededPerMigration, - ApplicationRequired, - } - private string HistoryFileName { get; } = Path.Combine(Program.DataMigrationsDirectory, "history"); private IEnumerable Databases { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); @@ -120,15 +116,9 @@ public class Migrator /* determine which migrations need to be applied - first, take the set difference (Except()) of the list of registered migrations and the history file. this gives - us a list of t he migrations that are not in the history file. - - next, check each of the migrations not in history to see if they need to be applied. if they _don't_, then - add them directly to the history dictionary. if they do, add them to the list of migrations to apply. - - the result is: - - an updated history dictionary, containing the dict loaded from disk plus any new migrations that _don't_ need to be applied - - a list of migrations that need to be applied + take the set difference between all registered migrations and the history file, then for each of the migrations + in that set, check to see if it needs to be applied. if it doesn't, add it to the history file with the current + timestamp. if it does, add it to the list of migrations to apply. */ List migrationsToApply = []; @@ -158,6 +148,8 @@ public class Migrator if (migrationsToApply.Count == 0) { + // write the history file to disk; if there are new migrations that didn't need to be applied, or the user + // used the 'force' flag, the history file on disk and in memory will have diverged. UpdateHistoryFile(history); Log.Information("Databases are up to date!"); -- cgit v1.2.3