Merge pull request 'fixed' (#13) from testbranch into main

Reviewed-on: #13
This commit was merged in pull request #13.
This commit is contained in:
2026-06-25 18:59:26 +00:00
+61
View File
@@ -133,6 +133,67 @@ using (var scope = app.Services.CreateScope())
{
var factory = scope.ServiceProvider.GetRequiredService<IDbContextFactory<TimetrackerDbContext>>();
await using var db = await factory.CreateDbContextAsync();
if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase))
{
try
{
var conn = db.Database.GetDbConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
await conn.OpenAsync();
}
// Check if AppSettings table already exists
using (var cmdCheck = conn.CreateCommand())
{
cmdCheck.CommandText = "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'AppSettings');";
var appSettingsExists = (bool)(await cmdCheck.ExecuteScalarAsync() ?? false);
if (appSettingsExists)
{
// Ensure __EFMigrationsHistory table exists
using (var cmdCreateHistory = conn.CreateCommand())
{
cmdCreateHistory.CommandText = @"
CREATE TABLE IF NOT EXISTS ""__EFMigrationsHistory"" (
""MigrationId"" character varying(150) NOT NULL,
""ProductVersion"" character varying(32) NOT NULL,
CONSTRAINT ""PK___EFMigrationsHistory"" PRIMARY KEY (""MigrationId"")
);";
await cmdCreateHistory.ExecuteNonQueryAsync();
}
// Check if the initial migration is already recorded
using (var cmdCheckHistory = conn.CreateCommand())
{
cmdCheckHistory.CommandText = @"SELECT COUNT(*) FROM ""__EFMigrationsHistory"" WHERE ""MigrationId"" = '20260520133634_Initial';";
var historyCount = Convert.ToInt32(await cmdCheckHistory.ExecuteScalarAsync() ?? 0);
if (historyCount == 0)
{
// Seed the history table with the migrations that were already created by EnsureCreated in older versions
using (var cmdInsertHistory = conn.CreateCommand())
{
cmdInsertHistory.CommandText = @"
INSERT INTO ""__EFMigrationsHistory"" (""MigrationId"", ""ProductVersion"") VALUES
('20260520133634_Initial', '10.0.9'),
('20260520200000_AddPublicHolidays', '10.0.9'),
('20260522081459_AddMultiUser', '10.0.9'),
('20260607213215_AddFlexTimeAndHolidayState', '10.0.9');";
await cmdInsertHistory.ExecuteNonQueryAsync();
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Fehler beim Vorbereiten der PostgreSQL Migrationshistorie: {ex.Message}");
}
}
await db.Database.MigrateAsync();
}