Fase 3b: de Hero bewaart zijn foto's in de bibliotheektabel (schakelaar uit), HeroImages blijft een kopie, controle bij het opstarten, FK-migratie met Down

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 00:10:58 +01:00
co-authored by Claude Sonnet 5.5
parent 53e1e3c8a6
commit 7246efccc1
18 changed files with 957 additions and 11 deletions
@@ -0,0 +1,185 @@
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Logging.Abstractions;
using MySql.Data.MySqlClient;
using SlpModularCms.Core.Hosting;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Services;
using SlpModularCms.Modules.Media.Data;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>
/// Phase 3b against a real MariaDB in a database of its own: both modules' migrations, then the hero in library storage mode
/// really writes and reads rows of MediaAssets, a slide can point at a photo that is only there (the foreign key to HeroImages
/// is gone), HeroImages is kept a copy, and the foreign key comes back with the migration's Down. Runs when
/// <c>ConnectionStrings__DefaultConnection</c> is set (CI sets it); otherwise it does nothing.
/// </summary>
public class HeroPhotosInLibraryMariaDbTests
{
private static string? ConnectionString()
{
var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection");
return string.IsNullOrWhiteSpace(baseString)
? null
: new MySqlConnectionStringBuilder(baseString) { Database = "hero_phase3_test_" + Guid.NewGuid().ToString("N")[..12] }.ConnectionString;
}
private static HeroDbContext OpenHero(string connectionString, bool inLibrary)
=> new(
new DbContextOptionsBuilder<HeroDbContext>()
.UseMySQL(connectionString)
.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>()
.ReplaceService<IModelCacheKeyFactory, HeroModelCacheKeyFactory>()
.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning))
.Options,
new HeroStorage(inLibrary));
private static MediaDbContext OpenMedia(string connectionString)
=> new(new DbContextOptionsBuilder<MediaDbContext>().UseMySQL(connectionString).ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>().Options);
private static HeroImage Photo(string sha, string set = "0123456789abcdef")
=> new()
{
Id = Guid.NewGuid(),
OriginalSha256 = sha,
OriginalKey = "originals/" + sha[..8] + ".jpg",
SourceFormat = "jpg",
Width = 3000,
Height = 2000,
Bytes = 2_000_000,
FocusX = 0.4,
FocusY = 0.6,
Zoom = 1.5,
WideFocusX = 0.1,
SetHash = set,
Variants = [new HeroVariant("wide", 480, 270, "image/webp", "wide-480.webp", 1234)],
CreatedAt = new DateTimeOffset(2026, 11, 17, 12, 0, 0, TimeSpan.Zero),
CreatedByUserId = Guid.NewGuid(),
OriginalFileName = "zomer.jpg",
};
[Fact]
public async Task The_hero_in_library_mode_writes_and_reads_rows_of_MediaAssets_and_keeps_HeroImages_a_copy()
{
var connection = ConnectionString();
if (connection is null)
{
return;
}
try
{
await using (var hero = OpenHero(connection, inLibrary: false))
{
await hero.Database.MigrateAsync();
}
await using (var media = OpenMedia(connection))
{
await media.Database.MigrateAsync();
}
await using var db = OpenHero(connection, inLibrary: true);
var photo = Photo(new string('b', 64));
db.HeroImages.Add(photo);
var content = new HeroContent { Id = Guid.NewGuid(), Title = "Titel", UpdatedAt = DateTimeOffset.UtcNow, LastModifiedByUserId = Guid.NewGuid() };
content.Slides.Add(new HeroSlide { Id = Guid.NewGuid(), HeroId = content.Id, ImageId = photo.Id, Alt = "een", SortOrder = 0 });
db.Heroes.Add(content);
await db.SaveChangesAsync();
// The row is in the library's table, as module hero, with the hero's focus in the library's column names.
await using (var check = OpenMedia(connection))
{
var row = await check.MediaAssets.AsNoTracking().SingleAsync(a => a.Id == photo.Id);
(row.Module, row.DefaultFocusX, row.DefaultFocusY, row.DefaultZoom, row.WideFocusX, row.SetHash, row.OriginalFileName).Should().Be(("hero", 0.4, 0.6, 1.5, 0.1, "0123456789abcdef", "zomer.jpg"));
row.Variants.Should().Contain("wide-480.webp");
}
// The slide points at a photo that is only in MediaAssets: the foreign key to HeroImages is gone, and it reads back.
(await db.HeroImages.CountAsync()).Should().Be(1);
var hero2 = await new Repositories.HeroRepository(OpenHero(connection, inLibrary: true)).GetReadOnlyAsync(HeroLanguages.Default, CancellationToken.None);
hero2!.Slides.Single().Image!.Id.Should().Be(photo.Id);
// A change goes to the library row; HeroImages is brought up to date by the mirror and nothing else wrote to it.
photo.FocusX = 0.9;
await db.SaveChangesAsync();
await new HeroLegacyImageMirror(db, NullLogger<HeroLegacyImageMirror>.Instance).SyncAsync(CancellationToken.None);
await using (var legacy = OpenHero(connection, inLibrary: true))
{
var copy = await legacy.LegacyHeroImages.AsNoTracking().SingleAsync();
(copy.Id, copy.FocusX, copy.SetHash).Should().Be((photo.Id, 0.9, "0123456789abcdef"));
copy.Variants.Should().ContainSingle();
}
await HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None);
// The same data, read the old way (the switch off), is complete because of the mirror: switching back loses nothing.
await using var off = OpenHero(connection, inLibrary: false);
var old = await off.HeroImages.AsNoTracking().SingleAsync();
(old.Id, old.FocusX, old.OriginalFileName).Should().Be((photo.Id, 0.9, "zomer.jpg"));
}
finally
{
await DropAsync(connection);
}
}
[Fact]
public async Task The_foreign_key_migration_can_be_undone()
{
var connection = ConnectionString();
if (connection is null)
{
return;
}
try
{
await using var db = OpenHero(connection, inLibrary: false);
await db.Database.MigrateAsync();
(await Count(db, "FK_HeroSlides_HeroImages_ImageId")).Should().Be(0);
var migrator = db.GetService<IMigrator>();
await migrator.MigrateAsync("20261117071410_RemoveHeroScheduleAndSwitch");
(await Count(db, "FK_HeroSlides_HeroImages_ImageId")).Should().NotBe(0);
}
finally
{
await DropAsync(connection);
}
}
private static async Task<int> Count(HeroDbContext db, string constraint)
{
await using var command = db.Database.GetDbConnection().CreateCommand();
if (command.Connection!.State != System.Data.ConnectionState.Open)
{
await command.Connection.OpenAsync();
}
command.CommandText = $"SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME = '{constraint}'";
return Convert.ToInt32(await command.ExecuteScalarAsync());
}
private static async Task DropAsync(string? connectionString)
{
if (connectionString is null)
{
return;
}
var builder = new MySqlConnectionStringBuilder(connectionString);
var database = builder.Database;
builder.Database = string.Empty;
await using var connection = new MySqlConnection(builder.ConnectionString);
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = $"DROP DATABASE IF EXISTS `{database}`";
await command.ExecuteNonQueryAsync();
}
}
@@ -0,0 +1,111 @@
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
using Xunit;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>Phase 3b: the hero's photos live in the library's table (rows of module "hero"), and HeroImages stays a complete copy.</summary>
public class HeroPhotosInLibraryTests
{
private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides);
[Fact]
public void The_model_maps_the_photos_to_the_library_table_only_when_the_switch_is_on()
{
using var off = new HeroTestRig();
using var on = new HeroTestRig(photosInLibrary: true);
off.Db.Model.FindEntityType(typeof(HeroImage))!.GetTableName().Should().Be("HeroImages");
off.Db.Model.FindEntityType(typeof(LegacyHeroImage)).Should().BeNull();
var photo = on.Db.Model.FindEntityType(typeof(HeroImage))!;
photo.GetTableName().Should().Be("MediaAssets");
photo.FindProperty(nameof(HeroImage.FocusX))!.GetColumnName().Should().Be("DefaultFocusX");
photo.FindProperty(nameof(HeroImage.Zoom))!.GetColumnName().Should().Be("DefaultZoom");
photo.FindProperty(HeroDbContext.ModuleColumn).Should().NotBeNull();
on.Db.Model.FindEntityType(typeof(LegacyHeroImage))!.GetTableName().Should().Be("HeroImages");
}
[Fact]
public async Task Everything_the_hero_does_with_a_photo_works_and_HeroImages_stays_a_complete_copy()
{
using var rig = new HeroTestRig(photosInLibrary: true);
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
var saved = await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, id, "een"), new HeroSlideRequest(null, id, "twee")), HeroTestRig.Admin, CancellationToken.None);
await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.3, 0.4, 1.4), new FocusDto(0.2, 0.2, 1.1), null), HeroTestRig.Admin, CancellationToken.None);
await rig.Images.SetSlideFocusAsync(Guid.Parse(saved.Slides[1].Id), new HeroFocusRequest(new FocusDto(0.9, 0.9, 1.2)), HeroTestRig.Admin, CancellationToken.None);
await rig.RenderPending();
var other = await rig.Upload(HeroTestRig.Photo(seed: 5));
await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(other.Id), HeroTestRig.Admin, CancellationToken.None);
var library = await rig.Db.HeroImages.AsNoTracking().OrderBy(i => i.CreatedAt).ToListAsync();
var legacy = await rig.Db.LegacyHeroImages.AsNoTracking().OrderBy(i => i.CreatedAt).ToListAsync();
library.Should().HaveCount(2);
legacy.Should().HaveCount(2);
library.Select(i => i.Id).Should().Equal(legacy.Select(i => i.Id));
foreach (var (l, c) in library.Zip(legacy))
{
(l.OriginalSha256, l.SetHash, l.FocusX, l.WideFocusX, l.DeletedAt, l.OriginalFileName).Should().Be((c.OriginalSha256, c.SetHash, c.FocusX, c.WideFocusX, c.DeletedAt, c.OriginalFileName));
l.Variants.Should().Equal(c.Variants);
}
library.Single(i => i.Id == id).Variants.Should().NotBeEmpty();
var publicHero = (await rig.Public.GetActiveAsync(CancellationToken.None))!;
publicHero.Slides.Should().HaveCount(2);
publicHero.Slides[0].Image.Focus.Should().Be(new FocusDto(0.3, 0.4, 1.4));
}
[Fact]
public async Task The_clean_up_removes_a_trashed_photo_from_both_tables()
{
using var rig = new HeroTestRig(photosInLibrary: true);
var image = await rig.Upload(HeroTestRig.Photo());
await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(image.Id), HeroTestRig.Admin, CancellationToken.None);
rig.Time.Now = rig.Time.Now.AddDays(60);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
(await rig.Db.HeroImages.CountAsync()).Should().Be(0);
(await rig.Db.LegacyHeroImages.CountAsync()).Should().Be(0);
}
[Fact]
public async Task The_start_up_check_passes_for_a_complete_copy_and_refuses_when_the_library_misses_a_photo_or_its_variants()
{
using var rig = new HeroTestRig(photosInLibrary: true);
var image = await rig.Upload(HeroTestRig.Photo());
await HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
// A photo that exists in HeroImages only (the copy was not run).
rig.Db.LegacyHeroImages.Add(new LegacyHeroImage { Id = Guid.NewGuid(), OriginalSha256 = new string('a', 64), OriginalKey = "originals/a.jpg", SourceFormat = "jpg", SetHash = "0123456789abcdef", CreatedAt = DateTimeOffset.UtcNow });
await rig.Db.SaveChangesAsync();
var missing = () => HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
(await missing.Should().ThrowAsync<InvalidOperationException>()).Which.Message.Should().Contain("is not in the library").And.Contain("copy");
// And a photo whose variants differ (a copy made before the variants were stored).
rig.Db.LegacyHeroImages.RemoveRange(rig.Db.LegacyHeroImages.Where(i => i.OriginalSha256 == new string('a', 64)));
var photo = await rig.Db.HeroImages.SingleAsync(i => i.Id == Guid.Parse(image.Id));
photo.Variants = [];
await rig.Db.SaveChangesAsync();
var differs = () => HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
(await differs.Should().ThrowAsync<InvalidOperationException>()).Which.Message.Should().Contain("differs from the library");
}
[Fact]
public async Task With_the_switch_off_nothing_is_mirrored_and_the_check_does_nothing()
{
using var rig = new HeroTestRig();
await rig.Upload(HeroTestRig.Photo());
await HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
await rig.LegacyMirror.SyncAsync(CancellationToken.None);
(await rig.Db.HeroImages.CountAsync()).Should().Be(1);
}
}
@@ -39,12 +39,15 @@ public sealed class HeroTestRig : IDisposable
public HeroMediaCleanup Cleanup { get; }
public HeroOptions HeroOptions { get; } = new();
public HeroTestRig(int maxPublishedSlides = 50, SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null)
public IHeroLegacyImageMirror LegacyMirror { get; }
public HeroTestRig(int maxPublishedSlides = 50, SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, bool photosInLibrary = false)
{
HeroOptions.MaxPublishedSlides = maxPublishedSlides;
Media = new MediaOptions { Directory = Directory, BusyWaitSeconds = 0 };
var mediaOptions = Options.Create(Media);
Db = new HeroDbContext(new DbContextOptionsBuilder<HeroDbContext>().UseInMemoryDatabase("hero-" + Guid.NewGuid()).Options);
Db = new HeroDbContext(new DbContextOptionsBuilder<HeroDbContext>().UseInMemoryDatabase("hero-" + Guid.NewGuid()).ReplaceService<Microsoft.EntityFrameworkCore.Infrastructure.IModelCacheKeyFactory, HeroModelCacheKeyFactory>().Options, new HeroStorage(photosInLibrary));
LegacyMirror = new HeroLegacyImageMirror(Db, NullLogger<HeroLegacyImageMirror>.Instance);
Repository = new HeroRepository(Db);
Store = new FileSystemMediaStore(mediaOptions);
Processor = new MagickImageProcessor(mediaOptions, NullLogger<MagickImageProcessor>.Instance);
@@ -52,10 +55,10 @@ public sealed class HeroTestRig : IDisposable
Status.SetDirectory(true);
var gate = new MediaProcessingGate(mediaOptions);
var guard = new MediaUploadGuard(Processor, gate, Status, mediaOptions);
Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger<HeroImageService>.Instance, Time, IndexCache, Signal, librarySync);
Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger<HeroService>.Instance, Time, IndexCache, null, librarySync);
Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger<HeroImageService>.Instance, Time, IndexCache, Signal, librarySync, LegacyMirror);
Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger<HeroService>.Instance, Time, IndexCache, null, librarySync, LegacyMirror);
Public = new PublicHeroService(Repository, Options.Create(HeroOptions), Time);
Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger<HeroMediaCleanup>.Instance, Time, librarySync);
Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger<HeroMediaCleanup>.Instance, Time, librarySync, LegacyMirror);
}
/// <summary>A photo-like fixture: the left half red, the right half blue, with a camera tag.</summary>
@@ -33,6 +33,7 @@
<ItemGroup>
<ProjectReference Include="..\SlpModularCms.Modules.Hero\SlpModularCms.Modules.Hero.csproj" />
<ProjectReference Include="..\SlpModularCms.Modules.Media\SlpModularCms.Modules.Media.csproj" />
</ItemGroup>
</Project>
@@ -112,6 +112,37 @@ public class HeroImage
};
}
/// <summary>
/// The same photo as <see cref="HeroImage"/>, always stored in <c>HeroImages</c>. Only used while the photos live in the
/// library (phase 3b): then <c>HeroImages</c> is kept up to date from the library rows so the switch can be turned back.
/// </summary>
public class LegacyHeroImage
{
public Guid Id { get; set; }
public string OriginalSha256 { get; set; } = string.Empty;
public string OriginalKey { get; set; } = string.Empty;
public string SourceFormat { get; set; } = string.Empty;
public int Width { get; set; }
public int Height { get; set; }
public long Bytes { get; set; }
public double FocusX { get; set; } = 0.5;
public double FocusY { get; set; } = 0.5;
public double Zoom { get; set; } = 1.0;
public double? WideFocusX { get; set; }
public double? WideFocusY { get; set; }
public double? WideZoom { get; set; }
public double? TallFocusX { get; set; }
public double? TallFocusY { get; set; }
public double? TallZoom { get; set; }
public string SetHash { get; set; } = string.Empty;
public List<HeroVariant> Variants { get; set; } = [];
public DateTimeOffset CreatedAt { get; set; }
public Guid CreatedByUserId { get; set; }
public string? OriginalFileName { get; set; }
public DateTimeOffset? LastUsedAt { get; set; }
public DateTimeOffset? DeletedAt { get; set; }
}
/// <summary>One file of the current set: the ratio, its pixel size, the type and the file name inside the set.</summary>
public record HeroVariant(string Ratio, int Width, int Height, string Type, string FileName, long Bytes);
@@ -2,11 +2,18 @@ using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Data;
public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(options)
public class HeroDbContext(DbContextOptions<HeroDbContext> options, HeroStorage? storage = null) : DbContext(options)
{
/// <summary>True while the photos live in the library's table (phase 3b); see <see cref="HeroStorage"/>.</summary>
public bool PhotosInLibrary { get; } = storage?.PhotosInLibrary ?? false;
/// <summary>The photos as they are always kept in <c>HeroImages</c> (only mapped while <see cref="PhotosInLibrary"/> is on).</summary>
public DbSet<LegacyHeroImage> LegacyHeroImages => Set<LegacyHeroImage>();
public DbSet<HeroContent> Heroes => Set<HeroContent>();
public DbSet<HeroSlide> HeroSlides => Set<HeroSlide>();
public DbSet<HeroImage> HeroImages => Set<HeroImage>();
@@ -86,6 +93,8 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(
v => v.ToList()));
});
ConfigureStorage(modelBuilder);
modelBuilder.Entity<HeroRetiredFile>(entity =>
{
entity.ToTable("HeroRetiredFiles");
@@ -95,4 +104,65 @@ public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(
entity.Property(e => e.RetiredAt).IsRequired();
});
}
private void ConfigureStorage(ModelBuilder modelBuilder)
{
if (!PhotosInLibrary)
{
modelBuilder.Ignore<LegacyHeroImage>();
return;
}
// The hero's photos are rows of the library's table, module "hero". The library owns that table and its migrations.
modelBuilder.Entity<HeroImage>(entity =>
{
entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations());
entity.Property<string>(ModuleColumn).HasMaxLength(20).IsRequired().HasDefaultValue(HeroMapping.Area);
entity.Property(e => e.FocusX).HasColumnName("DefaultFocusX");
entity.Property(e => e.FocusY).HasColumnName("DefaultFocusY");
entity.Property(e => e.Zoom).HasColumnName("DefaultZoom");
entity.HasQueryFilter(e => EF.Property<string>(e, ModuleColumn) == HeroMapping.Area);
});
// And HeroImages stays a complete copy, so switching back loses nothing.
modelBuilder.Entity<LegacyHeroImage>(entity =>
{
entity.ToTable("HeroImages");
entity.HasKey(e => e.Id);
entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16);
entity.Property(e => e.Variants)
.HasConversion(
variants => JsonSerializer.Serialize(variants, (JsonSerializerOptions?)null),
json => JsonSerializer.Deserialize<List<HeroVariant>>(json, (JsonSerializerOptions?)null) ?? new List<HeroVariant>())
.HasColumnType("longtext");
});
}
public const string ModuleColumn = "Module";
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
MarkNewPhotosAsHero();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
MarkNewPhotosAsHero();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
/// <summary>A photo the hero adds to the library's table belongs to the hero module.</summary>
private void MarkNewPhotosAsHero()
{
if (!PhotosInLibrary)
{
return;
}
foreach (var entry in ChangeTracker.Entries<HeroImage>().Where(e => e.State == EntityState.Added))
{
entry.Property(ModuleColumn).CurrentValue = HeroMapping.Area;
}
}
}
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace SlpModularCms.Modules.Hero.Data;
/// <summary>
/// Where the hero's photos are stored (media library phase 3b). Off (default): the hero's own table <c>HeroImages</c>. On
/// (<c>Media__Library__HeroPhotosInLibrary=true</c>): the library's <c>MediaAssets</c> table (rows of module <c>hero</c>), with
/// <c>HeroImages</c> kept up to date as a safety net so the switch can go back. The choice is made when the application starts.
/// </summary>
public sealed record HeroStorage(bool PhotosInLibrary);
/// <summary>EF builds the model once per key: the two storage modes have different models.</summary>
public sealed class HeroModelCacheKeyFactory : IModelCacheKeyFactory
{
public object Create(DbContext context, bool designTime)
=> (context.GetType(), context is HeroDbContext { PhotosInLibrary: true }, designTime);
}
@@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -26,12 +28,21 @@ public class HeroModule : IModule
public void RegisterServices(IServiceCollection services)
{
// Where the photos are stored (media library phase 3b): off = HeroImages, on = the library's MediaAssets table.
services.AddSingleton(serviceProvider => new HeroStorage(serviceProvider.GetRequiredService<IConfiguration>().GetValue<bool>("Media:Library:HeroPhotosInLibrary")));
services.AddDbContext<HeroDbContext>((serviceProvider, options) =>
{
var config = serviceProvider.GetRequiredService<IConfiguration>();
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
options.ReplaceService<IModelCacheKeyFactory, HeroModelCacheKeyFactory>();
if (serviceProvider.GetRequiredService<HeroStorage>().PhotosInLibrary)
{
// The model differs from the migrations snapshot on purpose (the photos' table belongs to the library).
options.ConfigureWarnings(warnings => warnings.Ignore(RelationalEventId.PendingModelChangesWarning));
}
});
services.AddScoped<IHeroLegacyImageMirror, HeroLegacyImageMirror>();
services.AddCmsTextsFrom(typeof(HeroModule).Assembly);
services.AddOptions<HeroOptions>().Configure<IConfiguration>((options, configuration) =>
@@ -63,5 +74,6 @@ public class HeroModule : IModule
using var scope = app.ApplicationServices.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
db.Database.Migrate();
HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None).GetAwaiter().GetResult();
}
}
@@ -0,0 +1,291 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SlpModularCms.Modules.Hero.Data;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
[DbContext(typeof(HeroDbContext))]
[Migration("20261117230241_DropSlideImageForeignKey")]
partial class DropSlideImageForeignKey
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Button1Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button1Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Button2Label")
.HasMaxLength(100)
.HasColumnType("varchar(100)");
b.Property<string>("Button2Target")
.HasMaxLength(500)
.HasColumnType("varchar(500)");
b.Property<string>("Display")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("varchar(20)");
b.Property<string>("Language")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<Guid>("LastModifiedByUserId")
.HasColumnType("char(36)");
b.Property<string>("Lead")
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<string>("Tagline")
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(1000)
.HasColumnType("varchar(1000)");
b.Property<DateTimeOffset>("UpdatedAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.HasIndex("Language")
.IsUnique();
b.ToTable("Heroes", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<long>("Bytes")
.HasColumnType("bigint");
b.Property<DateTimeOffset>("CreatedAt")
.HasColumnType("datetime");
b.Property<Guid>("CreatedByUserId")
.HasColumnType("char(36)");
b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("datetime");
b.Property<double>("FocusX")
.HasColumnType("double");
b.Property<double>("FocusY")
.HasColumnType("double");
b.Property<int>("Height")
.HasColumnType("int");
b.Property<DateTimeOffset?>("LastUsedAt")
.HasColumnType("datetime");
b.Property<string>("OriginalFileName")
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("OriginalKey")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("OriginalSha256")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("varchar(64)");
b.Property<string>("SetHash")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<string>("SourceFormat")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<double?>("TallFocusX")
.HasColumnType("double");
b.Property<double?>("TallFocusY")
.HasColumnType("double");
b.Property<double?>("TallZoom")
.HasColumnType("double");
b.Property<string>("Variants")
.IsRequired()
.HasColumnType("longtext");
b.Property<double?>("WideFocusX")
.HasColumnType("double");
b.Property<double?>("WideFocusY")
.HasColumnType("double");
b.Property<double?>("WideZoom")
.HasColumnType("double");
b.Property<int>("Width")
.HasColumnType("int");
b.Property<double>("Zoom")
.HasColumnType("double");
b.HasKey("Id");
b.HasIndex("OriginalSha256")
.IsUnique();
b.ToTable("HeroImages", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Key")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("varchar(200)");
b.Property<string>("Kind")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("varchar(10)");
b.Property<DateTimeOffset>("RetiredAt")
.HasColumnType("datetime");
b.HasKey("Id");
b.ToTable("HeroRetiredFiles", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<string>("Alt")
.IsRequired()
.HasMaxLength(600)
.HasColumnType("varchar(600)");
b.Property<bool>("Decorative")
.HasColumnType("tinyint(1)");
b.Property<double?>("FocusX")
.HasColumnType("double");
b.Property<double?>("FocusY")
.HasColumnType("double");
b.Property<Guid>("HeroId")
.HasColumnType("char(36)");
b.Property<Guid>("ImageId")
.HasColumnType("char(36)");
b.Property<bool>("IsActive")
.HasColumnType("tinyint(1)");
b.Property<string>("SetHash")
.HasMaxLength(16)
.HasColumnType("varchar(16)");
b.Property<int>("SortOrder")
.HasColumnType("int");
b.Property<double?>("TallFocusX")
.HasColumnType("double");
b.Property<double?>("TallFocusY")
.HasColumnType("double");
b.Property<double?>("TallZoom")
.HasColumnType("double");
b.Property<string>("Variants")
.HasColumnType("longtext");
b.Property<double?>("WideFocusX")
.HasColumnType("double");
b.Property<double?>("WideFocusY")
.HasColumnType("double");
b.Property<double?>("WideZoom")
.HasColumnType("double");
b.Property<double?>("Zoom")
.HasColumnType("double");
b.HasKey("Id");
b.HasIndex("ImageId");
b.HasIndex("HeroId", "SortOrder");
b.ToTable("HeroSlides", (string)null);
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b =>
{
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", null)
.WithMany("Slides")
.HasForeignKey("HeroId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SlpModularCms.Modules.Hero.Data.Entities.HeroImage", "Image")
.WithMany()
.HasForeignKey("ImageId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Image");
});
modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroContent", b =>
{
b.Navigation("Slides");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SlpModularCms.Modules.Hero.Migrations
{
/// <inheritdoc />
public partial class DropSlideImageForeignKey : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// Phase 3b of the media library: a slide's photo can live in the library's MediaAssets instead of HeroImages, so the
// database no longer ties a slide to HeroImages. The service layer already checks that the photo exists and that a
// photo in use is never removed. The model snapshot is deliberately unchanged (the relation stays in the EF model, only
// the constraint in the database goes), so this migration is reversible and later migrations see no difference.
migrationBuilder.DropForeignKey(name: "FK_HeroSlides_HeroImages_ImageId", table: "HeroSlides");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddForeignKey(
name: "FK_HeroSlides_HeroImages_ImageId",
table: "HeroSlides",
column: "ImageId",
principalTable: "HeroImages",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}
@@ -114,10 +114,22 @@ public class HeroImageService(
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
HeroRenderSignal? signal = null,
IMediaLibrarySync? librarySync = null) : IHeroImageService
IMediaLibrarySync? librarySync = null,
IHeroLegacyImageMirror? legacyMirror = null) : IHeroImageService
{
/// <summary>Mirrors the hero into the media library's tables (write-through, off unless switched on). Never throws.</summary>
private Task SyncLibraryAsync() => librarySync?.SyncAsync(HeroMapping.Area, CancellationToken.None) ?? Task.CompletedTask;
private async Task SyncLibraryAsync()
{
if (legacyMirror is not null)
{
await legacyMirror.SyncAsync(CancellationToken.None);
}
if (librarySync is not null)
{
await librarySync.SyncAsync(HeroMapping.Area, CancellationToken.None);
}
}
private readonly TimeProvider _time = time ?? TimeProvider.System;
@@ -0,0 +1,107 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
namespace SlpModularCms.Modules.Hero.Services;
/// <summary>Keeps <c>HeroImages</c> a complete copy of the hero's photos while they live in the library's table (phase 3b).</summary>
public interface IHeroLegacyImageMirror
{
/// <summary>Does nothing unless the photos live in the library. Never throws: the library rows are the truth then.</summary>
Task SyncAsync(CancellationToken cancellationToken);
}
public sealed class HeroLegacyImageMirror(HeroDbContext db, ILogger<HeroLegacyImageMirror> logger) : IHeroLegacyImageMirror
{
public async Task SyncAsync(CancellationToken cancellationToken)
{
if (!db.PhotosInLibrary)
{
return;
}
try
{
var library = await db.HeroImages.AsNoTracking().ToListAsync(cancellationToken);
var legacy = (await db.LegacyHeroImages.ToListAsync(cancellationToken)).ToDictionary(i => i.Id);
foreach (var photo in library)
{
if (!legacy.Remove(photo.Id, out var row))
{
row = new LegacyHeroImage { Id = photo.Id };
db.LegacyHeroImages.Add(row);
}
Copy(photo, row);
}
db.LegacyHeroImages.RemoveRange(legacy.Values);
await db.SaveChangesAsync(cancellationToken);
}
catch (Exception exception) when (exception is not OperationCanceledException)
{
logger.LogError(exception, "Keeping HeroImages up to date with the library photos failed; the library rows are the truth, the next change repairs it.");
}
}
internal static void Copy(HeroImage from, LegacyHeroImage to)
{
to.OriginalSha256 = from.OriginalSha256;
to.OriginalKey = from.OriginalKey;
to.SourceFormat = from.SourceFormat;
to.Width = from.Width;
to.Height = from.Height;
to.Bytes = from.Bytes;
(to.FocusX, to.FocusY, to.Zoom) = (from.FocusX, from.FocusY, from.Zoom);
(to.WideFocusX, to.WideFocusY, to.WideZoom) = (from.WideFocusX, from.WideFocusY, from.WideZoom);
(to.TallFocusX, to.TallFocusY, to.TallZoom) = (from.TallFocusX, from.TallFocusY, from.TallZoom);
to.SetHash = from.SetHash;
to.Variants = [.. from.Variants];
to.CreatedAt = from.CreatedAt;
to.CreatedByUserId = from.CreatedByUserId;
to.OriginalFileName = from.OriginalFileName;
to.LastUsedAt = from.LastUsedAt;
to.DeletedAt = from.DeletedAt;
}
}
/// <summary>
/// Run at start-up when the photos are to live in the library: the library rows must already be a complete copy of
/// <c>HeroImages</c> (same ids, hashes, sets and variants), otherwise the application refuses to start with a clear message
/// instead of showing a hero with missing photos. Fix: run the verified copy first (Media__Library__CopyOnStartup) and check
/// the library check, then switch the storage.
/// </summary>
public static class HeroPhotoStorageGuard
{
public static async Task VerifyAsync(HeroDbContext db, CancellationToken cancellationToken)
{
if (!db.PhotosInLibrary)
{
return;
}
var library = (await db.HeroImages.AsNoTracking().ToListAsync(cancellationToken)).ToDictionary(i => i.Id);
var problems = new List<string>();
foreach (var legacy in await db.LegacyHeroImages.AsNoTracking().ToListAsync(cancellationToken))
{
if (!library.TryGetValue(legacy.Id, out var photo))
{
problems.Add($"photo {legacy.Id} is not in the library");
}
else if (photo.OriginalSha256 != legacy.OriginalSha256 || photo.SetHash != legacy.SetHash || HeroMediaMigrationSource.Json(photo.Variants) != HeroMediaMigrationSource.Json(legacy.Variants))
{
problems.Add($"photo {legacy.Id} differs from the library (hash, set or variants)");
}
}
if (problems.Count > 0)
{
throw new InvalidOperationException(
"Media__Library__HeroPhotosInLibrary is on, but the library does not hold every hero photo as HeroImages does: "
+ string.Join("; ", problems.Take(5))
+ (problems.Count > 5 ? $" (and {problems.Count - 5} more)" : string.Empty)
+ ". Run the media library copy first (Media__Library__CopyOnStartup=true), check it with the library check, then switch the storage.");
}
}
}
@@ -19,7 +19,8 @@ public class HeroMediaCleanup(
IOptions<MediaOptions> options,
ILogger<HeroMediaCleanup> logger,
TimeProvider? time = null,
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null)
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
IHeroLegacyImageMirror? legacyMirror = null)
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
@@ -53,6 +54,11 @@ public class HeroMediaCleanup(
}
await repository.SaveChangesAsync(cancellationToken);
if (legacyMirror is not null && (unattached.Count > 0 || trashedGone))
{
await legacyMirror.SyncAsync(CancellationToken.None);
}
if (librarySync is not null && (unattached.Count > 0 || trashedGone))
{
await librarySync.SyncAsync(HeroMapping.Area, CancellationToken.None);
@@ -31,7 +31,8 @@ public class HeroService(
TimeProvider? time = null,
IIndexHtmlCache? indexCache = null,
ITextCatalog? texts = null,
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null) : IHeroService
SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null,
IHeroLegacyImageMirror? legacyMirror = null) : IHeroService
{
private readonly TimeProvider _time = time ?? TimeProvider.System;
private readonly ITextCatalog _texts = texts ?? TextCatalog.Default;
@@ -156,6 +157,11 @@ public class HeroService(
indexCache?.Invalidate();
imageService.NotifyPending();
if (legacyMirror is not null)
{
await legacyMirror.SyncAsync(CancellationToken.None);
}
if (librarySync is not null)
{
await librarySync.SyncAsync(HeroMapping.Area, CancellationToken.None);