diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-05-11 09:10:37 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-05-11 09:14:49 -0500 |
| commit | 9d2050a21996df047e20ca4a5da5da346d560008 (patch) | |
| tree | 1e50ee96796d4900159e2442517a0aa202c61bb4 /src/slskd/Core/Data | |
| parent | 3fed167896a1376bedf9f5997887674bc1b1e2ea (diff) | |
| download | slskd-9d2050a21996df047e20ca4a5da5da346d560008.tar.gz | |
final tweaks?
Diffstat (limited to 'src/slskd/Core/Data')
| -rw-r--r-- | src/slskd/Core/Data/Migrations/README.md | 4 | ||||
| -rw-r--r-- | src/slskd/Core/Data/Migrator.cs | 24 |
2 files changed, 11 insertions, 17 deletions
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<MMDDYYYY>_<ShortDescription>Migration +Z<MMDDYYYY>_<ShortDescription>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 /// <summary> /// Determines whether the migration needs to be applied. /// </summary> + /// <remarks> + /// This method MUST be read-only and should contain no side effects. + /// </remarks> /// <returns>A value indicating whether the migration needs to be applied.</returns> 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<string> Databases { get; } private ILogger Log { get; } = Serilog.Log.ForContext<Migrator>(); @@ -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<string> 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!"); |
