< Summary

Information
Class: Anichron.Infrastructure.Data.DatabaseFacadeExtensions
Assembly: Anichron.Infrastructure
File(s): /home/runner/work/anichron/anichron/src/Anichron.Infrastructure/Data/DatabaseFacadeExtensions.cs
Tag: 19_45211875487
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 61
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MigrateWithAdvisoryLockAsync()100%210%
.ctor(...)100%210%
TryAcquireAsync()100%210%
ReleaseAsync()100%210%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Infrastructure/Data/DatabaseFacadeExtensions.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.EntityFrameworkCore.Infrastructure;
 3using System.Data.Common;
 4using System.Diagnostics.CodeAnalysis;
 5
 6namespace Anichron.Infrastructure.Data;
 7
 8public static class DatabaseFacadeExtensions
 9{
 10    public static async Task MigrateWithAdvisoryLockAsync(
 11        this DatabaseFacade database, CancellationToken ct, int maxAttempts = 30)
 012    {
 13        // Explicitly hold the connection open so that all operations — acquire lock,
 14        // migrate, release lock — run on the same PostgreSQL session. Session-level advisory
 15        // locks are tied to the session; a different connection would see a different lock.
 016        await database.OpenConnectionAsync(ct);
 17        try
 018        {
 019            var advisoryLock = new PostgresAdvisoryLock(database.GetDbConnection());
 020            await new AdvisoryLockMigrator(advisoryLock, TimeProvider.System, maxAttempts)
 021                .RunAsync(database.MigrateAsync, ct);
 022        }
 23        finally
 024        {
 025            await database.CloseConnectionAsync();
 026        }
 027    }
 28
 29    // S2077 flags the two interpolated pg_advisory_lock command texts below as SQL built by
 30    // string formatting. There is no injection surface: the only interpolated value is
 31    // PostgresConstants.MigrationAdvisoryLockId, a `const long` fixed at compile time, and no
 32    // caller can influence it. The rule matches the interpolation PATTERN, not a reachable taint
 33    // path, so this is a false positive rather than a finding to fix.
 34    //
 35    // Suppressed at the class rather than in .editorconfig so the justification travels with the
 36    // code a reader is looking at. It became an ERROR rather than a warning when
 37    // SonarAnalyzer.CSharp went 10.25 → 10.34 under TreatWarningsAsErrors.
 38    //
 39    // 📌 Parameterising both commands would remove the suppression and is worth doing on its own
 40    // merits — but it is a behaviour change, and this is a dependency-bump PR.
 41    [SuppressMessage(
 42        "Major Code Smell",
 43        "S2077:Formatting SQL queries is security-sensitive",
 44        Justification = "Interpolates only a compile-time const; no caller-controlled input reaches this SQL.")]
 045    private sealed class PostgresAdvisoryLock(DbConnection connection) : IAdvisoryLock
 46    {
 47        public async Task<bool> TryAcquireAsync(CancellationToken ct)
 048        {
 049            await using var command = connection.CreateCommand();
 050            command.CommandText = $"SELECT pg_try_advisory_lock({PostgresConstants.MigrationAdvisoryLockId})";
 051            return (bool)(await command.ExecuteScalarAsync(ct))!;
 052        }
 53
 54        public async Task ReleaseAsync(CancellationToken ct)
 055        {
 056            await using var command = connection.CreateCommand();
 057            command.CommandText = $"SELECT pg_advisory_unlock({PostgresConstants.MigrationAdvisoryLockId})";
 058            await command.ExecuteNonQueryAsync(ct);
 059        }
 60    }
 61}