Fix database being in use while trying a master update.

This commit is contained in:
2021-12-10 02:44:01 +00:00
parent ffc23068d7
commit 36f57a58cb
4 changed files with 40 additions and 10 deletions

View File

@@ -277,9 +277,9 @@ namespace Aaru.Core
Start(ctx => Start(ctx =>
{ {
ProgressTask task = ctx.AddTask("Adding known iNES/NES 2.0 headers"); ProgressTask task = ctx.AddTask("Adding known iNES/NES 2.0 headers");
task.MaxValue = sync.NesHeaders.Count; task.MaxValue = sync.NesHeaders?.Count ?? 0;
foreach(NesHeaderDto header in sync.NesHeaders) foreach(NesHeaderDto header in sync.NesHeaders ?? new List<NesHeaderDto>())
{ {
task.Increment(1); task.Increment(1);

View File

@@ -85,11 +85,14 @@ public sealed class AaruContext : DbContext
/// <summary>Creates a database context with the database in the specified path</summary> /// <summary>Creates a database context with the database in the specified path</summary>
/// <param name="dbPath">Path to database file</param> /// <param name="dbPath">Path to database file</param>
/// <param name="pooling">Enable database pooling</param>
/// <returns>Database context</returns> /// <returns>Database context</returns>
public static AaruContext Create(string dbPath) public static AaruContext Create(string dbPath, bool pooling = true)
{ {
var optionsBuilder = new DbContextOptionsBuilder(); var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseLazyLoadingProxies().UseSqlite($"Data Source={dbPath}");
optionsBuilder.UseLazyLoadingProxies().
UseSqlite(!pooling ? $"Data Source={dbPath};Pooling=False" : $"Data Source={dbPath}");
return new AaruContext(optionsBuilder.Options); return new AaruContext(optionsBuilder.Options);
} }

View File

@@ -121,15 +121,25 @@ namespace Aaru.Gui.ViewModels.Windows
Task.Run(() => Task.Run(() =>
{ {
AaruContext ctx; AaruContext ctx = null;
try try
{ {
ctx = AaruContext.Create(Settings.Settings.LocalDbPath); ctx = AaruContext.Create(Settings.Settings.LocalDbPath, false);
ctx.Database.Migrate(); ctx.Database.Migrate();
} }
catch(NotSupportedException) catch(NotSupportedException)
{ {
try
{
ctx?.Database.CloseConnection();
ctx?.Dispose();
}
catch(Exception)
{
// Should not ever arrive here, but if it does, keep trying to replace it anyway
}
File.Delete(Settings.Settings.LocalDbPath); File.Delete(Settings.Settings.LocalDbPath);
ctx = AaruContext.Create(Settings.Settings.LocalDbPath); ctx = AaruContext.Create(Settings.Settings.LocalDbPath);
ctx.Database.EnsureCreated(); ctx.Database.EnsureCreated();
@@ -186,7 +196,7 @@ namespace Aaru.Gui.ViewModels.Windows
// TODO: Update database // TODO: Update database
} }
var mainContext = AaruContext.Create(Settings.Settings.MainDbPath); var mainContext = AaruContext.Create(Settings.Settings.MainDbPath, false);
if(mainContext.Database.GetPendingMigrations().Any()) if(mainContext.Database.GetPendingMigrations().Any())
{ {
@@ -202,6 +212,8 @@ namespace Aaru.Gui.ViewModels.Windows
ErrorWriteLine("Exception trying to remove old database version, cannot continue..."); ErrorWriteLine("Exception trying to remove old database version, cannot continue...");
AaruConsole.ErrorWriteLine("Please manually remove file at {0}", Settings.Settings.MainDbPath); AaruConsole.ErrorWriteLine("Please manually remove file at {0}", Settings.Settings.MainDbPath);
return;
} }
// TODO: Update database // TODO: Update database

View File

@@ -44,6 +44,7 @@ using Aaru.Commands.Device;
using Aaru.Commands.Filesystem; using Aaru.Commands.Filesystem;
using Aaru.Commands.Image; using Aaru.Commands.Image;
using Aaru.Commands.Media; using Aaru.Commands.Media;
using Aaru.CommonTypes.Enums;
using Aaru.Console; using Aaru.Console;
using Aaru.Core; using Aaru.Core;
using Aaru.Database; using Aaru.Database;
@@ -109,15 +110,25 @@ namespace Aaru
Settings.Settings.LoadSettings(); Settings.Settings.LoadSettings();
AaruContext ctx; AaruContext ctx=null;
try try
{ {
ctx = AaruContext.Create(Settings.Settings.LocalDbPath); ctx = AaruContext.Create(Settings.Settings.LocalDbPath, false);
ctx.Database.Migrate(); ctx.Database.Migrate();
} }
catch(NotSupportedException) catch(NotSupportedException)
{ {
try
{
ctx?.Database.CloseConnection();
ctx?.Dispose();
}
catch(Exception)
{
// Should not ever arrive here, but if it does, keep trying to replace it anyway
}
File.Delete(Settings.Settings.LocalDbPath); File.Delete(Settings.Settings.LocalDbPath);
ctx = AaruContext.Create(Settings.Settings.LocalDbPath); ctx = AaruContext.Create(Settings.Settings.LocalDbPath);
ctx.Database.EnsureCreated(); ctx.Database.EnsureCreated();
@@ -160,7 +171,7 @@ namespace Aaru
UpdateCommand.DoUpdate(true); UpdateCommand.DoUpdate(true);
} }
var mainContext = AaruContext.Create(Settings.Settings.MainDbPath); var mainContext = AaruContext.Create(Settings.Settings.MainDbPath, false);
if(mainContext.Database.GetPendingMigrations().Any()) if(mainContext.Database.GetPendingMigrations().Any())
{ {
@@ -174,8 +185,12 @@ namespace Aaru
{ {
AaruConsole.ErrorWriteLine("Exception trying to remove old database version, cannot continue..."); AaruConsole.ErrorWriteLine("Exception trying to remove old database version, cannot continue...");
AaruConsole.ErrorWriteLine("Please manually remove file at {0}", Settings.Settings.MainDbPath); AaruConsole.ErrorWriteLine("Please manually remove file at {0}", Settings.Settings.MainDbPath);
return (int)ErrorNumber.CannotRemoveDatabase;
} }
mainContext.Database.CloseConnection();
mainContext.Dispose();
UpdateCommand.DoUpdate(true); UpdateCommand.DoUpdate(true);
} }