Merge pull request #300228 from davidkna/kavita-fix-migrations

kavita: restore db migrations

authored by Pol Dellaiera and committed by GitHub 831dced7 a4ab96ff

+153
+6
pkgs/servers/web-apps/kavita/default.nix
··· 34 }) 35 # The webroot is hardcoded as ./wwwroot 36 ./change-webroot.diff 37 ]; 38 postPatch = '' 39 substituteInPlace API/Services/DirectoryService.cs --subst-var out
··· 34 }) 35 # The webroot is hardcoded as ./wwwroot 36 ./change-webroot.diff 37 + # Upstream removes database migrations between versions 38 + # Restore them to avoid breaking on updates 39 + # Info: Restores migrations for versions between v0.7.1.4 and v0.7.9 40 + # On update: check if more migrations need to be restored! 41 + # Migrations should at least allow updates from previous NixOS versions 42 + ./restore-migrations.diff 43 ]; 44 postPatch = '' 45 substituteInPlace API/Services/DirectoryService.cs --subst-var out
+147
pkgs/servers/web-apps/kavita/restore-migrations.diff
···
··· 1 + diff --git a/API/Data/ManualMigrations/MigrateDisableScrobblingOnComicLibraries.cs b/API/Data/ManualMigrations/MigrateDisableScrobblingOnComicLibraries.cs 2 + new file mode 100644 3 + index 00000000..0de7bf5d 4 + --- /dev/null 5 + +++ b/API/Data/ManualMigrations/MigrateDisableScrobblingOnComicLibraries.cs 6 + @@ -0,0 +1,38 @@ 7 + +using System.Linq; 8 + +using System.Threading.Tasks; 9 + +using API.Entities.Enums; 10 + +using Microsoft.EntityFrameworkCore; 11 + +using Microsoft.Extensions.Logging; 12 + + 13 + +namespace API.Data.ManualMigrations; 14 + + 15 + +/// <summary> 16 + +/// v0.7.4 introduced Scrobbling with Kavita+. By default, it is on, but Comic libraries have no scrobble providers, so disable 17 + +/// </summary> 18 + +public static class MigrateDisableScrobblingOnComicLibraries 19 + +{ 20 + + public static async Task Migrate(IUnitOfWork unitOfWork, DataContext dataContext, ILogger<Program> logger) 21 + + { 22 + + if (!await dataContext.Library.Where(s => s.Type == LibraryType.Comic).Where(l => l.AllowScrobbling).AnyAsync()) 23 + + { 24 + + return; 25 + + } 26 + + logger.LogInformation("Running MigrateDisableScrobblingOnComicLibraries migration. Please be patient, this may take some time"); 27 + + 28 + + 29 + + foreach (var lib in await dataContext.Library.Where(s => s.Type == LibraryType.Comic).Where(l => l.AllowScrobbling).ToListAsync()) 30 + + { 31 + + lib.AllowScrobbling = false; 32 + + unitOfWork.LibraryRepository.Update(lib); 33 + + } 34 + + 35 + + if (unitOfWork.HasChanges()) 36 + + { 37 + + await unitOfWork.CommitAsync(); 38 + + } 39 + + 40 + + logger.LogInformation("MigrateDisableScrobblingOnComicLibraries migration finished"); 41 + + 42 + + } 43 + + 44 + +} 45 + diff --git a/API/Data/ManualMigrations/MigrateLoginRoles.cs b/API/Data/ManualMigrations/MigrateLoginRoles.cs 46 + new file mode 100644 47 + index 00000000..f649908a 48 + --- /dev/null 49 + +++ b/API/Data/ManualMigrations/MigrateLoginRoles.cs 50 + @@ -0,0 +1,36 @@ 51 + +using System.Threading.Tasks; 52 + +using API.Constants; 53 + +using API.Entities; 54 + +using Microsoft.AspNetCore.Identity; 55 + +using Microsoft.Extensions.Logging; 56 + + 57 + +namespace API.Data.ManualMigrations; 58 + + 59 + +/// <summary> 60 + +/// Added in v0.7.1.18 61 + +/// </summary> 62 + +public static class MigrateLoginRoles 63 + +{ 64 + + /// <summary> 65 + + /// Will not run if any users have the <see cref="PolicyConstants.LoginRole"/> role already 66 + + /// </summary> 67 + + /// <param name="unitOfWork"></param> 68 + + /// <param name="userManager"></param> 69 + + /// <param name="logger"></param> 70 + + public static async Task Migrate(IUnitOfWork unitOfWork, UserManager<AppUser> userManager, ILogger<Program> logger) 71 + + { 72 + + var usersWithRole = await userManager.GetUsersInRoleAsync(PolicyConstants.LoginRole); 73 + + if (usersWithRole.Count != 0) return; 74 + + 75 + + logger.LogCritical("Running MigrateLoginRoles migration"); 76 + + 77 + + var allUsers = await unitOfWork.UserRepository.GetAllUsersAsync(); 78 + + foreach (var user in allUsers) 79 + + { 80 + + await userManager.RemoveFromRoleAsync(user, PolicyConstants.LoginRole); 81 + + await userManager.AddToRoleAsync(user, PolicyConstants.LoginRole); 82 + + } 83 + + 84 + + logger.LogInformation("MigrateLoginRoles migration complete"); 85 + + } 86 + +} 87 + diff --git a/API/Data/ManualMigrations/MigrateRemoveWebPSettingRows.cs b/API/Data/ManualMigrations/MigrateRemoveWebPSettingRows.cs 88 + new file mode 100644 89 + index 00000000..07e98ef6 90 + --- /dev/null 91 + +++ b/API/Data/ManualMigrations/MigrateRemoveWebPSettingRows.cs 92 + @@ -0,0 +1,31 @@ 93 + +using System.Threading.Tasks; 94 + +using API.Entities.Enums; 95 + +using Microsoft.Extensions.Logging; 96 + + 97 + +namespace API.Data.ManualMigrations; 98 + + 99 + +/// <summary> 100 + +/// Added in v0.7.2.7/v0.7.3 in which the ConvertXToWebP Setting keys were removed. This migration will remove them. 101 + +/// </summary> 102 + +public static class MigrateRemoveWebPSettingRows 103 + +{ 104 + + public static async Task Migrate(IUnitOfWork unitOfWork, ILogger<Program> logger) 105 + + { 106 + + logger.LogCritical("Running MigrateRemoveWebPSettingRows migration - Please be patient, this may take some time. This is not an error"); 107 + + 108 + + var key = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.ConvertBookmarkToWebP); 109 + + var key2 = await unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.ConvertCoverToWebP); 110 + + if (key == null && key2 == null) 111 + + { 112 + + logger.LogCritical("Running MigrateRemoveWebPSettingRows migration - complete. Nothing to do"); 113 + + return; 114 + + } 115 + + 116 + + unitOfWork.SettingsRepository.Remove(key); 117 + + unitOfWork.SettingsRepository.Remove(key2); 118 + + 119 + + await unitOfWork.CommitAsync(); 120 + + 121 + + logger.LogCritical("Running MigrateRemoveWebPSettingRows migration - Completed. This is not an error"); 122 + + } 123 + +} 124 + diff --git a/API/Startup.cs b/API/Startup.cs 125 + index 21c4fa45..04f4a077 100644 126 + --- a/API/Startup.cs 127 + +++ b/API/Startup.cs 128 + @@ -232,11 +232,19 @@ public class Startup 129 + Task.Run(async () => 130 + { 131 + // Apply all migrations on startup 132 + + var userManager = serviceProvider.GetRequiredService<UserManager<AppUser>>(); 133 + var dataContext = serviceProvider.GetRequiredService<DataContext>(); 134 + 135 + 136 + logger.LogInformation("Running Migrations"); 137 + 138 + + // v0.7.2 139 + + await MigrateLoginRoles.Migrate(unitOfWork, userManager, logger); 140 + + // v0.7.3 141 + + await MigrateRemoveWebPSettingRows.Migrate(unitOfWork, logger); 142 + + // v0.7.4 143 + + await MigrateDisableScrobblingOnComicLibraries.Migrate(unitOfWork, dataContext, logger); 144 + + 145 + // v0.7.9 146 + await MigrateUserLibrarySideNavStream.Migrate(unitOfWork, dataContext, logger); 147 +