From 8a27eca8b38f02b09b90589b6f4250839f0f543b Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Thu, 23 Feb 2023 00:09:49 +0100 Subject: [PATCH] DI: Just use file.copy for backups --- Pal.Client/DependencyContextInitializer.cs | 28 +++++++++++++++------- Pal.Client/DependencyInjectionContext.cs | 3 ++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Pal.Client/DependencyContextInitializer.cs b/Pal.Client/DependencyContextInitializer.cs index 4f29b38..0723736 100644 --- a/Pal.Client/DependencyContextInitializer.cs +++ b/Pal.Client/DependencyContextInitializer.cs @@ -30,7 +30,8 @@ namespace Pal.Client private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; - public DependencyContextInitializer(ILogger logger, IServiceProvider serviceProvider) + public DependencyContextInitializer(ILogger logger, + IServiceProvider serviceProvider) { _logger = logger; _serviceProvider = serviceProvider; @@ -135,16 +136,25 @@ namespace Pal.Client var pluginInterface = scope.ServiceProvider.GetRequiredService(); string backupPath = Path.Join(pluginInterface.GetPluginConfigDirectory(), $"backup-{DateTime.Today.ToUniversalTime():yyyy-MM-dd}.data.sqlite3"); - if (!File.Exists(backupPath)) + string sourcePath = Path.Join(pluginInterface.GetPluginConfigDirectory(), + DependencyInjectionContext.DatabaseFileName); + if (File.Exists(sourcePath) && !File.Exists(backupPath)) { - _logger.LogInformation("Creating database backup '{Path}'", backupPath); + if (File.Exists(sourcePath + "-shm") || File.Exists(sourcePath + "-wal")) + { + _logger.LogWarning("Could not create backup, database is open in another program"); + return; + } - await using var db = scope.ServiceProvider.GetRequiredService(); - await using SqliteConnection source = new(db.Database.GetConnectionString()); - await source.OpenAsync(); - await using SqliteConnection backup = new($"Data Source={backupPath}"); - source.BackupDatabase(backup); - SqliteConnection.ClearPool(backup); + _logger.LogInformation("Creating database backup '{Path}'", backupPath); + try + { + File.Copy(sourcePath, backupPath); + } + catch (Exception e) + { + _logger.LogError(e, "Could not create backup"); + } } else _logger.LogInformation("Database backup in '{Path}' already exists", backupPath); diff --git a/Pal.Client/DependencyInjectionContext.cs b/Pal.Client/DependencyInjectionContext.cs index 941db6c..3a75509 100644 --- a/Pal.Client/DependencyInjectionContext.cs +++ b/Pal.Client/DependencyInjectionContext.cs @@ -33,6 +33,7 @@ namespace Pal.Client /// internal sealed class DependencyInjectionContext : IDisposable { + public const string DatabaseFileName = "palace-pal.data.sqlite3"; public static DalamudLoggerProvider LoggerProvider { get; } = new(); /// @@ -89,7 +90,7 @@ namespace Pal.Client _serviceCollection.AddSingleton(new WindowSystem(typeof(DependencyInjectionContext).AssemblyQualifiedName)); _sqliteConnectionString = - $"Data Source={Path.Join(pluginInterface.GetPluginConfigDirectory(), "palace-pal.data.sqlite3")}"; + $"Data Source={Path.Join(pluginInterface.GetPluginConfigDirectory(), DatabaseFileName)}"; } public IServiceProvider BuildServiceContainer()