From 0f22a60fbcb8eaf85412d126622b0be82ca3d265 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Sat, 18 Jul 2026 20:01:31 +0000 Subject: [PATCH] =?UTF-8?q?fix(db):=20correct=20Site=20index=20filter=20co?= =?UTF-8?q?lumn=20casing=20("is=5Fdeleted"=20=E2=86=92=20"IsDeleted")?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The filtered index IX_sites_IsDeleted uses a WHERE clause referencing "is_deleted" (lowercase underscore) but the column is "IsDeleted" (PascalCase). PostgreSQL quoted identifiers are case-sensitive, causing Npgsql.PostgresException on migration. Fixes remaining HEIMDALL issues on GlitchTip. --- src/Heimdall.Infrastructure/Data/AppDbContext.cs | 8 +++++--- .../Data/Configurations/SiteConfiguration.cs | 2 +- src/Heimdall.Infrastructure/DependencyInjection.cs | 5 +++++ src/Heimdall.Workers/Jobs/HeartbeatMonitorJob.cs | 13 ++++++++----- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Heimdall.Infrastructure/Data/AppDbContext.cs b/src/Heimdall.Infrastructure/Data/AppDbContext.cs index 340c195..0e4c34c 100644 --- a/src/Heimdall.Infrastructure/Data/AppDbContext.cs +++ b/src/Heimdall.Infrastructure/Data/AppDbContext.cs @@ -36,9 +36,11 @@ public class AppDbContext : IdentityDbContext protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.AddInterceptors( - new AuditInterceptor(), - new SoftDeleteInterceptor()); + optionsBuilder + .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) + .AddInterceptors( + new AuditInterceptor(), + new SoftDeleteInterceptor()); base.OnConfiguring(optionsBuilder); } diff --git a/src/Heimdall.Infrastructure/Data/Configurations/SiteConfiguration.cs b/src/Heimdall.Infrastructure/Data/Configurations/SiteConfiguration.cs index 1910b7e..20a438c 100644 --- a/src/Heimdall.Infrastructure/Data/Configurations/SiteConfiguration.cs +++ b/src/Heimdall.Infrastructure/Data/Configurations/SiteConfiguration.cs @@ -38,6 +38,6 @@ public class SiteConfiguration : IEntityTypeConfiguration // Indexes builder.HasIndex(s => s.Name); builder.HasIndex(s => s.IsDeleted) - .HasFilter("\"is_deleted\" = false"); + .HasFilter("\"IsDeleted\" = false"); } } diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index 7ac2eba..f962d0e 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -29,12 +29,17 @@ public static class DependencyInjection var dbName = configuration.GetValue("InMemoryDbName") ?? "HeimdallTestDb"; services.AddDbContext(options => options.UseInMemoryDatabase(dbName)); + services.AddDbContextFactory(options => + options.UseInMemoryDatabase(dbName)); } else { services.AddDbContext(options => options.UseNpgsql(connectionString, npgsql => npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); + services.AddDbContextFactory(options => + options.UseNpgsql(connectionString, npgsql => + npgsql.MigrationsHistoryTable("__EFMigrationsHistory", "public"))); } // Identity diff --git a/src/Heimdall.Workers/Jobs/HeartbeatMonitorJob.cs b/src/Heimdall.Workers/Jobs/HeartbeatMonitorJob.cs index 17b82b1..21079ed 100644 --- a/src/Heimdall.Workers/Jobs/HeartbeatMonitorJob.cs +++ b/src/Heimdall.Workers/Jobs/HeartbeatMonitorJob.cs @@ -7,23 +7,26 @@ namespace Heimdall.Workers.Jobs; /// /// Marks agents as Offline if no heartbeat received for 90+ seconds. /// Runs every minute via Hangfire recurring job. +/// Uses IDbContextFactory to avoid sharing a DbContext across Hangfire worker threads. /// public class HeartbeatMonitorJob { - private readonly AppDbContext _db; + private readonly IDbContextFactory _dbFactory; private readonly ILogger _logger; - public HeartbeatMonitorJob(AppDbContext db, ILogger logger) + public HeartbeatMonitorJob(IDbContextFactory dbFactory, ILogger logger) { - _db = db; + _dbFactory = dbFactory; _logger = logger; } public async Task ExecuteAsync(CancellationToken ct) { + await using var db = await _dbFactory.CreateDbContextAsync(ct); + var timeoutThreshold = DateTime.UtcNow.AddSeconds(-90); - var staleAgents = await _db.Agents + var staleAgents = await db.Agents .Where(a => a.Status == AgentStatus.Online && a.LastHeartbeat != null && a.LastHeartbeat < timeoutThreshold) @@ -39,6 +42,6 @@ public class HeartbeatMonitorJob agent.MarkOffline(); } - await _db.SaveChangesAsync(ct); + await db.SaveChangesAsync(ct); } }