|
1 | 1 | namespace ServiceControl.Audit.Persistence.Sql.SqlServer.Infrastructure; |
2 | 2 |
|
3 | 3 | using Microsoft.EntityFrameworkCore; |
| 4 | +using ServiceControl.Audit.Persistence.Sql.Core.Abstractions; |
4 | 5 | using ServiceControl.Audit.Persistence.Sql.Core.DbContexts; |
5 | 6 | using ServiceControl.Audit.Persistence.Sql.Core.Infrastructure; |
6 | 7 |
|
7 | 8 | // Partition/table names cannot be parameterized in SQL; all values come from internal constants and date formatting |
8 | 9 | #pragma warning disable EF1002, EF1003 |
9 | | -public class SqlServerPartitionManager : IPartitionManager |
| 10 | +public class SqlServerPartitionManager(MinimumRequiredStorageState storageState) : IPartitionManager |
10 | 11 | { |
11 | 12 | const string PartitionFunctionName = "pf_CreatedOn"; |
12 | 13 | const string PartitionSchemeName = "ps_CreatedOn"; |
@@ -54,14 +55,23 @@ public async Task DropPartition(AuditDbContextBase dbContext, DateTime partition |
54 | 55 | // target partition is scanned. |
55 | 56 | dbContext.Database.SetCommandTimeout(TimeSpan.FromMinutes(5)); |
56 | 57 |
|
57 | | - foreach (var table in PartitionedTables) |
| 58 | + storageState.CanIngestMore = false; |
| 59 | + try |
58 | 60 | { |
| 61 | + foreach (var table in PartitionedTables) |
| 62 | + { |
| 63 | + await dbContext.Database.ExecuteSqlRawAsync( |
| 64 | + "DELETE FROM " + table + " WHERE CreatedOn >= '" + hourStr + "' AND CreatedOn < '" + nextHourStr + "'", ct); |
| 65 | + } |
| 66 | + |
59 | 67 | await dbContext.Database.ExecuteSqlRawAsync( |
60 | | - "DELETE FROM " + table + " WHERE CreatedOn >= '" + hourStr + "' AND CreatedOn < '" + nextHourStr + "'", ct); |
61 | | - } |
| 68 | + "ALTER PARTITION FUNCTION " + PartitionFunctionName + "() MERGE RANGE ('" + hourStr + "')", ct); |
62 | 69 |
|
63 | | - await dbContext.Database.ExecuteSqlRawAsync( |
64 | | - "ALTER PARTITION FUNCTION " + PartitionFunctionName + "() MERGE RANGE ('" + hourStr + "')", ct); |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + storageState.CanIngestMore = true; |
| 74 | + } |
65 | 75 | } |
66 | 76 |
|
67 | 77 | public async Task<List<DateTime>> GetExpiredPartitions(AuditDbContextBase dbContext, DateTime cutoff, CancellationToken ct) |
|
0 commit comments