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:
@@ -83,3 +83,4 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu
|
||||
| 52 | **Phase 3a: the library tables hold what the hero needs to publish** (2026-11-18). | Additive migration `AddHeroPhotoColumns` of the Media module: `MediaAssets.SetHash`, `MediaAssets.Variants` (the module's own JSON), the wide/tall overrides of the photo's crop, and `MediaUses.Variants`. The copy, the write-through and the verification fill and compare them; the read check shows a difference in them. Nothing reads them to publish yet (that is 3b). | Store the variants relationally: more tables for no gain while only the hero publishes from here. | `MediaEntities`, `MediaLibraryCopier`, `HeroMediaMigrationSource` |
|
||||
| 53 | **Phase 3b maps the hero's photos onto the library table, behind a switch, with HeroImages kept as a copy** (2026-11-18). | `Media__Library__HeroPhotosInLibrary` (default off, test only via the workflow input `media_library_hero_photos`). On: the hero's photo entity is the library's `MediaAssets` rows of module `hero`; `HeroImages` is mirrored after every change so the switch can go back; the application refuses to start if the library lacks a photo or its set/variants; the foreign key slide -> `HeroImages` is dropped by a reversible Hero migration. No data is deleted in 3b; dropping `HeroImages` is step 3c. | Move the data in one go and drop the old table at once: cannot be switched back. | `HeroDbContext.ConfigureStorage`, `HeroLegacyImageMirror`, `HeroPhotoStorageGuard`, migration `DropSlideImageForeignKey` |
|
||||
| 54 | **3c is built on its own branch and drops `HeroImages` behind a guard** (2026-11-18). | `feature/media-library-phase3c`: the photos always live in `MediaAssets`; the switches, copier, write-through, reader, mirror, read-check endpoint and the "Controle bibliotheek" panel are gone (Bryan: keep the panel until 3b is verified, then remove it in 3c); migration `DropHeroImagesTable` is destructive and only runs when the library holds every photo (guard before the migrations), `Down` gives the empty table. Not deployed before 3b is verified on test, a fresh restore point exists and SLS-Main has been told the exact migration. `MediaUses` stays unused in the database. | Keep the migration aids for another release: more code to carry for no use. | `HeroImagesDropGuard`, `DropHeroImagesTable`, `HeroDbContext` |
|
||||
| 55 | **Production gets 3c in one release: the start-up copies `HeroImages` into the library right before the drop** (2026-11-18). | `HeroImagesDropGuard.PrepareAndVerify` applies the hero's migrations up to the drop, asks the Media module (`IMediaLibrarySchema`) to migrate its tables, copies the photos into `MediaAssets` in one transaction (update differing, insert missing, never delete) and then runs the strict check; a refusal throws `ModuleStartupException`, which the module orchestrator now rethrows so the application really does not start (before, every module error was logged and ignored). `MediaUses` are not copied (nothing reads them after 3c). | Staged road on production (phase 2 with the copy, 3b switch, then 3c): more releases and a period with the switches; the copier code would have to stay on master. | `HeroImagesDropGuard`, `IMediaLibrarySchema`, `ModuleOrchestrator`, `production-rollout.md` |
|
||||
|
||||
+24
-19
@@ -1,29 +1,34 @@
|
||||
# Media library: order of rollout to production
|
||||
# Media library: rollout to production in one release
|
||||
|
||||
Status (2026-11-18): NOT executed. Production only on an explicit go from Bryan. No `release/*` branches. Written on `feature/media-library-phase3c`.
|
||||
Status (2026-11-18): built on `feature/media-library-oneshot`, NOT executed, NOT on production. Production only on an explicit go from Bryan. No `release/*` branches.
|
||||
|
||||
## Why the order matters
|
||||
## What changed compared with the staged plan
|
||||
|
||||
The test sites went phase 2 -> copy -> 3b switch -> 3c. Production must take the same road, because the 3c release has **no copier and no switches** any more: `HeroImagesDropGuard` refuses to start the application (nothing is dropped) unless every photo of `HeroImages` is in `MediaAssets` (module `hero`) with the same hash, set and variants. A direct jump from today's production to 3c therefore fails at start-up, by design, unless `HeroImages` is empty.
|
||||
Master (phases 2 and 3c) has no copier and no switches any more, so production (which still has the hero's photos only in `HeroImages`) could not go straight to it: the drop guard would refuse. The staged road (phase 2 with the copy, then 3b, then 3c) is no longer needed, because the start-up now does the copy itself, once, right before the drop:
|
||||
|
||||
## Steps (each a separate release; every step: notice to SLS-Main, then Bryan's go)
|
||||
1. `HeroModule.UseModule` calls `HeroImagesDropGuard.PrepareAndVerify`. If the migration `DropHeroImagesTable` is pending:
|
||||
2. the hero's own migrations up to the one before the drop are applied (so `HeroImages` has its final columns);
|
||||
3. if `HeroImages` holds photos: the library tables are brought to the current schema through `IMediaLibrarySchema` (implemented by the Media module, which runs its migrations; this makes the order in which modules start irrelevant) and the photos are copied into `MediaAssets` (module `hero`): rows that differ are updated, missing rows inserted, nothing is deleted. HeroImages is the truth at that moment. The copy is one transaction;
|
||||
4. `Verify` (unchanged, strict) compares hash, set hash and variants of every photo; on any difference it throws `ModuleStartupException`;
|
||||
5. only then `Migrate()` runs the drop.
|
||||
|
||||
| # | Release | Settings | Check before the next step | Rollback |
|
||||
|---|---|---|---|---|
|
||||
| 0 | Fresh restore point of production (database dump + media directory listing, the Pi-Main script) and row counts: `HeroImages`, `HeroSlides`, `MediaAssets`, 10 sample public addresses | none | Restore point exists and its path is written down | n/a |
|
||||
| 1 | Phase 2 (master after PR #27: write-through, crop per slide, read switch, 3a columns, `DropSlideImageForeignKey`) | `Media__Library__CopyOnStartup=true`, `Media__Library__DualWrite=true`; the other switches off | `GET media-library/read-check` says `matches: true` (hash, set and variants); public addresses unchanged | Switches off; `Down` of the additive migrations; last resort the restore point |
|
||||
| 2 | Same build, setting only | `Media__Library__HeroPhotosInLibrary=true` (the guard refuses to start if the copy is incomplete; log says why) | Hero admin: upload, crop per slide, save, trash, restore; public site; read-check still matches. Leave it running for a quiet period (test had days) | Switch off: `HeroImages` is still a complete copy |
|
||||
| 3 | Phase 3c (PR after #27): `DropHeroImagesTable` + guard; switches and copier removed | none needed (settings of step 1 and 2 are ignored; remove them from the environment file afterwards) | Journal: no guard error, `Applying migration ..._DropHeroImagesTable`, no failure; `HeroImages` gone; hero admin and site work | Only from the restore point of step 0 (take a NEW one right before this step): restore the dump, deploy the previous release. `Down` only recreates an empty table |
|
||||
Uses (`MediaUses`) are not copied: after 3c nothing reads them (the library screen asks the modules' providers).
|
||||
|
||||
Step 3 needs its own fresh restore point and Bryan's go on the exact migration (`DROP TABLE HeroImages`), as on test.
|
||||
**Guard now really stops the application.** `ModuleOrchestrator.UseModules` used to log every module exception and continue, so a refusing guard would have left the application running without the hero's migration (data safe, but a half-state). `ModuleStartupException` is now rethrown, so the host does not start; the log has a Critical line with the reason.
|
||||
|
||||
## Things that differ from test
|
||||
## The steps on production
|
||||
|
||||
- Production has real photos on both sites; test SlpSoftware had none (guard passes on an empty `HeroImages`). Count the photos per site in step 0 and compare them with `MediaAssets` after step 1.
|
||||
- The workflow inputs for the switches exist only on the phase 2 build. After 3c they are gone, so steps 1 and 2 must be run while phase 2 is the latest release (do not merge PR 3c to master before step 2 is done on production).
|
||||
- The new default `Media__AutoCleanupUnattached=false` applies from step 1: unused uploads are kept until someone deletes them in the library.
|
||||
- The media directory is never changed destructively by these steps (retired sets stay for the retention time), so a database restore does not need a media restore.
|
||||
| # | Step | Check | Rollback |
|
||||
|---|---|---|---|
|
||||
| 0 | Fresh restore point of production (database dump; the Pi-Main script) and counts per site: `HeroImages` rows, `HeroSlides`, 10 sample public addresses | Path of the restore point written down | n/a |
|
||||
| 1 | Deploy this release (the one with the one-shot copy). Needs Bryan's go on the exact migration (`DROP TABLE HeroImages` after the copy) | Journal: `Applying migration ..._AddHeroPhotoColumns` (Media), the Hero migrations, `..._DropHeroImagesTable`, no Critical line; `/health` 200; `MediaAssets` has the same number of hero rows as `HeroImages` had; hero admin and public site; the 10 addresses unchanged | Restore point of step 0 + deploy the previous release. `Down` of the drop only recreates an empty table |
|
||||
|
||||
## Order of merging versus deploying to test
|
||||
The restore point is the only way back after the drop. It must be taken immediately before step 1.
|
||||
|
||||
Master deploys to test. After PR #27 (phase 2) is merged, test runs phase 2 code on databases where `HeroImages` is already dropped by 3c. With `Media__Library__HeroPhotosInLibrary=true` still in the environment file of test the photos are read from the library, but the legacy mirror (`HeroLegacyImageMirror`) then writes to `HeroImages`, which does not exist, so saving in the hero admin on test can fail until 3c is merged too. Merge #27 and the 3c PR directly after each other (or accept a short window on test where saving the hero fails).
|
||||
## Things to know
|
||||
|
||||
- Both sites are handled the same way; a site with an empty `HeroImages` just migrates.
|
||||
- If the guard refuses (Critical line, application not running): nothing was dropped (the copy is a transaction and the drop never ran). Fix the cause named in the message and start again; or deploy the previous release, which still reads `HeroImages`... only if the Hero migrations before the drop are compatible: those migrations (crop columns on slides, foreign key removal) are additive and the previous release ignores them, except that the foreign key `FK_HeroSlides_HeroImages_ImageId` is gone (the old release works without it).
|
||||
- Media files are never touched by any of this.
|
||||
- `Media__AutoCleanupUnattached` is off by default (unused uploads stay until deleted in the library).
|
||||
- Test already ran the staged road; the one-shot path is tested on MariaDB in `HeroPhotosInLibraryTableTests.One_release_from_the_old_state_...` (hero tables just before phase 2, photos incl. a trashed one, no library tables) and should be tried on test once before production (restore the test database to a state before phase 2 if Bryan wants a rehearsal).
|
||||
|
||||
@@ -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