Productie in een keer: de start kopieert HeroImages in de bibliotheek vlak voor de drop
Continuous Integration / config-asf (pull_request) Successful in 6s
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 12s
Continuous Integration / frontend-prepare (pull_request) Skipped
Continuous Integration / frontend-build (pull_request) Skipped
Continuous Integration / frontend-test (pull_request) Skipped
Continuous Integration / frontend-lint (pull_request) Skipped
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m12s
Continuous Integration / compile-sites (pull_request) Successful in 2m9s
Continuous Integration / backend-build (pull_request) Successful in 2m12s
Continuous Integration / backend-test (pull_request) Successful in 2m41s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Continuous Integration / config-asf (pull_request) Successful in 6s
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 12s
Continuous Integration / frontend-prepare (pull_request) Skipped
Continuous Integration / frontend-build (pull_request) Skipped
Continuous Integration / frontend-test (pull_request) Skipped
Continuous Integration / frontend-lint (pull_request) Skipped
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m12s
Continuous Integration / compile-sites (pull_request) Successful in 2m9s
Continuous Integration / backend-build (pull_request) Successful in 2m12s
Continuous Integration / backend-test (pull_request) Successful in 2m41s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Een weigerende guard stopt de applicatie nu echt (ModuleStartupException wordt door de orchestrator doorgegooid). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -38,6 +38,33 @@ public class ModuleOrchestratorTests
|
||||
module.Received(1).UseModule(app);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UseModules_Rethrows_AModuleStartupException_SoTheApplicationDoesNotStart()
|
||||
{
|
||||
var module = Substitute.For<IModule>();
|
||||
module.When(m => m.UseModule(Arg.Any<IApplicationBuilder>())).Do(_ => throw new ModuleStartupException("guard refuses"));
|
||||
var app = Substitute.For<IApplicationBuilder>();
|
||||
|
||||
InjectModules(_orchestrator, [module]);
|
||||
var act = () => _orchestrator.UseModules(app);
|
||||
|
||||
act.Should().Throw<ModuleStartupException>().WithMessage("guard refuses");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UseModules_KeepsGoing_WhenAModuleFailsWithAnOrdinaryException()
|
||||
{
|
||||
var failing = Substitute.For<IModule>();
|
||||
failing.When(m => m.UseModule(Arg.Any<IApplicationBuilder>())).Do(_ => throw new InvalidOperationException("boom"));
|
||||
var other = Substitute.For<IModule>();
|
||||
var app = Substitute.For<IApplicationBuilder>();
|
||||
|
||||
InjectModules(_orchestrator, [failing, other]);
|
||||
_orchestrator.UseModules(app);
|
||||
|
||||
other.Received(1).UseModule(app);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RegisterModuleServices_DoesNotThrow_WhenModuleListIsEmpty()
|
||||
{
|
||||
|
||||
@@ -104,6 +104,11 @@ public class ModuleOrchestrator
|
||||
module.UseModule(app);
|
||||
_logger.LogInformation("Module geactiveerd in pipeline: {ModuleName}", module.Name);
|
||||
}
|
||||
catch (ModuleStartupException ex)
|
||||
{
|
||||
_logger.LogCritical(ex, "Module {ModuleName} stopt het opstarten van de applicatie", module.Name);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Fout bij het activeren van module {ModuleName} in de pipeline", module.Name);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace SlpModularCms.Core.Media.Library;
|
||||
|
||||
/// <summary>
|
||||
/// Lets a module bring the library's own tables (<c>MediaAssets</c>) to the current schema before it needs them, whatever the order
|
||||
/// in which the modules start. Implemented by the Media module; a host without it simply has no library tables.
|
||||
/// </summary>
|
||||
public interface IMediaLibrarySchema
|
||||
{
|
||||
/// <summary>Applies the library's pending migrations (idempotent).</summary>
|
||||
void EnsureReady();
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SlpModularCms.Core.Modules;
|
||||
|
||||
/// <summary>
|
||||
/// Thrown by a module when it must stop the application from starting (for example a guard before a destructive migration).
|
||||
/// The module orchestrator rethrows it; every other exception of a module is logged and the application continues.
|
||||
/// </summary>
|
||||
public class ModuleStartupException(string message) : InvalidOperationException(message);
|
||||
@@ -167,6 +167,65 @@ public class HeroPhotosInLibraryTableTests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task One_release_from_the_old_state_copies_the_photos_into_the_library_and_then_drops_HeroImages()
|
||||
{
|
||||
var connection = ConnectionString();
|
||||
if (connection is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// The state of an environment that never ran phase 2: the hero's tables up to just before the drop, with photos in
|
||||
// HeroImages (and a trashed one), and no library tables at all.
|
||||
await using var db = OpenHero(connection);
|
||||
var migrations = db.Database.GetMigrations().ToList();
|
||||
var dropIndex = migrations.FindIndex(m => m.EndsWith("_DropHeroImagesTable", StringComparison.Ordinal));
|
||||
var firstPhase2 = migrations.FindIndex(m => m.EndsWith("_AddSlideCrop", StringComparison.Ordinal));
|
||||
firstPhase2.Should().BeGreaterThan(0);
|
||||
await db.GetService<IMigrator>().MigrateAsync(migrations[firstPhase2 - 1]);
|
||||
|
||||
var photo = Photo(new string('d', 64));
|
||||
var trashed = Photo(new string('e', 64), "fedcba9876543210");
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"INSERT INTO `HeroImages` (Id, OriginalSha256, OriginalKey, SourceFormat, Width, Height, Bytes, FocusX, FocusY, Zoom, SetHash, Variants, CreatedAt, CreatedByUserId, OriginalFileName, DeletedAt) VALUES "
|
||||
+ $"('{photo.Id}', '{photo.OriginalSha256}', '{photo.OriginalKey}', 'jpg', 3000, 2000, 2000000, 0.4, 0.6, 1.5, '0123456789abcdef', '[]', '2026-11-17 12:00:00', '{photo.CreatedByUserId}', 'zomer.jpg', NULL), "
|
||||
+ $"('{trashed.Id}', '{trashed.OriginalSha256}', '{trashed.OriginalKey}', 'jpg', 10, 10, 10, 0.5, 0.5, 1, 'fedcba9876543210', '[]', '2026-11-17 12:00:00', '{trashed.CreatedByUserId}', NULL, '2026-11-17 13:00:00')");
|
||||
(await TableExists(db, "MediaAssets")).Should().Be(0);
|
||||
|
||||
// Without a way to prepare the library tables the guard refuses and nothing is dropped.
|
||||
var refused = () => HeroImagesDropGuard.PrepareAndVerify(db, library: null);
|
||||
refused.Should().Throw<SlpModularCms.Core.Modules.ModuleStartupException>();
|
||||
(await TableExists(db, "HeroImages")).Should().Be(1);
|
||||
|
||||
// With the library schema: the library tables are created, the photos are copied, and the drop goes through.
|
||||
await using (var media = OpenMedia(connection))
|
||||
{
|
||||
HeroImagesDropGuard.PrepareAndVerify(db, new SlpModularCms.Modules.Media.Services.MediaLibrarySchema(media));
|
||||
}
|
||||
|
||||
await db.Database.MigrateAsync();
|
||||
(await TableExists(db, "HeroImages")).Should().Be(0);
|
||||
|
||||
await using var check = OpenMedia(connection);
|
||||
var rows = await check.MediaAssets.AsNoTracking().OrderBy(a => a.OriginalSha256).ToListAsync();
|
||||
rows.Should().HaveCount(2);
|
||||
rows[0].Module.Should().Be("hero");
|
||||
(rows[0].Id, rows[0].DefaultFocusX, rows[0].DefaultFocusY, rows[0].DefaultZoom, rows[0].SetHash, rows[0].OriginalFileName, rows[0].DeletedAt)
|
||||
.Should().Be((photo.Id, 0.4, 0.6, 1.5, "0123456789abcdef", "zomer.jpg", null));
|
||||
rows[1].DeletedAt.Should().NotBeNull("a photo in the trash stays in the trash");
|
||||
|
||||
// Running it again on the finished database does nothing.
|
||||
HeroImagesDropGuard.PrepareAndVerify(db, null);
|
||||
}
|
||||
finally
|
||||
{
|
||||
await DropAsync(connection);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task DropAsync(string? connectionString)
|
||||
{
|
||||
if (connectionString is null)
|
||||
|
||||
@@ -1,16 +1,109 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Core.Modules;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Runs before the migrations at start-up. The migration <c>DropHeroImagesTable</c> destroys <c>HeroImages</c>; this refuses to let
|
||||
/// it run (it throws, the application does not start, nothing is dropped) unless every photo of <c>HeroImages</c> is in the
|
||||
/// library's <c>MediaAssets</c> (module hero) with the same hash, set and variants. A database where <c>HeroImages</c> is empty
|
||||
/// (a new one) passes without the library table having to exist yet.
|
||||
/// Runs before the migrations at start-up. The migration <c>DropHeroImagesTable</c> destroys <c>HeroImages</c>; <see cref="PrepareAndVerify"/>
|
||||
/// first copies whatever photos the library does not have yet (one release from an older state to this one), then <see cref="Verify"/>
|
||||
/// refuses to let the migration run (it throws a <see cref="ModuleStartupException"/>: the application does not start, nothing is
|
||||
/// dropped) unless every photo of <c>HeroImages</c> is in the library's <c>MediaAssets</c> (module hero) with the same hash, set and
|
||||
/// variants. A database where <c>HeroImages</c> is empty (a new one) passes without the library table having to exist yet.
|
||||
/// </summary>
|
||||
public static class HeroImagesDropGuard
|
||||
{
|
||||
private const string DropMigrationSuffix = "_DropHeroImagesTable";
|
||||
|
||||
/// <summary>
|
||||
/// The one-release path: bring the hero's own tables to the state just before the drop, bring the library's tables to the
|
||||
/// current schema, copy the photos of <c>HeroImages</c> the library lacks or holds differently (HeroImages is the truth until
|
||||
/// this moment), then verify. Does nothing when the drop is not pending.
|
||||
/// </summary>
|
||||
public static void PrepareAndVerify(HeroDbContext db, IMediaLibrarySchema? library)
|
||||
{
|
||||
if (!db.Database.CanConnect())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var pending = db.Database.GetPendingMigrations().ToList();
|
||||
if (!pending.Any(id => id.EndsWith(DropMigrationSuffix, StringComparison.Ordinal)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// HeroImages must have its final columns before it is read: apply everything before the drop first.
|
||||
var all = db.Database.GetMigrations().ToList();
|
||||
var dropIndex = all.FindIndex(id => id.EndsWith(DropMigrationSuffix, StringComparison.Ordinal));
|
||||
if (dropIndex > 0 && pending.Contains(all[dropIndex - 1]))
|
||||
{
|
||||
db.GetService<IMigrator>().Migrate(all[dropIndex - 1]);
|
||||
}
|
||||
|
||||
if (Exists(db, "HeroImages") && Scalar(db, "SELECT COUNT(*) FROM `HeroImages`") > 0)
|
||||
{
|
||||
library?.EnsureReady();
|
||||
if (Exists(db, "MediaAssets"))
|
||||
{
|
||||
CopyIntoLibrary(db);
|
||||
}
|
||||
}
|
||||
|
||||
Verify(db);
|
||||
}
|
||||
|
||||
/// <summary>Makes <c>MediaAssets</c> (module hero) equal to <c>HeroImages</c>: updates the rows that differ, inserts the missing ones. Never deletes.</summary>
|
||||
private static void CopyIntoLibrary(HeroDbContext db)
|
||||
{
|
||||
var connection = db.Database.GetDbConnection();
|
||||
var wasOpen = connection.State == System.Data.ConnectionState.Open;
|
||||
if (!wasOpen)
|
||||
{
|
||||
connection.Open();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var transaction = connection.BeginTransaction();
|
||||
Execute(connection, transaction, @"UPDATE `MediaAssets` m JOIN `HeroImages` h ON m.`Id` = h.`Id` AND m.`Module` = 'hero'
|
||||
SET m.`OriginalSha256` = h.`OriginalSha256`, m.`OriginalKey` = h.`OriginalKey`, m.`SourceFormat` = h.`SourceFormat`,
|
||||
m.`Width` = h.`Width`, m.`Height` = h.`Height`, m.`Bytes` = h.`Bytes`,
|
||||
m.`DefaultFocusX` = h.`FocusX`, m.`DefaultFocusY` = h.`FocusY`, m.`DefaultZoom` = h.`Zoom`,
|
||||
m.`WideFocusX` = h.`WideFocusX`, m.`WideFocusY` = h.`WideFocusY`, m.`WideZoom` = h.`WideZoom`,
|
||||
m.`TallFocusX` = h.`TallFocusX`, m.`TallFocusY` = h.`TallFocusY`, m.`TallZoom` = h.`TallZoom`,
|
||||
m.`SetHash` = h.`SetHash`, m.`Variants` = h.`Variants`,
|
||||
m.`CreatedAt` = h.`CreatedAt`, m.`CreatedByUserId` = h.`CreatedByUserId`, m.`OriginalFileName` = h.`OriginalFileName`,
|
||||
m.`LastUsedAt` = h.`LastUsedAt`, m.`DeletedAt` = h.`DeletedAt`");
|
||||
Execute(connection, transaction, @"INSERT INTO `MediaAssets` (`Id`, `Module`, `OriginalSha256`, `OriginalKey`, `SourceFormat`, `Width`, `Height`, `Bytes`,
|
||||
`DefaultFocusX`, `DefaultFocusY`, `DefaultZoom`, `WideFocusX`, `WideFocusY`, `WideZoom`, `TallFocusX`, `TallFocusY`, `TallZoom`,
|
||||
`SetHash`, `Variants`, `CreatedAt`, `CreatedByUserId`, `OriginalFileName`, `LastUsedAt`, `DeletedAt`)
|
||||
SELECT h.`Id`, 'hero', h.`OriginalSha256`, h.`OriginalKey`, h.`SourceFormat`, h.`Width`, h.`Height`, h.`Bytes`,
|
||||
h.`FocusX`, h.`FocusY`, h.`Zoom`, h.`WideFocusX`, h.`WideFocusY`, h.`WideZoom`, h.`TallFocusX`, h.`TallFocusY`, h.`TallZoom`,
|
||||
h.`SetHash`, h.`Variants`, h.`CreatedAt`, h.`CreatedByUserId`, h.`OriginalFileName`, h.`LastUsedAt`, h.`DeletedAt`
|
||||
FROM `HeroImages` h
|
||||
WHERE NOT EXISTS (SELECT 1 FROM `MediaAssets` m WHERE m.`Id` = h.`Id`)");
|
||||
transaction.Commit();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!wasOpen)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void Execute(System.Data.Common.DbConnection connection, System.Data.Common.DbTransaction transaction, string sql)
|
||||
{
|
||||
using var command = connection.CreateCommand();
|
||||
command.Transaction = transaction;
|
||||
command.CommandText = sql;
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public static void Verify(HeroDbContext db)
|
||||
{
|
||||
// A database that does not exist yet (a new environment) has nothing to protect; Migrate creates it.
|
||||
@@ -31,7 +124,7 @@ public static class HeroImagesDropGuard
|
||||
|
||||
if (!Exists(db, "MediaAssets"))
|
||||
{
|
||||
throw new InvalidOperationException("HeroImages holds photos but the library table MediaAssets does not exist: the hero's photos are not in the library, so HeroImages is not dropped. Run the media library copy first.");
|
||||
throw new ModuleStartupException("HeroImages holds photos but the library table MediaAssets does not exist: the hero's photos are not in the library, so HeroImages is not dropped. The copy into the library did not run or failed.");
|
||||
}
|
||||
|
||||
var missing = Scalar(db, @"SELECT COUNT(*) FROM `HeroImages` h
|
||||
@@ -42,7 +135,7 @@ public static class HeroImagesDropGuard
|
||||
OR NOT (m.`Variants` <=> h.`Variants`)");
|
||||
if (missing > 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{missing} photo(s) of HeroImages are not in the library table MediaAssets (or differ in hash, set or variants), so HeroImages is not dropped. Run the media library copy first and check it.");
|
||||
throw new ModuleStartupException($"{missing} photo(s) of HeroImages are not in the library table MediaAssets (or differ in hash, set or variants), so HeroImages is not dropped. The copy into the library did not complete.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ public class HeroModule : IModule
|
||||
{
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<HeroDbContext>();
|
||||
HeroImagesDropGuard.Verify(db);
|
||||
HeroImagesDropGuard.PrepareAndVerify(db, scope.ServiceProvider.GetService<IMediaLibrarySchema>());
|
||||
db.Database.Migrate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Core.Modules;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
using SlpModularCms.Modules.Media.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Media;
|
||||
|
||||
@@ -29,6 +31,8 @@ public class MediaModule : IModule
|
||||
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
|
||||
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
||||
});
|
||||
|
||||
services.AddScoped<IMediaLibrarySchema, MediaLibrarySchema>();
|
||||
}
|
||||
|
||||
public void UseModule(IApplicationBuilder app)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Core.Media.Library;
|
||||
using SlpModularCms.Modules.Media.Data;
|
||||
|
||||
namespace SlpModularCms.Modules.Media.Services;
|
||||
|
||||
/// <summary>Brings the library's tables to the current schema on request (see <see cref="IMediaLibrarySchema"/>).</summary>
|
||||
public sealed class MediaLibrarySchema(MediaDbContext db) : IMediaLibrarySchema
|
||||
{
|
||||
public void EnsureReady() => db.Database.Migrate();
|
||||
}
|
||||
Reference in New Issue
Block a user