Backup database before migration
parent
39dd24e0ba
commit
6b005eb384
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue