Migration Exception handling

This commit is contained in:
Michael Bucari-Tovo 2022-06-12 16:35:48 -06:00
parent fa195483d6
commit b7b49a60cf

View File

@ -405,58 +405,71 @@ namespace AppScaffolding
public static void migrate_from_7_10_1(Configuration config) public static void migrate_from_7_10_1(Configuration config)
{ {
//https://github.com/rmcrackan/Libation/issues/270#issuecomment-1152863629 var lastNigrationThres = config.GetNonString<bool>($"{nameof(migrate_from_7_10_1)}_ThrewError");
//This migration helps fix databases contaminated with the 7.10.1 hack workaround
//and those with improperly identified or missing series. This does not solve cases
//where individual episodes are in the db with a valid series link, but said series'
//parents have not been imported into the database. For those cases, Libation will
//attempt fixup by retrieving parents from the catalog endpoint
using var context = DbContexts.GetContext(); if (lastNigrationThres) return;
//This migration removes books and series with SERIES_ prefix that were created try
//as a hack workaround in 7.10.1. Said workaround was removed in 7.10.2 {
string removeHackSeries = "delete " +
"from series " +
"where AudibleSeriesId like 'SERIES%'";
string removeHackBooks = "delete " + //https://github.com/rmcrackan/Libation/issues/270#issuecomment-1152863629
"from books " + //This migration helps fix databases contaminated with the 7.10.1 hack workaround
"where AudibleProductId like 'SERIES%'"; //and those with improperly identified or missing series. This does not solve cases
//where individual episodes are in the db with a valid series link, but said series'
//parents have not been imported into the database. For those cases, Libation will
//attempt fixup by retrieving parents from the catalog endpoint
//Detect series parents that were added to the database as books with ContentType.Episode, using var context = DbContexts.GetContext();
//and change them to ContentType.Parent
string updateContentType =
"UPDATE books " +
"SET contenttype = 4 " +
"WHERE audibleproductid IN (SELECT books.audibleproductid " +
"FROM books " +
"INNER JOIN series " +
"ON ( books.audibleproductid = " +
"series.audibleseriesid) " +
"WHERE books.contenttype = 2)";
//Then detect series parents that were added to the database as books with ContentType.Parent //This migration removes books and series with SERIES_ prefix that were created
//but are missing a series link, and add the link (don't know how this happened) //as a hack workaround in 7.10.1. Said workaround was removed in 7.10.2
string addMissingSeriesLink = string removeHackSeries = "delete " +
"INSERT INTO seriesbook " + "from series " +
"SELECT series.seriesid, " + "where AudibleSeriesId like 'SERIES%'";
"books.bookid, " +
"'- 1' " +
"FROM books " +
"LEFT OUTER JOIN seriesbook " +
"ON books.bookid = seriesbook.bookid " +
"INNER JOIN series " +
"ON books.audibleproductid = series.audibleseriesid " +
"WHERE books.contenttype = 4 " +
"AND seriesbook.seriesid IS NULL";
context.Database.ExecuteSqlRaw(removeHackSeries); string removeHackBooks = "delete " +
context.Database.ExecuteSqlRaw(removeHackBooks); "from books " +
context.Database.ExecuteSqlRaw(updateContentType); "where AudibleProductId like 'SERIES%'";
context.Database.ExecuteSqlRaw(addMissingSeriesLink);
LibraryCommands.SaveContext(context); //Detect series parents that were added to the database as books with ContentType.Episode,
//and change them to ContentType.Parent
string updateContentType =
"UPDATE books " +
"SET contenttype = 4 " +
"WHERE audibleproductid IN (SELECT books.audibleproductid " +
"FROM books " +
"INNER JOIN series " +
"ON ( books.audibleproductid = " +
"series.audibleseriesid) " +
"WHERE books.contenttype = 2)";
//Then detect series parents that were added to the database as books with ContentType.Parent
//but are missing a series link, and add the link (don't know how this happened)
string addMissingSeriesLink =
"INSERT INTO seriesbook " +
"SELECT series.seriesid, " +
"books.bookid, " +
"'- 1' " +
"FROM books " +
"LEFT OUTER JOIN seriesbook " +
"ON books.bookid = seriesbook.bookid " +
"INNER JOIN series " +
"ON books.audibleproductid = series.audibleseriesid " +
"WHERE books.contenttype = 4 " +
"AND seriesbook.seriesid IS NULL";
context.Database.ExecuteSqlRaw(removeHackSeries);
context.Database.ExecuteSqlRaw(removeHackBooks);
context.Database.ExecuteSqlRaw(updateContentType);
context.Database.ExecuteSqlRaw(addMissingSeriesLink);
LibraryCommands.SaveContext(context);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "An error occured while running database migrations in {0}", nameof(migrate_from_7_10_1));
config.SetObject($"{nameof(migrate_from_7_10_1)}_ThrewError", true);
}
} }
} }
} }