Backup database before migration
parent
39dd24e0ba
commit
6b005eb384
|
@ -11,6 +11,7 @@ using System.Text;
|
||||||
using Hangfire;
|
using Hangfire;
|
||||||
using NLog;
|
using NLog;
|
||||||
using LANCommander.Services.MediaGrabbers;
|
using LANCommander.Services.MediaGrabbers;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
namespace LANCommander
|
namespace LANCommander
|
||||||
{
|
{
|
||||||
|
@ -221,11 +222,24 @@ namespace LANCommander
|
||||||
if (!Directory.Exists("Snippets"))
|
if (!Directory.Exists("Snippets"))
|
||||||
Directory.CreateDirectory("Snippets");
|
Directory.CreateDirectory("Snippets");
|
||||||
|
|
||||||
|
if (!Directory.Exists("Backups"))
|
||||||
|
Directory.CreateDirectory("Backups");
|
||||||
|
|
||||||
// Migrate
|
// Migrate
|
||||||
Logger.Debug("Migrating database if required");
|
Logger.Debug("Migrating database if required");
|
||||||
await using var scope = app.Services.CreateAsyncScope();
|
await using var scope = app.Services.CreateAsyncScope();
|
||||||
using var db = scope.ServiceProvider.GetService<DatabaseContext>();
|
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();
|
await db.Database.MigrateAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Autostart any server processes
|
// Autostart any server processes
|
||||||
Logger.Debug("Autostarting Servers");
|
Logger.Debug("Autostarting Servers");
|
||||||
|
|
Loading…
Reference in New Issue