Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Refactor (#190)

Leendert de Borst3b5e944417f7
diff --git a/src/AliasVault.Admin/Program.cs b/src/AliasVault.Admin/Program.cs
index ae2abb8c5..e1c5c4cfc 100644
--- a/src/AliasVault.Admin/Program.cs
+++ b/src/AliasVault.Admin/Program.cs
@@ -133,7 +133,7 @@ app.MapRazorComponents<App>()
using (var scope = app.Services.CreateScope())
{
var container = scope.ServiceProvider;
- await using var db = container.GetRequiredService<IAliasServerDbContextFactory>().CreateDbContext();
+ await using var db = await container.GetRequiredService<IAliasServerDbContextFactory>().CreateDbContextAsync();
await db.Database.MigrateAsync();
await StartupTasks.CreateRolesIfNotExist(scope.ServiceProvider);
diff --git a/src/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs b/src/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs
index 14262fc89..021b266ea 100644
--- a/src/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs
+++ b/src/Databases/AliasServerDb/Configuration/DatabaseConfiguration.cs
@@ -52,7 +52,6 @@ public static class DatabaseConfiguration
case "postgresql":
services.AddSingleton<IAliasServerDbContextFactory, PostgresqlDbContextFactory>();
break;
- case "sqlite":
default:
services.AddSingleton<IAliasServerDbContextFactory, SqliteDbContextFactory>();
break;
diff --git a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
index 1a282f58b..519482e79 100644
--- a/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
+++ b/src/Tests/AliasVault.IntegrationTests/SmtpServer/TestHostBuilder.cs
@@ -184,6 +184,8 @@ public class TestHostBuilder : IAsyncDisposable
""";
await cmd.ExecuteNonQueryAsync();
}
+
+ GC.SuppressFinalize(this);
}
}
}
diff --git a/src/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs b/src/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs
index a54325b6a..71e109494 100644
--- a/src/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs
+++ b/src/Tests/AliasVault.IntegrationTests/TaskRunner/TestHostBuilder.cs
@@ -149,5 +149,7 @@ public class TestHostBuilder : IAsyncDisposable
await cmd.ExecuteNonQueryAsync();
}
}
+
+ GC.SuppressFinalize(this);
}
}
diff --git a/src/Utilities/AliasVault.InstallCli/Program.cs b/src/Utilities/AliasVault.InstallCli/Program.cs
index 5d30a2703..ee34eb6bd 100644
--- a/src/Utilities/AliasVault.InstallCli/Program.cs
+++ b/src/Utilities/AliasVault.InstallCli/Program.cs
@@ -10,12 +10,12 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
// Add return type for top-level statements
-return await Program.Run(args);
+return await Run(args);
/// <summary>
/// Handles the migration of data between SQLite and PostgreSQL databases and password hashing utilities.
/// </summary>
-public partial class Program
+public static partial class Program
{
/// <summary>
/// Runs the program with the given arguments.
@@ -222,15 +222,6 @@ public partial class Program
if (items.Count > 0)
{
- // Remove any existing entries in the destination table
- var existingEntries = await destination.ToListAsync();
- if (existingEntries.Any())
- {
- Console.WriteLine($"Removing {existingEntries.Count} existing entries from {tableName}...");
- destination.RemoveRange(existingEntries);
- await destinationContext.SaveChangesAsync();
- }
-
const int batchSize = 30;
foreach (var batch in items.Chunk(batchSize))
{
@@ -242,26 +233,7 @@ public partial class Program
}
catch (DbUpdateConcurrencyException ex)
{
- // Handle concurrency conflict
- foreach (var entry in ex.Entries)
- {
- // Get current values from database
- var databaseValues = await entry.GetDatabaseValuesAsync();
-
- if (databaseValues == null)
- {
- // Row was deleted
- entry.State = EntityState.Detached;
- }
- else
- {
- // Update the original values with the database values
- entry.OriginalValues.SetValues(databaseValues);
-
- // Retry the save operation
- await destinationContext.SaveChangesAsync();
- }
- }
+ await HandleConcurrencyConflict(ex, destinationContext);
}
}
}
@@ -272,4 +244,29 @@ public partial class Program
throw new ArgumentException($"The amount of records in the source is greater than the destination. Check if the migration is working correctly.");
}
}
+
+ /// <summary>
+ /// Handles a concurrency conflict by updating the original values with the database values.
+ /// </summary>
+ /// <param name="ex">The DbUpdateConcurrencyException that occurred.</param>
+ /// <param name="destinationContext">The destination database context.</param>
+ /// <returns>A task representing the asynchronous operation.</returns>
+ private static async Task HandleConcurrencyConflict(
+ DbUpdateConcurrencyException ex,
+ DbContext destinationContext)
+ {
+ foreach (var entry in ex.Entries)
+ {
+ var databaseValues = await entry.GetDatabaseValuesAsync();
+ if (databaseValues == null)
+ {
+ entry.State = EntityState.Detached;
+ }
+ else
+ {
+ entry.OriginalValues.SetValues(databaseValues);
+ await destinationContext.SaveChangesAsync();
+ }
+ }
+ }
}