Backup database before migration

net8.0
Pat Hartl 2023-11-17 12:32:09 -06:00
parent 39dd24e0ba
commit 6b005eb384
1 changed files with 16 additions and 2 deletions

View File

@ -11,6 +11,7 @@ using System.Text;
using Hangfire;
using NLog;
using LANCommander.Services.MediaGrabbers;
using Microsoft.Data.Sqlite;
namespace LANCommander
{
@ -221,11 +222,24 @@ namespace LANCommander
if (!Directory.Exists("Snippets"))
Directory.CreateDirectory("Snippets");
if (!Directory.Exists("Backups"))
Directory.CreateDirectory("Backups");
// Migrate
Logger.Debug("Migrating database if required");
await using var scope = app.Services.CreateAsyncScope();
using var db = scope.ServiceProvider.GetService<DatabaseContext>();
await db.Database.MigrateAsync();
using (var db = scope.ServiceProvider.GetService<DatabaseContext>())
{
if ((await db.Database.GetPendingMigrationsAsync()).Any())
{
var dataSource = new SqliteConnectionStringBuilder(settings.DatabaseConnectionString).DataSource;
if (File.Exists(dataSource))
File.Copy(dataSource, Path.Combine("Backups", $"LANCommander.db.{DateTime.Now.ToString("dd-MM-yyyy-HH.mm.ss.bak")}"));
await db.Database.MigrateAsync();
}
}
// Autostart any server processes
Logger.Debug("Autostarting Servers");