Fixed removing temp. database file when importing JSON
This commit is contained in:
parent
18fd1faedd
commit
96823aa948
@ -1,9 +1,9 @@
|
||||
using DataModel.Database;
|
||||
using System.Diagnostics;
|
||||
using DataModel.Database;
|
||||
using DataModel.Database.Common;
|
||||
using DataModel.MigrationScripts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace DataModel;
|
||||
|
||||
@ -13,12 +13,10 @@ public static class Setup
|
||||
private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate";
|
||||
|
||||
private static string USED_DATA_FILE = string.Empty;
|
||||
private static SetupMaintenance SETUP_MAINTENANCE = new();
|
||||
public static SetupMaintenance SETUP_MAINTENANCE = new();
|
||||
|
||||
public static string DataFile => Setup.USED_DATA_FILE;
|
||||
|
||||
public static SetupMaintenance Maintenance => Setup.SETUP_MAINTENANCE;
|
||||
|
||||
/// <summary>
|
||||
/// Tries to migrate the database.
|
||||
/// </summary>
|
||||
@ -44,16 +42,19 @@ public static class Setup
|
||||
/// </summary>
|
||||
public static async Task ImportDataAndAddDatabase(this IServiceCollection serviceCollection, string path2JSONFile)
|
||||
{
|
||||
Console.WriteLine($"Importing the data from the JSON file '{path2JSONFile}' into a new database.");
|
||||
var tempPath = Path.GetTempFileName();
|
||||
|
||||
Setup.USED_DATA_FILE = tempPath;
|
||||
Setup.SETUP_MAINTENANCE = new(path2JSONFile, true);
|
||||
Console.WriteLine($"The temporary database file is: {tempPath}");
|
||||
serviceCollection.AddDbContext<DataContext>(options => options.UseSqlite($"Filename={tempPath};Mode={DB_READ_WRITE_CREATE_MODE};"), ServiceLifetime.Transient);
|
||||
|
||||
// Get the database service:
|
||||
await using var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
var dbContext = serviceProvider.GetRequiredService<DataContext>();
|
||||
|
||||
Setup.USED_DATA_FILE = tempPath;
|
||||
Setup.SETUP_MAINTENANCE = new(tempPath, true);
|
||||
|
||||
// Migrate the database to create the tables etc.:
|
||||
await Setup.PerformDataMigration(dbContext);
|
||||
|
||||
@ -128,7 +129,6 @@ public static class Setup
|
||||
return dbContext;
|
||||
}
|
||||
|
||||
|
||||
public readonly record struct SetupMaintenance(string PathToDataFile = "", bool RemoveTempDatabaseAfterwards = false) : IDisposable
|
||||
{
|
||||
public void Dispose()
|
||||
@ -136,9 +136,22 @@ public static class Setup
|
||||
if (!this.RemoveTempDatabaseAfterwards)
|
||||
return;
|
||||
|
||||
Console.WriteLine("Removing the temporary database file...");
|
||||
try
|
||||
{
|
||||
File.Delete(this.PathToDataFile);
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new()
|
||||
{
|
||||
FileName = "cmd.exe",
|
||||
Arguments = $@"/C del /Q /F ""{Setup.SETUP_MAINTENANCE.PathToDataFile}""",
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
|
||||
process.Start();
|
||||
Console.WriteLine($"The temporary database file '{this.PathToDataFile}' has been removed.");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
@ -2,5 +2,5 @@
|
||||
|
||||
public static class Version
|
||||
{
|
||||
public static string Text => $"v0.8.2 (2023-02-11), .NET {Environment.Version}";
|
||||
public static string Text => $"v0.8.3 (2023-02-11), .NET {Environment.Version}";
|
||||
}
|
@ -60,12 +60,9 @@ internal static class Program
|
||||
}
|
||||
});
|
||||
|
||||
// Tear down the setup:
|
||||
using var setupMaintenance = Setup.Maintenance;
|
||||
|
||||
// Get the host out of the DI system:
|
||||
var host = builder.Build();
|
||||
|
||||
using (var host = builder.Build())
|
||||
{
|
||||
// Create a service scope:
|
||||
using (var scope = host.Services.CreateScope())
|
||||
{
|
||||
@ -86,4 +83,8 @@ internal static class Program
|
||||
} while (Program.RestartMainApp);
|
||||
}
|
||||
}
|
||||
|
||||
// Tear down the setup:
|
||||
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => Setup.SETUP_MAINTENANCE.Dispose();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user