From 80d0308de811af869f7792628970e522cec64bb2 Mon Sep 17 00:00:00 2001 From: Niek Otten Date: Tue, 14 Jul 2026 10:03:14 +0200 Subject: [PATCH] fix(projection): serialise concurrent migrators with a pg advisory lock (refs #75) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit verify-up failed: the event-subscriber and projection-api both migrate the shared projection DB on start, and EF releases its migrations-history lock between individual migrations — harmless with one migration, but the new AddNotificationResource made a second, so a migrator re-applied it in the window between the other's two migrations ("column resource already exists"). Hold a session pg_advisory_lock across the whole MigrateAsync so the sequence runs exactly once; the second migrator then finds nothing pending. Verified by starting both images simultaneously against a fresh Postgres: both reach health, the resource column is created once, and __EFMigrationsHistory has both rows. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../ServiceCollectionExtensions.cs | 38 ++++++++++++++++++- .../ProjectionApi.Api/Program.cs | 5 ++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/services/projection-api/Projection.ReadModel/ServiceCollectionExtensions.cs b/services/projection-api/Projection.ReadModel/ServiceCollectionExtensions.cs index 1779f21..d191dd8 100644 --- a/services/projection-api/Projection.ReadModel/ServiceCollectionExtensions.cs +++ b/services/projection-api/Projection.ReadModel/ServiceCollectionExtensions.cs @@ -19,12 +19,46 @@ public static class ServiceCollectionExtensions return services; } + // A fixed application-scoped key for the migration advisory lock (any stable 64-bit constant). + private const long MigrationAdvisoryLockKey = 727501; + /// Apply any pending EF migrations. Called once on service start so a fresh stack - /// reaches a usable schema without a manual migration step (DoD: compose up reaches green). + /// reaches a usable schema without a manual migration step (DoD: compose up reaches green). + /// + /// The Event Subscriber and the projection-api share this DB and both migrate on start. EF's + /// migrations-history lock is released between individual migrations, so with more than one pending + /// migration two migrators can interleave and one re-applies a migration the other just did + /// ("column already exists"). Hold a Postgres session pg_advisory_lock across the whole + /// sequence so it runs exactly once; the second migrator then finds nothing pending. public static async Task MigrateProjectionAsync(this IServiceProvider services, CancellationToken ct = default) { await using var scope = services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); - await db.Database.MigrateAsync(ct); + var connection = db.Database.GetDbConnection(); + + await connection.OpenAsync(ct); + try + { + await ExecuteAsync(connection, $"SELECT pg_advisory_lock({MigrationAdvisoryLockKey})", ct); + try + { + await db.Database.MigrateAsync(ct); + } + finally + { + await ExecuteAsync(connection, $"SELECT pg_advisory_unlock({MigrationAdvisoryLockKey})", ct); + } + } + finally + { + await connection.CloseAsync(); + } + } + + private static async Task ExecuteAsync(System.Data.Common.DbConnection connection, string sql, CancellationToken ct) + { + await using var command = connection.CreateCommand(); + command.CommandText = sql; + await command.ExecuteNonQueryAsync(ct); } } diff --git a/services/projection-api/ProjectionApi.Api/Program.cs b/services/projection-api/ProjectionApi.Api/Program.cs index 74a24be..bc13277 100644 --- a/services/projection-api/ProjectionApi.Api/Program.cs +++ b/services/projection-api/ProjectionApi.Api/Program.cs @@ -10,8 +10,9 @@ builder.Services.AddProjectionReadModel(connectionString); var app = builder.Build(); -// Ensure the schema exists before serving reads. EF serialises concurrent migrators via the -// migrations-history lock, so it is safe that the Event Subscriber migrates too. +// Ensure the schema exists before serving reads. The Event Subscriber migrates this shared DB too; +// MigrateProjectionAsync serialises concurrent migrators with a session advisory lock so the whole +// migration sequence runs exactly once (see ServiceCollectionExtensions). await app.Services.MigrateProjectionAsync(); app.MapGet("/health", () => "Healthy");