Migration Safety (#967)

* Updated EF version

* When we perform a migration, backup the database to temp/migration/VERSION and do it only once in case a migration fails.

* When a migration fails, we will now restore what was corrupted.
This commit is contained in:
Joseph Milazzo 2022-01-19 15:03:47 -08:00 committed by GitHub
parent d54e43edb1
commit 6c2731071d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 5 deletions

View File

@ -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" />

View File

@ -62,7 +62,15 @@ namespace API
if (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 API
catch (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) =>