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
+381 -497
View File
File diff suppressed because it is too large Load Diff
+57 -34
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,44 +170,63 @@ using (var scope = app.Services.CreateScope())
await conn.OpenAsync(); await conn.OpenAsync();
} }
// Check if AppSettings table already exists using (var cmdCheckTables = conn.CreateCommand())
using (var cmdCheck = conn.CreateCommand())
{ {
cmdCheck.CommandText = "SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'AppSettings');"; cmdCheckTables.CommandText = @"
var appSettingsExists = (bool)(await cmdCheck.ExecuteScalarAsync() ?? false); SELECT COUNT(*)
FROM information_schema.tables
if (appSettingsExists) WHERE table_schema = 'public'
AND table_name <> '__EFMigrationsHistory';";
var existingTableCount = Convert.ToInt32(await cmdCheckTables.ExecuteScalarAsync() ?? 0);
if (existingTableCount == 0)
{ {
// Ensure __EFMigrationsHistory table exists await db.Database.EnsureCreatedAsync();
using (var cmdCreateHistory = conn.CreateCommand()) databaseCreatedFromModel = true;
{ }
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 if (!databaseCreatedFromModel)
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) // 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())
{ {
// Seed the history table with the migrations that were already created by EnsureCreated in older versions cmdCreateHistory.CommandText = @"
using (var cmdInsertHistory = conn.CreateCommand()) 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)
{ {
cmdInsertHistory.CommandText = @" // Seed the history table with the migrations that were already created by EnsureCreated in older versions
INSERT INTO ""__EFMigrationsHistory"" (""MigrationId"", ""ProductVersion"") VALUES using (var cmdInsertHistory = conn.CreateCommand())
('20260520133634_Initial', '10.0.9'), {
('20260520200000_AddPublicHolidays', '10.0.9'), cmdInsertHistory.CommandText = @"
('20260522081459_AddMultiUser', '10.0.9'), INSERT INTO ""__EFMigrationsHistory"" (""MigrationId"", ""ProductVersion"") VALUES
('20260607213215_AddFlexTimeAndHolidayState', '10.0.9');"; ('20260520133634_Initial', '10.0.9'),
await cmdInsertHistory.ExecuteNonQueryAsync(); ('20260520200000_AddPublicHolidays', '10.0.9'),
('20260522081459_AddMultiUser', '10.0.9'),
('20260607213215_AddFlexTimeAndHolidayState', '10.0.9');";
await cmdInsertHistory.ExecuteNonQueryAsync();
}
} }
} }
} }
@@ -219,9 +239,12 @@ using (var scope = app.Services.CreateScope())
} }
} }
await db.Database.MigrateAsync(); if (!databaseCreatedFromModel)
{
await db.Database.MigrateAsync();
}
if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase)) if (dbProvider.Equals("PostgreSQL", StringComparison.OrdinalIgnoreCase) && !databaseCreatedFromModel)
{ {
try try
{ {