diff --git a/LANCommander/Program.cs b/LANCommander/Program.cs index 584d8c0..72aa0f1 100644 --- a/LANCommander/Program.cs +++ b/LANCommander/Program.cs @@ -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(); - await db.Database.MigrateAsync(); + using (var db = scope.ServiceProvider.GetService()) + { + 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");