DI: Just use file.copy for backups
This commit is contained in:
parent
0d8f655936
commit
8a27eca8b3
@ -30,7 +30,8 @@ namespace Pal.Client
|
|||||||
private readonly ILogger<DependencyContextInitializer> _logger;
|
private readonly ILogger<DependencyContextInitializer> _logger;
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
public DependencyContextInitializer(ILogger<DependencyContextInitializer> logger, IServiceProvider serviceProvider)
|
public DependencyContextInitializer(ILogger<DependencyContextInitializer> logger,
|
||||||
|
IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_serviceProvider = serviceProvider;
|
_serviceProvider = serviceProvider;
|
||||||
@ -135,16 +136,25 @@ namespace Pal.Client
|
|||||||
var pluginInterface = scope.ServiceProvider.GetRequiredService<DalamudPluginInterface>();
|
var pluginInterface = scope.ServiceProvider.GetRequiredService<DalamudPluginInterface>();
|
||||||
string backupPath = Path.Join(pluginInterface.GetPluginConfigDirectory(),
|
string backupPath = Path.Join(pluginInterface.GetPluginConfigDirectory(),
|
||||||
$"backup-{DateTime.Today.ToUniversalTime():yyyy-MM-dd}.data.sqlite3");
|
$"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<PalClientContext>();
|
_logger.LogInformation("Creating database backup '{Path}'", backupPath);
|
||||||
await using SqliteConnection source = new(db.Database.GetConnectionString());
|
try
|
||||||
await source.OpenAsync();
|
{
|
||||||
await using SqliteConnection backup = new($"Data Source={backupPath}");
|
File.Copy(sourcePath, backupPath);
|
||||||
source.BackupDatabase(backup);
|
}
|
||||||
SqliteConnection.ClearPool(backup);
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError(e, "Could not create backup");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_logger.LogInformation("Database backup in '{Path}' already exists", backupPath);
|
_logger.LogInformation("Database backup in '{Path}' already exists", backupPath);
|
||||||
|
@ -33,6 +33,7 @@ namespace Pal.Client
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal sealed class DependencyInjectionContext : IDisposable
|
internal sealed class DependencyInjectionContext : IDisposable
|
||||||
{
|
{
|
||||||
|
public const string DatabaseFileName = "palace-pal.data.sqlite3";
|
||||||
public static DalamudLoggerProvider LoggerProvider { get; } = new();
|
public static DalamudLoggerProvider LoggerProvider { get; } = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -89,7 +90,7 @@ namespace Pal.Client
|
|||||||
_serviceCollection.AddSingleton(new WindowSystem(typeof(DependencyInjectionContext).AssemblyQualifiedName));
|
_serviceCollection.AddSingleton(new WindowSystem(typeof(DependencyInjectionContext).AssemblyQualifiedName));
|
||||||
|
|
||||||
_sqliteConnectionString =
|
_sqliteConnectionString =
|
||||||
$"Data Source={Path.Join(pluginInterface.GetPluginConfigDirectory(), "palace-pal.data.sqlite3")}";
|
$"Data Source={Path.Join(pluginInterface.GetPluginConfigDirectory(), DatabaseFileName)}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public IServiceProvider BuildServiceContainer()
|
public IServiceProvider BuildServiceContainer()
|
||||||
|
Loading…
Reference in New Issue
Block a user