Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Migration Safety (#967)
Joseph Milazzo6c2731071dc9
diff --git a/API/API.csproj b/API/API.csprojindex fbf15067e..759f1ae86 100644--- a/API/API.csproj+++ b/API/API.csproj@@ -49,13 +49,13 @@<PackageReference Include="MarkdownDeep.NET.Core" Version="1.5.0.4" /><PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" /><PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.1" />- <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />+ <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.1" /><PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />- <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">+ <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1"><PrivateAssets>all</PrivateAssets><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets></PackageReference>- <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />+ <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" /><PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /><PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.2.0" /><PackageReference Include="NetVips" Version="2.1.0" />diff --git a/API/Program.cs b/API/Program.csindex 4ed8ce56a..286a0f0cd 100644--- a/API/Program.cs+++ b/API/Program.cs@@ -62,7 +62,15 @@ namespace APIif (pendingMigrations.Any()){logger.LogInformation("Performing backup as migrations are needed. Backup will be kavita.db in temp folder");- directoryService.CopyFileToDirectory(directoryService.FileSystem.Path.Join(directoryService.ConfigDirectory, "kavita.db"), directoryService.TempDirectory);+ var migrationDirectory = await GetMigrationDirectory(context, directoryService);+ directoryService.ExistOrCreate(migrationDirectory);++ if (!directoryService.FileSystem.File.Exists(+ directoryService.FileSystem.Path.Join(migrationDirectory, "kavita.db")))+ {+ directoryService.CopyFileToDirectory(directoryService.FileSystem.Path.Join(directoryService.ConfigDirectory, "kavita.db"), migrationDirectory);+ logger.LogInformation("Database backed up to {MigrationDirectory}", migrationDirectory);+ }}await context.Database.MigrateAsync();@@ -82,12 +90,42 @@ namespace APIcatch (Exception ex){var logger = services.GetRequiredService<ILogger<Program>>();- logger.LogCritical(ex, "An error occurred during migration");+ var context = services.GetRequiredService<DataContext>();+ var migrationDirectory = await GetMigrationDirectory(context, directoryService);++ logger.LogCritical(ex, "A migration failed during startup. Restoring backup from {MigrationDirectory} and exiting", migrationDirectory);+ directoryService.CopyFileToDirectory(directoryService.FileSystem.Path.Join(migrationDirectory, "kavita.db"), directoryService.ConfigDirectory);++ return;}await host.RunAsync();}+ private static async Task<string> GetMigrationDirectory(DataContext context, IDirectoryService directoryService)+ {+ string currentVersion = null;+ try+ {+ currentVersion =+ (await context.ServerSetting.SingleOrDefaultAsync(s =>+ s.Key == ServerSettingKey.InstallVersion))?.Value;+ }+ catch+ {+ // ignored+ }++ if (string.IsNullOrEmpty(currentVersion))+ {+ currentVersion = "vUnknown";+ }++ var migrationDirectory = directoryService.FileSystem.Path.Join(directoryService.TempDirectory,+ "migration", currentVersion);+ return migrationDirectory;+ }+private static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostingContext, config) =>