From 30e6deeeaaf0a8dae444a50d5272fdb0d7dac89a Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Tue, 7 Jun 2022 15:25:52 -0600 Subject: [PATCH] Add migration to cleans DB of 7.10.1 hack --- Source/AppScaffolding/LibationScaffolding.cs | 24 +++++++++++++++++++ Source/ApplicationServices/LibraryCommands.cs | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Source/AppScaffolding/LibationScaffolding.cs b/Source/AppScaffolding/LibationScaffolding.cs index 20af1da9..e16f52f0 100644 --- a/Source/AppScaffolding/LibationScaffolding.cs +++ b/Source/AppScaffolding/LibationScaffolding.cs @@ -59,6 +59,7 @@ namespace AppScaffolding // Migrations.migrate_to_v6_6_9(config); + Migrations.migrate_from_7_10_1(config); } public static void PopulateMissingConfigValues(Configuration config) @@ -401,5 +402,28 @@ namespace AppScaffolding UNSAFE_MigrationHelper.Settings_AddUniqueToArray("Serilog.Enrich", "WithExceptionDetails"); } } + + public static void migrate_from_7_10_1(Configuration config) + { + //This migration removes books and series with SERIES_ prefix that were created + //as a hack workaround in 7.10.1. Said workaround was removed in 7.10.2 + + var migrated = config.GetNonString(nameof(migrate_from_7_10_1)); + + if (migrated) return; + + using var context = DbContexts.GetContext(); + + var booksToRemove = context.Books.Where(b => b.AudibleProductId.StartsWith("SERIES_")).ToArray(); + var seriesToRemove = context.Series.Where(s => s.AudibleSeriesId.StartsWith("SERIES_")).ToArray(); + var lbToRemove = context.LibraryBooks.Where(lb => booksToRemove.Any(b => b == lb.Book)).ToArray(); + + context.LibraryBooks.RemoveRange(lbToRemove); + context.Books.RemoveRange(booksToRemove); + context.Series.RemoveRange(seriesToRemove); + + LibraryCommands.SaveContext(context); + config.SetObject(nameof(migrate_from_7_10_1), true); + } } } diff --git a/Source/ApplicationServices/LibraryCommands.cs b/Source/ApplicationServices/LibraryCommands.cs index 745e8a8f..66ee71e1 100644 --- a/Source/ApplicationServices/LibraryCommands.cs +++ b/Source/ApplicationServices/LibraryCommands.cs @@ -200,7 +200,7 @@ namespace ApplicationServices var libraryBookImporter = new LibraryBookImporter(context); var newCount = await Task.Run(() => libraryBookImporter.Import(importItems)); logTime("importIntoDbAsync -- post Import()"); - int qtyChanges = saveChanges(context); + int qtyChanges = SaveContext(context); logTime("importIntoDbAsync -- post SaveChanges"); // this is any changes at all to the database, not just new books @@ -211,7 +211,7 @@ namespace ApplicationServices return newCount; } - private static int saveChanges(LibationContext context) + public static int SaveContext(LibationContext context) { try {