90 lines
4.6 KiB
C#
90 lines
4.6 KiB
C#
using Heimdall.Infrastructure.Data;
|
|||
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
||
|
|
using Heimdall.Infrastructure.Jobs;
|
||
|
|
using Heimdall.TestCommon;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
||
|
|
using Npgsql;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Infrastructure.Tests;
|
||
|
|
|
||
|
|
public class PartitionNameTests
|
||
|
|
{
|
||
|
|
[Theory]
|
||
|
|
[InlineData("camera_events_2025_01", true)]
|
||
|
|
[InlineData("camera_events_0001_12", true)]
|
||
|
|
[InlineData("camera_events_2025_00", false)]
|
||
|
|
[InlineData("camera_events_2025_13", false)]
|
||
|
|
[InlineData("camera_events_0000_01", false)]
|
||
|
|
[InlineData("camera_events_2025_1", false)]
|
||
|
|
[InlineData("camera_events_2025_01_extra", false)]
|
||
|
|
[InlineData("camera_events_2025-01", false)]
|
||
|
|
[InlineData("camera_events_+025_01", false)]
|
||
|
|
[InlineData("camera_events_2025_01", false)]
|
||
|
|
[InlineData("Camera_events_2025_01", false)]
|
||
|
|
public void NamesAreStrict(string name, bool valid)
|
||
|
|
=> PartitionManagerJob.TryParsePartitionDate(name, out _).ShouldBe(valid);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Collection("PostgresInfra")]
|
||
|
|
public class PartitionManagerJobTests(PostgreSqlContainerFixture postgres)
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public async Task Execute_DropsOnlyExpiredAttachedPartitions_AndClosesReaderBeforeDdl()
|
||
|
|
{
|
||
|
|
// Use an isolated database: the application fixtures create a nonpartitioned table.
|
||
|
|
var database = "partition_test_" + Guid.NewGuid().ToString("N");
|
||
|
|
await using var admin = new NpgsqlConnection(postgres.ConnectionString);
|
||
|
|
await admin.OpenAsync();
|
||
|
|
await using (var create = new NpgsqlCommand($"CREATE DATABASE {database}", admin))
|
||
|
|
await create.ExecuteNonQueryAsync();
|
||
|
|
var builder = new NpgsqlConnectionStringBuilder(postgres.ConnectionString) { Database = database, Pooling = false };
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var opts = new DbContextOptionsBuilder<AppDbContext>().UseNpgsql(builder.ConnectionString).Options;
|
||
|
|
var factory = new Factory(opts);
|
||
|
|
await using var db = factory.CreateDbContext();
|
||
|
|
var now = DateTime.UtcNow;
|
||
|
|
var month = new DateTime(now.Year, now.Month, 1, 0, 0, 0, DateTimeKind.Utc);
|
||
|
|
var old = month.AddMonths(-13);
|
||
|
|
var boundary = month.AddMonths(-12);
|
||
|
|
var expiredName = $"camera_events_{old:yyyy_MM}";
|
||
|
|
var boundaryName = $"camera_events_{boundary:yyyy_MM}";
|
||
|
|
var unrelatedName = $"camera_events_{old.AddMonths(-2):yyyy_MM}";
|
||
|
|
#pragma warning disable EF1002 // Test DDL identifiers generated exclusively from dates.
|
||
|
|
await db.Database.ExecuteSqlRawAsync($"""
|
||
|
|
CREATE TABLE camera_events (occurred_at timestamptz NOT NULL) PARTITION BY RANGE (occurred_at);
|
||
|
|
CREATE TABLE "{expiredName}" PARTITION OF camera_events FOR VALUES FROM ('{old:yyyy-MM-dd}') TO ('{old.AddMonths(1):yyyy-MM-dd}');
|
||
|
|
CREATE TABLE "{boundaryName}" PARTITION OF camera_events FOR VALUES FROM ('{boundary:yyyy-MM-dd}') TO ('{boundary.AddMonths(1):yyyy-MM-dd}');
|
||
|
|
CREATE TABLE camera_events_bad PARTITION OF camera_events DEFAULT;
|
||
|
|
CREATE TABLE "{unrelatedName}" (id int);
|
||
|
|
""");
|
||
|
|
#pragma warning restore EF1002
|
||
|
|
await new PartitionManagerJob(factory, NullLogger<PartitionManagerJob>.Instance).ExecuteAsync(CancellationToken.None);
|
||
|
|
// A second execution must be harmless.
|
||
|
|
await new PartitionManagerJob(factory, NullLogger<PartitionManagerJob>.Instance).ExecuteAsync(CancellationToken.None);
|
||
|
|
await using var conn = new NpgsqlConnection(builder.ConnectionString);
|
||
|
|
await conn.OpenAsync();
|
||
|
|
await using var cmd = new NpgsqlCommand("SELECT tablename FROM pg_tables WHERE schemaname = 'public'", conn);
|
||
|
|
await using var reader = await cmd.ExecuteReaderAsync();
|
||
|
|
var tables = new List<string>();
|
||
|
|
while (await reader.ReadAsync()) tables.Add(reader.GetString(0));
|
||
|
|
tables.ShouldNotContain(expiredName);
|
||
|
|
tables.ShouldContain(boundaryName);
|
||
|
|
tables.ShouldContain(unrelatedName);
|
||
|
|
tables.ShouldContain("camera_events_bad");
|
||
|
|
for (var i = 0; i <= 2; i++) tables.ShouldContain($"camera_events_{month.AddMonths(i):yyyy_MM}");
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
await using var drop = new NpgsqlCommand($"DROP DATABASE {database} WITH (FORCE)", admin);
|
||
|
|
await drop.ExecuteNonQueryAsync();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private sealed class Factory(DbContextOptions<AppDbContext> options) : IDbContextFactory<AppDbContext>
|
||
|
|
{
|
||
|
|
public AppDbContext CreateDbContext() => new(options, new AuditInterceptor());
|
||
|
|
}
|
||
|
|
}
|