| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.EntityFrameworkCore.Infrastructure; |
| | | 3 | | using System.Data.Common; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Infrastructure.Data; |
| | | 7 | | |
| | | 8 | | public static class DatabaseFacadeExtensions |
| | | 9 | | { |
| | | 10 | | public static async Task MigrateWithAdvisoryLockAsync( |
| | | 11 | | this DatabaseFacade database, CancellationToken ct, int maxAttempts = 30) |
| | 0 | 12 | | { |
| | | 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. |
| | 0 | 16 | | await database.OpenConnectionAsync(ct); |
| | | 17 | | try |
| | 0 | 18 | | { |
| | 0 | 19 | | var advisoryLock = new PostgresAdvisoryLock(database.GetDbConnection()); |
| | 0 | 20 | | await new AdvisoryLockMigrator(advisoryLock, TimeProvider.System, maxAttempts) |
| | 0 | 21 | | .RunAsync(database.MigrateAsync, ct); |
| | 0 | 22 | | } |
| | | 23 | | finally |
| | 0 | 24 | | { |
| | 0 | 25 | | await database.CloseConnectionAsync(); |
| | 0 | 26 | | } |
| | 0 | 27 | | } |
| | | 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.")] |
| | 0 | 45 | | private sealed class PostgresAdvisoryLock(DbConnection connection) : IAdvisoryLock |
| | | 46 | | { |
| | | 47 | | public async Task<bool> TryAcquireAsync(CancellationToken ct) |
| | 0 | 48 | | { |
| | 0 | 49 | | await using var command = connection.CreateCommand(); |
| | 0 | 50 | | command.CommandText = $"SELECT pg_try_advisory_lock({PostgresConstants.MigrationAdvisoryLockId})"; |
| | 0 | 51 | | return (bool)(await command.ExecuteScalarAsync(ct))!; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | public async Task ReleaseAsync(CancellationToken ct) |
| | 0 | 55 | | { |
| | 0 | 56 | | await using var command = connection.CreateCommand(); |
| | 0 | 57 | | command.CommandText = $"SELECT pg_advisory_unlock({PostgresConstants.MigrationAdvisoryLockId})"; |
| | 0 | 58 | | await command.ExecuteNonQueryAsync(ct); |
| | 0 | 59 | | } |
| | | 60 | | } |
| | | 61 | | } |