This commit is contained in:
Wieland, Marc
2026-06-26 15:34:34 +02:00
parent ab966c66d0
commit 6ff8743dad
2 changed files with 438 additions and 531 deletions
+378 -494
View File
File diff suppressed because it is too large Load Diff
+24 -1
View File
@@ -158,6 +158,7 @@ using (var scope = app.Services.CreateScope())
{ {
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<TimetrackerDbContext>>(); var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<TimetrackerDbContext>>();
await using var db = await factory.CreateDbContextAsync(); await using var db = await factory.CreateDbContextAsync();
var databaseCreatedFromModel = false;
if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase)) if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase))
{ {
@@ -169,6 +170,24 @@ using (var scope = app.Services.CreateScope())
await conn.OpenAsync(); await conn.OpenAsync();
} }
using (var cmdCheckTables = conn.CreateCommand())
{
cmdCheckTables.CommandText = @"
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name <> '__EFMigrationsHistory';";
var existingTableCount = Convert.ToInt32(await cmdCheckTables.ExecuteScalarAsync() ?? 0);
if (existingTableCount == 0)
{
await db.Database.EnsureCreatedAsync();
databaseCreatedFromModel = true;
}
}
if (!databaseCreatedFromModel)
{
// Check if AppSettings table already exists // Check if AppSettings table already exists
using (var cmdCheck = conn.CreateCommand()) using (var cmdCheck = conn.CreateCommand())
{ {
@@ -213,15 +232,19 @@ using (var scope = app.Services.CreateScope())
} }
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Fehler beim Vorbereiten der PostgreSQL Migrationshistorie: {ex.Message}"); Console.WriteLine($"Fehler beim Vorbereiten der PostgreSQL Migrationshistorie: {ex.Message}");
} }
} }
if (!databaseCreatedFromModel)
{
await db.Database.MigrateAsync(); await db.Database.MigrateAsync();
}
if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase)) if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase) && !databaseCreatedFromModel)
{ {
try try
{ {