diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index dc34038..f894507 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -15,26 +15,6 @@ on: description: 'Deploy ASF test (Andrea Slop Fotografie)' type: boolean default: false - media_library_copy: - description: 'Media library copy on the TEST deploys (Media__Library__CopyOnStartup in shared/env): unchanged, on or off' - type: choice - options: [unchanged, 'on', 'off'] - default: unchanged - media_library_dual_write: - description: 'Media library write-through on the TEST deploys (Media__Library__DualWrite in shared/env): unchanged, on or off' - type: choice - options: [unchanged, 'on', 'off'] - default: unchanged - media_library_read: - description: 'Media library read switch on the TEST deploys (Media__Library__ReadFromLibrary in shared/env): unchanged, off, compare or on' - type: choice - options: [unchanged, 'off', compare, 'on'] - default: unchanged - media_library_hero_photos: - description: 'Hero photos in the media library on the TEST deploys (Media__Library__HeroPhotosInLibrary in shared/env): unchanged, on or off. Needs the copy and the library check first.' - type: choice - options: [unchanged, 'on', 'off'] - default: unchanged pull_request: types: [opened, synchronize, reopened] push: @@ -575,10 +555,6 @@ jobs: service_name: ${{ vars.SERVICE_NAME_TEST }} health_check_url: ${{ vars.HEALTH_CHECK_URL_TEST }} run_db_backup: false - media_library_copy: ${{ github.event.inputs.media_library_copy || 'unchanged' }} - media_library_dual_write: ${{ github.event.inputs.media_library_dual_write || 'unchanged' }} - media_library_read: ${{ github.event.inputs.media_library_read || 'unchanged' }} - media_library_hero_photos: ${{ github.event.inputs.media_library_hero_photos || 'unchanged' }} # Production deploy ONLY on an explicit workflow_dispatch run with deploy_sls_production checked, and # only when that run is on a release/* branch (same rule as ASF) — never reachable from a plain push @@ -706,10 +682,6 @@ jobs: service_name: ${{ vars.SERVICE_NAME_ASF_TEST }} health_check_url: ${{ vars.HEALTH_CHECK_URL_ASF_TEST }} run_db_backup: false - media_library_copy: ${{ github.event.inputs.media_library_copy || 'unchanged' }} - media_library_dual_write: ${{ github.event.inputs.media_library_dual_write || 'unchanged' }} - media_library_read: ${{ github.event.inputs.media_library_read || 'unchanged' }} - media_library_hero_photos: ${{ github.event.inputs.media_library_hero_photos || 'unchanged' }} # --- Smoke tests of the deployed compositions (anonymous requests, see scripts/smoke-site.sh) --- # The CI tests the master CMS; this is the check that the composition of each site that was just deployed to test diff --git a/.gitea/workflows/deploy-scp.yaml b/.gitea/workflows/deploy-scp.yaml index 4207513..7bb47a1 100644 --- a/.gitea/workflows/deploy-scp.yaml +++ b/.gitea/workflows/deploy-scp.yaml @@ -37,26 +37,6 @@ on: required: false type: string default: scp - media_library_copy: - description: 'unchanged | on | off: sets Media__Library__CopyOnStartup in shared/env before the restart. TEST only, never production.' - required: false - type: string - default: unchanged - media_library_dual_write: - description: 'unchanged | on | off: sets Media__Library__DualWrite (the write-through of phase 2) in shared/env before the restart. TEST only, never production.' - required: false - type: string - default: unchanged - media_library_read: - description: 'unchanged | off | compare | on: sets Media__Library__ReadFromLibrary (the read switch of phase 2) in shared/env before the restart. TEST only, never production.' - required: false - type: string - default: unchanged - media_library_hero_photos: - description: 'unchanged | on | off: sets Media__Library__HeroPhotosInLibrary (phase 3b: the hero photos live in the library table) in shared/env before the restart. TEST only, never production.' - required: false - type: string - default: unchanged jobs: deploy: @@ -231,119 +211,6 @@ jobs: ln -sf \"../../shared/modules/\$name\" \"$RELEASE_DIR/\$name\"; \ done" - # The media library copy switch (migration plan of the media library, decision 13): the application reads - # Media__Library__CopyOnStartup from the host environment file shared/env (D-16). The deploy account can edit that file, - # nobody else needs a shell on the host. Only this ONE key is touched (nothing else of the file is read or printed, the - # file holds secrets), only on a test environment, and only with a value that is exactly on, off or unchanged. - # `on` makes the next start copy the photos into the library's tables and verify the copy (log line - # "Media library copy verified: N photos and M uses"); `off` removes the key again (the default is off). - - name: Set the media library copy switch (test only) - if: inputs.media_library_copy != 'unchanged' - env: - SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} - SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} - SSH_PORT: ${{ secrets.PI_MAIN_PORT }} - SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} - run: | - MODE="${{ inputs.media_library_copy }}" - if [ "${{ inputs.environment }}" != "test" ]; then - echo "The media library copy switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 - exit 1 - fi - if [ "$MODE" != "on" ] && [ "$MODE" != "off" ]; then - echo "media_library_copy must be unchanged, on or off (got: $MODE)." >&2 - exit 1 - fi - ENV_FILE="${{ inputs.deploy_path }}/shared/env" - if [ "$MODE" = "on" ]; then - rssh "touch $ENV_FILE && sed -i '/^Media__Library__CopyOnStartup=/d' $ENV_FILE && echo 'Media__Library__CopyOnStartup=true' >> $ENV_FILE" - else - rssh "touch $ENV_FILE && sed -i '/^Media__Library__CopyOnStartup=/d' $ENV_FILE" - fi - echo "Media__Library__CopyOnStartup is now: $(rssh "grep '^Media__Library__CopyOnStartup=' $ENV_FILE || echo '(not set = off)'")" - - # The write-through switch of phase 2 (same rules as the copy switch above: only this one key of shared/env, only on test, - # only on, off or unchanged). `on` makes every change of the hero also land in the library's tables. - - name: Set the media library write-through switch (test only) - if: inputs.media_library_dual_write != 'unchanged' - env: - SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} - SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} - SSH_PORT: ${{ secrets.PI_MAIN_PORT }} - SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} - run: | - MODE="${{ inputs.media_library_dual_write }}" - if [ "${{ inputs.environment }}" != "test" ]; then - echo "The media library write-through switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 - exit 1 - fi - if [ "$MODE" != "on" ] && [ "$MODE" != "off" ]; then - echo "media_library_dual_write must be unchanged, on or off (got: $MODE)." >&2 - exit 1 - fi - ENV_FILE="${{ inputs.deploy_path }}/shared/env" - if [ "$MODE" = "on" ]; then - rssh "touch $ENV_FILE && sed -i '/^Media__Library__DualWrite=/d' $ENV_FILE && echo 'Media__Library__DualWrite=true' >> $ENV_FILE" - else - rssh "touch $ENV_FILE && sed -i '/^Media__Library__DualWrite=/d' $ENV_FILE" - fi - echo "Media__Library__DualWrite is now: $(rssh "grep '^Media__Library__DualWrite=' $ENV_FILE || echo '(not set = off)'")" - - # The read switch of phase 2 (same rules as the switches above): compare = the library tables are read too and differences are - # logged and shown by GET media-library/read-check; on = the library screen reads the library tables (needs the write-through). - - name: Set the media library read switch (test only) - if: inputs.media_library_read != 'unchanged' - env: - SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} - SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} - SSH_PORT: ${{ secrets.PI_MAIN_PORT }} - SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} - run: | - MODE="${{ inputs.media_library_read }}" - if [ "${{ inputs.environment }}" != "test" ]; then - echo "The media library read switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 - exit 1 - fi - if [ "$MODE" != "on" ] && [ "$MODE" != "off" ] && [ "$MODE" != "compare" ]; then - echo "media_library_read must be unchanged, off, compare or on (got: $MODE)." >&2 - exit 1 - fi - ENV_FILE="${{ inputs.deploy_path }}/shared/env" - if [ "$MODE" = "off" ]; then - rssh "touch $ENV_FILE && sed -i '/^Media__Library__ReadFromLibrary=/d' $ENV_FILE" - else - rssh "touch $ENV_FILE && sed -i '/^Media__Library__ReadFromLibrary=/d' $ENV_FILE && echo 'Media__Library__ReadFromLibrary=$MODE' >> $ENV_FILE" - fi - echo "Media__Library__ReadFromLibrary is now: $(rssh "grep '^Media__Library__ReadFromLibrary=' $ENV_FILE || echo '(not set = off)'")" - - # The hero-photos storage switch of phase 3b (same rules as the switches above). `on` makes the hero keep its photos in the - # library's MediaAssets table (HeroImages stays a copy so it can go back); the application REFUSES to start when the library - # does not yet hold every hero photo, so run the copy first and check it with the library check. - - name: Set the media library hero photos switch (test only) - if: inputs.media_library_hero_photos != 'unchanged' - env: - SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} - SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} - SSH_PORT: ${{ secrets.PI_MAIN_PORT }} - SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} - run: | - MODE="${{ inputs.media_library_hero_photos }}" - if [ "${{ inputs.environment }}" != "test" ]; then - echo "The hero photos switch is for the test environment only (environment: ${{ inputs.environment }})." >&2 - exit 1 - fi - if [ "$MODE" != "on" ] && [ "$MODE" != "off" ]; then - echo "media_library_hero_photos must be unchanged, on or off (got: $MODE)." >&2 - exit 1 - fi - ENV_FILE="${{ inputs.deploy_path }}/shared/env" - if [ "$MODE" = "on" ]; then - rssh "touch $ENV_FILE && sed -i '/^Media__Library__HeroPhotosInLibrary=/d' $ENV_FILE && echo 'Media__Library__HeroPhotosInLibrary=true' >> $ENV_FILE" - else - rssh "touch $ENV_FILE && sed -i '/^Media__Library__HeroPhotosInLibrary=/d' $ENV_FILE" - fi - echo "Media__Library__HeroPhotosInLibrary is now: $(rssh "grep '^Media__Library__HeroPhotosInLibrary=' $ENV_FILE || echo '(not set = off)'")" - # Atomic release switch (FR-06, D-27): `ln -sfn` replaces the `current` symlink target in a # single filesystem operation, so there is no moment where `current` points at a half-written # directory. The process is then restarted so it picks up the new assemblies — a running .NET @@ -360,20 +227,6 @@ jobs: "ln -sfn $RELEASE_DIR ${{ inputs.deploy_path }}/current && \ systemctl --user restart ${{ inputs.service_name }}" - # After a restart with the copy on, show the verification line of the copy in this run's log (the journal of the user - # service), so nobody needs a shell on the host to see "Media library copy verified: N photos and M uses". - - name: Report the media library copy (test only) - if: inputs.media_library_copy == 'on' - env: - SSH_USER: ${{ secrets.PI_MAIN_USERNAME }} - SSH_PASS: ${{ secrets.PI_MAIN_PASSWORD }} - SSH_PORT: ${{ secrets.PI_MAIN_PORT }} - SSH_HOST: ${{ secrets.PI_MAIN_ADDRESS }} - run: | - sleep 20 - rssh "journalctl --user -u ${{ inputs.service_name }} --since '-10 min' --no-pager 2>/dev/null | grep -E 'Media library copy|media library copy' | tail -5 || true" - echo "Look for 'Media library copy verified: N photos and M uses'. An error line means the copy was rolled back and nothing was copied." - # Verifies the restart actually produced a healthy process before this run is allowed to # report success. Retries absorb process-startup time, which is about 10 seconds normally but # 30 seconds or more when the host is busy (a migration step, two services restarting at once diff --git a/aidlc-docs/features/media-library/assumptions-and-open-questions.md b/aidlc-docs/features/media-library/assumptions-and-open-questions.md index 2ce64dd..16606ec 100644 --- a/aidlc-docs/features/media-library/assumptions-and-open-questions.md +++ b/aidlc-docs/features/media-library/assumptions-and-open-questions.md @@ -82,3 +82,4 @@ Kept while Bryan is away (2026-11-17): no questions to him, a most probable assu | 51 | **"Controle bibliotheek" panel in the library screen** (SLS-Main for Bryan, 2026-11-17). | A small panel under the storage meter, for administrators (the whole screen is admin only), asks `GET media-library/read-check` only when its button is clicked and says in plain words (nl/en) whether the library tables match each module or lists the differences, plus the state of the three switches (copy, write-through, reading off/compare/on; the response now carries `copyOnStartup` and `dualWrite`). It changes nothing. Always shown, not only on test. | Show it only when the read switch is not off: Bryan could then not check before switching. | `LibraryCheck.tsx`, `MediaLibraryController.ReadCheck`, `IMediaLibrarySwitchState` | | 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` | diff --git a/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md b/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md index 4958fcf..f01e1a7 100644 --- a/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md +++ b/aidlc-docs/features/media-library/inception/application-design/phase3-plan.md @@ -60,3 +60,23 @@ Public addresses (`/media/hero//...`), ids and hashes stay as they are 4. Rollback at any time: `media_library_hero_photos=off` (the application reads `HeroImages` again, a complete copy). To undo the foreign-key migration too: the `Down` of `DropSlideImageForeignKey`. Last resort: the restore point of the test sites. **Step 3c (not built):** drop `HeroImages` and the legacy mirror, remove the three media switches and the copier, make the library table the only home. Needs: Bryan's verification of 3b on test, a notice to SLS-Main with the exact migration, a fresh restore point; test only. + +## Step 3c as built (2026-11-18) - on its own branch, NOT deployed + +Branch `feature/media-library-phase3c` (from `feature/media-library-phase2`). It is a separate branch on purpose: dispatching `phase2` can never carry the destructive migration by accident. It is code, docs and tests only; nothing has been run on a test host. + +**What it removes** +- The three migration switches and everything behind them: `Media__Library__CopyOnStartup` and the copier, `DualWrite` and the write-through (`IMediaLibrarySync`), `ReadFromLibrary` and the reader/compare mode (`IMediaLibraryReader`), `HeroPhotosInLibrary` and the legacy mirror, the start-up guard and the storage switch (`HeroStorage`, model cache key). +- The "Controle bibliotheek" panel, the `read-check` endpoint and the migration source (`IMediaMigrationSource`, `HeroMediaMigrationSource`). Bryan: keep the panel until 3b is verified on test, then remove it together with the other migration aids (so: in this step). +- The four workflow inputs (`media_library_copy`, `_dual_write`, `_read`, `_hero_photos`) and their deploy steps. + +**What it makes permanent:** the hero's photos are rows of the library's `MediaAssets` (module `hero`, filter and `Module` column in the hero's model; the table and its migrations belong to the Media module). `MediaUses` is no longer written or read (it stays in the database, empty of meaning; dropping it is a later clean-up). + +**The destructive migration:** Hero `DropHeroImagesTable` (`DROP TABLE HeroImages`). Protected twice: +1. `HeroImagesDropGuard` runs before the migrations at start-up: when that migration is pending and `HeroImages` holds rows, it throws (the application does not start, nothing is dropped) unless every row is in `MediaAssets` (module hero) with the same hash, set and variants, or when `MediaAssets` does not exist. A new database (nothing in `HeroImages`) passes. +2. The restore point of the test sites, taken right before the deploy. +`Down` brings back the EMPTY table (data comes from the restore point). Tested on MariaDB: refused with a missing photo, refused with a different set, dropped when the library holds everything, Down recreates the empty table. + +**Order on test (needs Bryan's OK on 3b and a notice to SLS-Main with this exact migration):** (1) 3b verified: copy done, library check matches, `HeroPhotosInLibrary=on` verified by Bryan and live for a quiet period; (2) fresh restore point (Pi-Main script); (3) deploy `feature/media-library-phase3c` (the switches no longer exist; the variable `Media__Library__HeroPhotosInLibrary` in `shared/env` can stay, it is ignored); (4) check the hero admin, the public site and the library screen. Rollback: restore the restore point and deploy the previous release; `Down` only gives the empty table. + +Not tested here: the deploy itself, the media directory on the Pi, the production database (never). diff --git a/aidlc-docs/features/media-library/inception/application-design/production-rollout.md b/aidlc-docs/features/media-library/inception/application-design/production-rollout.md new file mode 100644 index 0000000..da9b6dd --- /dev/null +++ b/aidlc-docs/features/media-library/inception/application-design/production-rollout.md @@ -0,0 +1,29 @@ +# Media library: order of rollout to production + +Status (2026-11-18): NOT executed. Production only on an explicit go from Bryan. No `release/*` branches. Written on `feature/media-library-phase3c`. + +## Why the order matters + +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. + +## Steps (each a separate release; every step: notice to SLS-Main, then Bryan's go) + +| # | 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 | + +Step 3 needs its own fresh restore point and Bryan's go on the exact migration (`DROP TABLE HeroImages`), as on test. + +## Things that differ from test + +- 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. + +## Order of merging versus deploying to test + +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). diff --git a/frontend/src/features/media/components/LibraryCheck.tsx b/frontend/src/features/media/components/LibraryCheck.tsx deleted file mode 100644 index d058dff..0000000 --- a/frontend/src/features/media/components/LibraryCheck.tsx +++ /dev/null @@ -1,70 +0,0 @@ -import { useState } from 'react'; -import { useTranslation } from 'react-i18next'; -import { CheckCircle2, ListChecks, TriangleAlert } from 'lucide-react'; -import { Button } from '@/components/ui/button'; -import { useReadCheck } from '../services/hooks'; - -/** - * "Library check": a small panel for administrators that compares the library's own tables with the modules (the hero) in - * plain words and shows the state of the migration switches (copy, write-through, reading). It only asks the server when the - * button is clicked; it changes nothing. - */ -export function LibraryCheck() { - const { t } = useTranslation(); - const [asked, setAsked] = useState(false); - const check = useReadCheck(asked); - - return ( -
-
-

- {t('media.check.title')} -

- -
-

{t('media.check.hint')}

- {check.isError && ( -

- {t('media.check.error')} -

- )} - {check.data && ( -
-

- {t('media.check.switches', { - copy: check.data.copyOnStartup ? t('media.check.on') : t('media.check.off'), - write: check.data.dualWrite ? t('media.check.on') : t('media.check.off'), - read: t(`media.check.read.${check.data.mode}`), - })} -

- {check.data.modules.length === 0 &&

{t('media.check.noModules')}

} - {check.data.modules.map((module) => ( -
- {module.matches ? ( -

-

- ) : ( -
-

-

-
    - {module.differences.map((difference) => ( -
  • {difference}
  • - ))} -
-
- )} -
- ))} -
- )} -
- ); -} diff --git a/frontend/src/features/media/mocks/handlers.ts b/frontend/src/features/media/mocks/handlers.ts index f94a417..f56cd28 100644 --- a/frontend/src/features/media/mocks/handlers.ts +++ b/frontend/src/features/media/mocks/handlers.ts @@ -1,5 +1,5 @@ import { http, HttpResponse } from 'msw'; -import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types'; +import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPhotoDto, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from '@/features/media/services/types'; const daysAgo = (days: number) => new Date(Date.now() - days * 86_400_000).toISOString(); @@ -81,9 +81,6 @@ export const mediaHandlers = [ ...mediaTrashHandlers, http.get('*/media-library/photos', () => HttpResponse.json({ photos: seedPhotos(), modules: ['hero'] } satisfies MediaLibraryDto)), http.get('*/media-library/storage', () => HttpResponse.json(seedStorage())), - http.get('*/media-library/read-check', () => - HttpResponse.json({ mode: 'off', modules: [{ module: 'hero', mode: 'off', photos: 3, uses: 2, matches: true, differences: [] }], copyOnStartup: false, dualWrite: false } satisfies MediaReadCheckResponse), - ), http.get('*/media-library/cleanup-preview', ({ request }) => { const url = new URL(request.url); const days = Number(url.searchParams.get('days') ?? 7); diff --git a/frontend/src/features/media/pages/MediaLibraryPage.test.tsx b/frontend/src/features/media/pages/MediaLibraryPage.test.tsx index 9ae89d7..c1d008e 100644 --- a/frontend/src/features/media/pages/MediaLibraryPage.test.tsx +++ b/frontend/src/features/media/pages/MediaLibraryPage.test.tsx @@ -339,52 +339,6 @@ describe('MediaLibraryPage', () => { expect(within(dialog).getByTestId('cleanup-confirm')).toBeInTheDocument(); }); - it('checks the library against the hero in plain words, with the state of the switches, only when asked', async () => { - let calls = 0; - server.use( - http.get('*/media-library/read-check', () => { - calls += 1; - return HttpResponse.json({ mode: 'compare', modules: [{ module: 'hero', mode: 'compare', photos: 11, uses: 8, matches: true, differences: [] }], copyOnStartup: true, dualWrite: true }); - }), - ); - renderPage(); - await screen.findByTestId('photo-selection-list'); - expect(calls).toBe(0); - expect(screen.queryByTestId('library-check-result')).not.toBeInTheDocument(); - - await userEvent.click(screen.getByTestId('library-check-run')); - - expect(await screen.findByTestId('library-check-hero')).toHaveTextContent('De bibliotheek komt overeen met Hero (11 foto'); - expect(screen.getByTestId('library-check-switches')).toHaveTextContent('kopie bij het opstarten: aan; schrijven naar de bibliotheek: aan; lezen uit de bibliotheek: vergelijken (alleen loggen)'); - expect(calls).toBe(1); - }); - - it('lists the differences when the library does not match', async () => { - server.use( - http.get('*/media-library/read-check', () => - HttpResponse.json({ mode: 'off', modules: [{ module: 'hero', mode: 'off', photos: 3, uses: 2, matches: false, differences: ['photo 1 is missing in the library', 'use 2 differs'] }], copyOnStartup: false, dualWrite: false }), - ), - ); - renderPage(); - await screen.findByTestId('photo-selection-list'); - - await userEvent.click(screen.getByTestId('library-check-run')); - - expect(await screen.findByTestId('library-check-hero')).toHaveTextContent('De bibliotheek wijkt af van Hero: 2 verschillen.'); - expect(within(screen.getByTestId('library-check-hero-differences')).getAllByRole('listitem')).toHaveLength(2); - expect(screen.getByTestId('library-check-switches')).toHaveTextContent('kopie bij het opstarten: uit; schrijven naar de bibliotheek: uit; lezen uit de bibliotheek: uit'); - }); - - it('says so when the check cannot be run', async () => { - server.use(http.get('*/media-library/read-check', () => new HttpResponse(null, { status: 500 }))); - renderPage(); - await screen.findByTestId('photo-selection-list'); - - await userEvent.click(screen.getByTestId('library-check-run')); - - expect(await screen.findByTestId('library-check-error')).toHaveTextContent('De controle kon niet worden uitgevoerd.'); - }); - it('shows an error when the library cannot be loaded', async () => { server.use(http.get('*/media-library/photos', () => new HttpResponse(null, { status: 500 }))); renderPage(); diff --git a/frontend/src/features/media/pages/MediaLibraryPage.tsx b/frontend/src/features/media/pages/MediaLibraryPage.tsx index 6e53f98..5bf5208 100644 --- a/frontend/src/features/media/pages/MediaLibraryPage.tsx +++ b/frontend/src/features/media/pages/MediaLibraryPage.tsx @@ -7,7 +7,6 @@ import { CleanupDialog } from '../components/CleanupDialog'; import { DeleteDialog } from '../components/DeleteDialog'; import { PurgeDialog } from '../components/PurgeDialog'; import { PhotoSelectionList, type PhotoUse } from '../components/PhotoSelectionList'; -import { LibraryCheck } from '../components/LibraryCheck'; import { StorageMeter } from '../components/StorageMeter'; import { useLayoutPreference } from '../lib/layoutPreference'; import { emptySelection, prune, type Selection } from '../lib/selection'; @@ -91,7 +90,6 @@ export function MediaLibraryPage() { {storage.data && } -
{(['library', 'trash'] as const).map((value) => ( diff --git a/frontend/src/features/media/services/hooks.ts b/frontend/src/features/media/services/hooks.ts index d8f8cf4..055d3d3 100644 --- a/frontend/src/features/media/services/hooks.ts +++ b/frontend/src/features/media/services/hooks.ts @@ -1,6 +1,6 @@ import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api-client'; -import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaReadCheckResponse, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types'; +import type { MediaCleanupPreviewDto, MediaLibraryDto, MediaPurgeRequest, MediaStorageDto, MediaTrashRequest, MediaTrashResponse } from './types'; export const MEDIA_KEY = ['media-library']; @@ -22,17 +22,6 @@ export function useMediaStorage() { }); } -/** Compares the library tables with the modules and tells the state of the migration switches. Only asked for when the person clicks. */ -export function useReadCheck(enabled: boolean) { - return useQuery({ - queryKey: [...MEDIA_KEY, 'read-check'], - queryFn: () => api.get('/api/v1/media-library/read-check'), - enabled, - staleTime: 0, - gcTime: 0, - }); -} - /** What a clean-up with these settings would select. A preview: nothing is deleted. */ export function useCleanupPreview(days: number, onlyUnused: boolean, enabled: boolean) { return useQuery({ diff --git a/frontend/src/features/media/services/types.ts b/frontend/src/features/media/services/types.ts index cdef54e..12b9177 100644 --- a/frontend/src/features/media/services/types.ts +++ b/frontend/src/features/media/services/types.ts @@ -29,23 +29,6 @@ export interface MediaLibraryDto { export type MediaStorageLevel = 'ok' | 'warning' | 'full'; -export interface MediaReadCheckModuleDto { - module: string; - mode: string; - photos: number; - uses: number; - matches: boolean; - differences: string[]; -} - -/** The check of the library tables against the modules, plus the state of the migration switches. */ -export interface MediaReadCheckResponse { - mode: 'off' | 'compare' | 'on'; - modules: MediaReadCheckModuleDto[]; - copyOnStartup: boolean; - dualWrite: boolean; -} - export interface MediaStorageDto { usedBytes: number; /** 0 means no limit. */ diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index ec158d2..9ede23f 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -902,25 +902,6 @@ "irreversible": "This cannot be undone.", "partial_one": "{{count}} photo could not be deleted. The rest was deleted for good.", "partial_other": "{{count}} photos could not be deleted. The rest was deleted for good." - }, - "check": { - "title": "Library check", - "run": "Check", - "running": "Checking…", - "hint": "Compares the library’s tables with the hero and shows the state of the switches. Nothing is changed.", - "error": "The check could not be run.", - "switches": "State of the switches: copy at start-up: {{copy}}; writing to the library: {{write}}; reading from the library: {{read}}.", - "on": "on", - "off": "off", - "read": { - "off": "off", - "compare": "compare (log only)", - "on": "on" - }, - "noModules": "Nothing to check.", - "matches": "The library matches {{module}} ({{photos}} photos, {{uses}} places).", - "differs_one": "The library differs from {{module}}: {{count}} difference.", - "differs_other": "The library differs from {{module}}: {{count}} differences." } } } diff --git a/frontend/src/i18n/locales/nl/translation.json b/frontend/src/i18n/locales/nl/translation.json index bdaa0d1..3336a64 100644 --- a/frontend/src/i18n/locales/nl/translation.json +++ b/frontend/src/i18n/locales/nl/translation.json @@ -902,25 +902,6 @@ "irreversible": "Dit kan niet ongedaan worden gemaakt.", "partial_one": "{{count}} foto kon niet worden verwijderd. De rest is definitief verwijderd.", "partial_other": "{{count}} foto's konden niet worden verwijderd. De rest is definitief verwijderd." - }, - "check": { - "title": "Controle bibliotheek", - "run": "Controleren", - "running": "Bezig met controleren…", - "hint": "Vergelijkt de tabellen van de bibliotheek met de Hero en toont de stand van de schakelaars. Er wordt niets gewijzigd.", - "error": "De controle kon niet worden uitgevoerd.", - "switches": "Stand van de schakelaars: kopie bij het opstarten: {{copy}}; schrijven naar de bibliotheek: {{write}}; lezen uit de bibliotheek: {{read}}.", - "on": "aan", - "off": "uit", - "read": { - "off": "uit", - "compare": "vergelijken (alleen loggen)", - "on": "aan" - }, - "noModules": "Geen onderdelen om te controleren.", - "matches": "De bibliotheek komt overeen met {{module}} ({{photos}} foto’s, {{uses}} plekken).", - "differs_one": "De bibliotheek wijkt af van {{module}}: {{count}} verschil.", - "differs_other": "De bibliotheek wijkt af van {{module}}: {{count}} verschillen." } } } diff --git a/openapi/slpsoftware.v1.json b/openapi/slpsoftware.v1.json index 86f681a..a13b49e 100644 --- a/openapi/slpsoftware.v1.json +++ b/openapi/slpsoftware.v1.json @@ -1260,76 +1260,6 @@ }, "type": "object" }, - "MediaReadCheckDto": { - "properties": { - "differences": { - "items": { - "type": "string" - }, - "type": "array" - }, - "matches": { - "type": "boolean" - }, - "mode": { - "type": "string" - }, - "module": { - "type": "string" - }, - "photos": { - "format": "int32", - "pattern": "^-?(?:0|[1-9]\\d*)$", - "type": [ - "integer", - "string" - ] - }, - "uses": { - "format": "int32", - "pattern": "^-?(?:0|[1-9]\\d*)$", - "type": [ - "integer", - "string" - ] - } - }, - "required": [ - "module", - "mode", - "photos", - "uses", - "matches", - "differences" - ], - "type": "object" - }, - "MediaReadCheckResponse": { - "properties": { - "copyOnStartup": { - "default": false, - "type": "boolean" - }, - "dualWrite": { - "default": false, - "type": "boolean" - }, - "mode": { - "type": "string" - }, - "modules": { - "items": { - "$ref": "#/components/schemas/MediaReadCheckDto" - }, - "type": "array" - } - }, - "required": [ - "mode", - "modules" - ], - "type": "object" - }, "MediaRestoreRequest": { "properties": { "ids": { @@ -4134,35 +4064,6 @@ ] } }, - "/api/v1/media-library/read-check": { - "get": { - "responses": { - "200": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/MediaReadCheckResponse" - } - }, - "text/json": { - "schema": { - "$ref": "#/components/schemas/MediaReadCheckResponse" - } - }, - "text/plain": { - "schema": { - "$ref": "#/components/schemas/MediaReadCheckResponse" - } - } - }, - "description": "OK" - } - }, - "tags": [ - "MediaLibrary" - ] - } - }, "/api/v1/media-library/restore": { "post": { "requestBody": { diff --git a/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs b/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs index f26fd49..161cf50 100644 --- a/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs +++ b/src/SlpModularCms.Core.Tests/Media/MediaLibraryControllerTests.cs @@ -103,37 +103,4 @@ public class MediaLibraryControllerTests result.Should().Be(new MediaStorageDto(0, 0, "ok", 80)); } - - [Fact] - public async Task The_read_check_lists_what_each_module_found_and_the_mode() - { - var library = Substitute.For(); - library.Modules.Returns(["hero"]); - var verifier = Substitute.For(); - verifier.CheckAsync(Arg.Any()).Returns(new MediaReadCheck("hero", MediaReadMode.Compare, 3, 2, ["photo x is missing in the library"])); - var reader = Substitute.For(); - reader.Mode.Returns(MediaReadMode.Compare); - var switches = Substitute.For(); - switches.Current.Returns(new MediaLibrarySwitches(true, true, MediaReadMode.Compare)); - - var result = await new MediaLibraryController(library, new Clock(), null, [verifier], reader, switches).ReadCheck(CancellationToken.None); - - var response = result.Should().BeOfType().Subject.Value.Should().BeOfType().Subject; - response.Mode.Should().Be("compare"); - response.CopyOnStartup.Should().BeTrue(); - response.DualWrite.Should().BeTrue(); - response.Modules.Should().ContainSingle().Which.Should().Match(m => m.Module == "hero" && m.Photos == 3 && m.Uses == 2 && !m.Matches && m.Differences.Count == 1); - } - - [Fact] - public async Task The_read_check_without_modules_says_off_and_nothing() - { - var library = Substitute.For(); - - var result = await new MediaLibraryController(library, new Clock()).ReadCheck(CancellationToken.None); - - var response = result.Should().BeOfType().Subject.Value.Should().BeOfType().Subject; - response.Mode.Should().Be("off"); - response.Modules.Should().BeEmpty(); - } } diff --git a/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs b/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs index 7a0855e..c199a95 100644 --- a/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs +++ b/src/SlpModularCms.Core/Hosting/ServiceCollectionExtensions.cs @@ -87,9 +87,6 @@ public static class ServiceCollectionExtensions services.TryAddSingleton(TimeProvider.System); // The media library reads from the usage providers of whichever modules are loaded (none is fine). services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddScoped(); - services.TryAddSingleton(); services.TryAddSingleton(sp => new ResetCooldown(sp.GetRequiredService(), TimeSpan.FromMinutes(sp.GetRequiredService>().Value.CooldownMinutes))); services.AddScoped(); diff --git a/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs b/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs index 9a6b8d8..6b01c28 100644 --- a/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs +++ b/src/SlpModularCms.Core/Media/Library/MediaLibraryController.cs @@ -20,14 +20,6 @@ public sealed record MediaPhotoDto( DateTimeOffset? UnusedSince = null, DateTimeOffset? DeletedAt = null); -/// The result of comparing a module's own tables with the library tables (read switch, phase 2 step C). -public sealed record MediaReadCheckDto(string Module, string Mode, int Photos, int Uses, bool Matches, IReadOnlyList Differences); - -/// The read switch: off, compare or on. -/// The copy switch (start-up copy into the library tables). -/// The write-through switch. -public sealed record MediaReadCheckResponse(string Mode, IReadOnlyList Modules, bool CopyOnStartup = false, bool DualWrite = false); - public sealed record MediaLibraryDto(IReadOnlyList Photos, IReadOnlyList Modules); public sealed record MediaStorageDto(long UsedBytes, long LimitBytes, string Level, int WarningPercent); @@ -57,27 +49,8 @@ public sealed record MediaTrashResponse(IReadOnlyList Results [ApiController] [Route("media-library")] [Authorize(Policy = "AdminOnly")] -public class MediaLibraryController(IMediaLibraryService library, TimeProvider time, MediaStorageUsage? storage = null, IEnumerable? verifiers = null, IMediaLibraryReader? reader = null, IMediaLibrarySwitchState? switches = null) : ControllerBase +public class MediaLibraryController(IMediaLibraryService library, TimeProvider time, MediaStorageUsage? storage = null) : ControllerBase { - /// - /// Compares what each module holds with what the library's own tables hold and lists every difference. Read-only; works - /// whatever the read switch says, so it can be used before the switch is turned on. - /// - [HttpGet("read-check")] - [ProducesResponseType(StatusCodes.Status200OK)] - public async Task ReadCheck(CancellationToken cancellationToken) - { - var results = new List(); - foreach (var verifier in verifiers ?? []) - { - var check = await verifier.CheckAsync(cancellationToken); - results.Add(new MediaReadCheckDto(check.Module, check.Mode.ToString().ToLowerInvariant(), check.Photos, check.Uses, check.Matches, check.Differences.Take(50).ToList())); - } - - var state = switches?.Current ?? new MediaLibrarySwitches(false, false, reader?.Mode ?? MediaReadMode.Off); - return Ok(new MediaReadCheckResponse(state.ReadFromLibrary.ToString().ToLowerInvariant(), results, state.CopyOnStartup, state.DualWrite)); - } - [HttpGet("photos")] [ProducesResponseType(StatusCodes.Status200OK)] public async Task Photos(CancellationToken cancellationToken) diff --git a/src/SlpModularCms.Core/Media/Library/MediaLibraryReader.cs b/src/SlpModularCms.Core/Media/Library/MediaLibraryReader.cs deleted file mode 100644 index 3abbb9d..0000000 --- a/src/SlpModularCms.Core/Media/Library/MediaLibraryReader.cs +++ /dev/null @@ -1,130 +0,0 @@ -namespace SlpModularCms.Core.Media.Library; - -/// Where the media library screen reads the photos of a module from (migration phase 2, step C). -public enum MediaReadMode -{ - /// The module's own tables (the default, and the truth until phase 3). - Off, - - /// Still the module's own tables, but the library tables are read too and every difference is logged and reported. - Compare, - - /// The library tables. Needs the write-through (Media:Library:DualWrite) to be current. - On, -} - -/// Reads a module's photos and uses back from the library's own tables. -public interface IMediaLibraryReader -{ - MediaReadMode Mode { get; } - - Task> ListAssetsAsync(string module, CancellationToken cancellationToken); - - Task> ListUsesAsync(string module, CancellationToken cancellationToken); -} - -/// The default: no library tables to read, so everything stays with the module. -public sealed class NoMediaLibraryReader : IMediaLibraryReader -{ - public MediaReadMode Mode => MediaReadMode.Off; - - public Task> ListAssetsAsync(string module, CancellationToken cancellationToken) => Task.FromResult>([]); - - public Task> ListUsesAsync(string module, CancellationToken cancellationToken) => Task.FromResult>([]); -} - -/// The current state of the migration switches (for the check in the library screen). -public sealed record MediaLibrarySwitches(bool CopyOnStartup, bool DualWrite, MediaReadMode ReadFromLibrary); - -/// Tells the current state of the migration switches. -public interface IMediaLibrarySwitchState -{ - MediaLibrarySwitches Current { get; } -} - -/// The default: no library tables, so every switch is off. -public sealed class NoMediaLibrarySwitchState : IMediaLibrarySwitchState -{ - public MediaLibrarySwitches Current => new(false, false, MediaReadMode.Off); -} - -/// What a comparison of a module's own tables with the library tables found. Differences is empty when they agree. -public sealed record MediaReadCheck(string Module, MediaReadMode Mode, int Photos, int Uses, IReadOnlyList Differences) -{ - public bool Matches => Differences.Count == 0; -} - -/// A module that can compare its own data with the library tables (used by GET media-library/read-check). -public interface IMediaReadVerifier -{ - string Module { get; } - - Task CheckAsync(CancellationToken cancellationToken); -} - -/// Compares what a module holds with what the library tables hold, by id, and lists every difference in words. -public static class MediaReadComparer -{ - public static IReadOnlyList Compare( - IReadOnlyList ownAssets, - IReadOnlyList ownUses, - IReadOnlyList libraryAssets, - IReadOnlyList libraryUses) - { - var differences = new List(); - var library = libraryAssets.ToDictionary(a => a.Id); - foreach (var own in ownAssets) - { - if (!library.Remove(own.Id, out var copy)) - { - differences.Add($"photo {own.Id} is missing in the library"); - continue; - } - - if (own.OriginalSha256 != copy.OriginalSha256 || own.OriginalKey != copy.OriginalKey || own.Bytes != copy.Bytes) - { - differences.Add($"photo {own.Id}: hash, key or size differs"); - } - - if (own.SetHash != copy.SetHash || own.VariantsJson != copy.VariantsJson) - { - differences.Add($"photo {own.Id}: set hash or variants differ"); - } - - if ((own.DeletedAt is null) != (copy.DeletedAt is null)) - { - differences.Add($"photo {own.Id}: trash state differs"); - } - - if (own.OriginalFileName != copy.OriginalFileName) - { - differences.Add($"photo {own.Id}: file name differs"); - } - - if (own.LastUsedAt?.ToUnixTimeSeconds() != copy.LastUsedAt?.ToUnixTimeSeconds()) - { - differences.Add($"photo {own.Id}: last used differs"); - } - } - - differences.AddRange(library.Keys.Select(id => $"photo {id} is in the library but not in the module")); - - var libraryUsesById = libraryUses.ToDictionary(u => u.Id); - foreach (var own in ownUses) - { - if (!libraryUsesById.Remove(own.Id, out var copy)) - { - differences.Add($"use {own.Id} is missing in the library"); - continue; - } - - if (own.AssetId != copy.AssetId || own.Position != copy.Position || own.Active != copy.Active || own.SetHash != copy.SetHash || own.VariantsJson != copy.VariantsJson) - { - differences.Add($"use {own.Id}: photo, position, active flag or set hash differs"); - } - } - - differences.AddRange(libraryUsesById.Keys.Select(id => $"use {id} is in the library but not in the module")); - return differences; - } -} diff --git a/src/SlpModularCms.Core/Media/Library/MediaLibrarySync.cs b/src/SlpModularCms.Core/Media/Library/MediaLibrarySync.cs deleted file mode 100644 index 6fd0bc5..0000000 --- a/src/SlpModularCms.Core/Media/Library/MediaLibrarySync.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace SlpModularCms.Core.Media.Library; - -/// -/// Write-through of a module's photos and uses into the media library's own tables (migration phase 2, step A). A module -/// calls it after every change it saved to its OWN tables, which stay the truth until phase 3. It never throws and never -/// changes the module's data: a failure is logged by the implementation and the next sync (or the verified copy) repairs it. -/// With the switch Media:Library:DualWrite off (the default) it does nothing. -/// -public interface IMediaLibrarySync -{ - /// Mirrors everything the module offers through its into the library tables. - Task SyncAsync(string module, CancellationToken cancellationToken); -} - -/// The default: the Media module is not there or the switch is off, so nothing is written. -public sealed class NoMediaLibrarySync : IMediaLibrarySync -{ - public Task SyncAsync(string module, CancellationToken cancellationToken) => Task.CompletedTask; -} diff --git a/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs b/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs deleted file mode 100644 index 0a7eea6..0000000 --- a/src/SlpModularCms.Core/Media/Library/MediaMigrationTypes.cs +++ /dev/null @@ -1,54 +0,0 @@ -namespace SlpModularCms.Core.Media.Library; - -/// One crop of a photo for a ratio (focus point and zoom). -public sealed record MediaCropSnapshot(string Ratio, double X, double Y, double Zoom); - -/// -/// A photo as a module holds it today, for copying into the library's own tables. Same ids, hashes and set hashes as the -/// module uses, so no public address changes (migration plan, decision 13). -/// -// Phase 3: the snapshots also carry what the module needs to publish the photo from the library tables: the variants of the -// current set (opaque JSON, the module's own format) and the wide and tall overrides of the photo's crop (null = none). -public sealed record MediaAssetSnapshot( - Guid Id, - string Module, - string OriginalSha256, - string OriginalKey, - string SourceFormat, - int Width, - int Height, - long Bytes, - double FocusX, - double FocusY, - double Zoom, - string SetHash, - IReadOnlyList Crops, - DateTimeOffset CreatedAt, - Guid CreatedByUserId, - string? OriginalFileName, - DateTimeOffset? LastUsedAt, - DateTimeOffset? DeletedAt, - string? VariantsJson = null, - double? WideFocusX = null, - double? WideFocusY = null, - double? WideZoom = null, - double? TallFocusX = null, - double? TallFocusY = null, - double? TallZoom = null); - -/// One place a photo is used, as a module holds it today. Crops are the crops of THIS use (crop per use); null = the crops of its photo. -public sealed record MediaUseSnapshot(Guid Id, Guid AssetId, string Module, string PlaceKind, int Position, bool Active, string SetHash, IReadOnlyList? Crops = null, string? VariantsJson = null); - -/// -/// What a module offers so the library can copy its photos and uses into its own tables. Read-only: the module's data is -/// never changed by the copy. -/// -public interface IMediaMigrationSource -{ - string Module { get; } - - /// Every photo of the module, also those in the trash. - Task> ListAssetsAsync(CancellationToken cancellationToken); - - Task> ListUsesAsync(CancellationToken cancellationToken); -} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroLibrarySyncTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroLibrarySyncTests.cs deleted file mode 100644 index 4bd3a7f..0000000 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroLibrarySyncTests.cs +++ /dev/null @@ -1,94 +0,0 @@ -using FluentAssertions; -using NSubstitute; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Hero.Data.Entities; -using SlpModularCms.Modules.Hero.Models; -using SlpModularCms.Modules.Hero.Services; -using Xunit; - -namespace SlpModularCms.Modules.Hero.Tests; - -/// Phase 2, step A: the hero tells the media library after every change it saved (the write-through). -public class HeroLibrarySyncTests -{ - private static (HeroTestRig Rig, IMediaLibrarySync Sync) Rig() - { - var sync = Substitute.For(); - return (new HeroTestRig(librarySync: sync), sync); - } - - private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides); - - [Fact] - public async Task Every_change_of_a_photo_or_a_slide_is_written_through_for_the_hero_module() - { - var (rig, sync) = Rig(); - using var _r = rig; - var image = await rig.Upload(HeroTestRig.Photo()); - var id = Guid.Parse(image.Id); - var afterUpload = sync.ReceivedCalls().Count(); - afterUpload.Should().BeGreaterThan(0, "the upload and the rendering of its variants both wrote through"); - - var saved = await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, id, "een")), HeroTestRig.Admin, CancellationToken.None); - var slideId = Guid.Parse(saved.Slides[0].Id); - await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.2, 0.3, 1.5)), HeroTestRig.Admin, CancellationToken.None); - await rig.Images.SetSlideFocusAsync(slideId, new HeroFocusRequest(new FocusDto(0.6, 0.6, 1.2)), HeroTestRig.Admin, CancellationToken.None); - await rig.RenderPending(); - await rig.Images.ResetSlideFocusAsync(slideId, HeroTestRig.Admin, CancellationToken.None); - await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(id, HeroTestRig.Admin, CancellationToken.None); - await rig.Images.RestoreAsync(id, HeroTestRig.Admin, CancellationToken.None); - - await sync.Received().SyncAsync("hero", Arg.Any()); - sync.ReceivedCalls().Count().Should().BeGreaterThan(afterUpload + 5); - sync.ReceivedCalls().Select(c => c.GetArguments()[0]).Should().OnlyContain(m => (string)m! == "hero"); - } - - [Fact] - public async Task Without_a_write_through_nothing_changes_for_the_hero() - { - using var rig = new HeroTestRig(); - - var image = await rig.Upload(HeroTestRig.Photo()); - - image.Processing.Should().BeFalse(); - } - - [Fact] - public async Task The_migration_source_gives_a_slide_with_its_own_crop_its_own_set_and_crops() - { - using var rig = new HeroTestRig(); - 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.SetSlideFocusAsync(Guid.Parse(saved.Slides[1].Id), new HeroFocusRequest(new FocusDto(0.1, 0.9, 1.4)), HeroTestRig.Admin, CancellationToken.None); - await rig.RenderPending(); - - var uses = await new HeroMediaMigrationSource(rig.Repository).ListUsesAsync(CancellationToken.None); - - var photoSet = rig.Db.HeroImages.Single(i => i.Id == id).SetHash; - var first = uses.Single(u => u.Position == 1); - var second = uses.Single(u => u.Position == 2); - first.SetHash.Should().Be(photoSet); - first.Crops.Should().BeNull("a slide without its own crop uses the crops of its photo"); - second.SetHash.Should().NotBe(photoSet).And.Be(rig.Db.HeroSlides.Single(s => s.Id == Guid.Parse(saved.Slides[1].Id)).SetHash); - second.Crops.Should().NotBeNull(); - second.Crops!.Single(c => c.Ratio == "wide").X.Should().BeApproximately(0.1, 0.0001); - } - [Fact] - public async Task The_migration_source_gives_the_variants_in_the_format_the_hero_stores_them_and_the_overrides_of_the_crop() - { - using var rig = new HeroTestRig(); - var image = await rig.Upload(HeroTestRig.Photo()); - var id = Guid.Parse(image.Id); - await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.5, 0.5, 1), new FocusDto(0.2, 0.3, 1.5), null), HeroTestRig.Admin, CancellationToken.None); - await rig.RenderPending(); - - var asset = (await new HeroMediaMigrationSource(rig.Repository).ListAssetsAsync(CancellationToken.None)).Single(); - - var stored = rig.Db.HeroImages.Single(i => i.Id == id); - asset.SetHash.Should().Be(stored.SetHash); - System.Text.Json.JsonSerializer.Deserialize>(asset.VariantsJson!).Should().Equal(stored.Variants); - (asset.WideFocusX, asset.WideFocusY, asset.WideZoom).Should().Be((0.2, 0.3, 1.5)); - (asset.TallFocusX, asset.TallFocusY, asset.TallZoom).Should().Be((null, null, null)); - } -} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroMediaMigrationSourceTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroMediaMigrationSourceTests.cs deleted file mode 100644 index e6a8262..0000000 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroMediaMigrationSourceTests.cs +++ /dev/null @@ -1,56 +0,0 @@ -using FluentAssertions; -using SlpModularCms.Modules.Hero.Models; -using SlpModularCms.Modules.Hero.Services; -using Xunit; - -namespace SlpModularCms.Modules.Hero.Tests; - -public class HeroMediaMigrationSourceTests -{ - [Fact] - public async Task Offers_every_photo_with_the_ids_hashes_and_set_hashes_the_hero_uses_and_one_use_per_slide() - { - using var rig = new HeroTestRig(); - var a = await rig.Upload(HeroTestRig.Photo(seed: 1)); - var b = await rig.Upload(HeroTestRig.Photo(seed: 2)); - var c = await rig.Upload(HeroTestRig.Photo(seed: 3)); - await rig.Service.SaveAsync( - new HeroSaveRequest("Titel", null, null, "single", [], [new HeroSlideRequest(null, Guid.Parse(b.Id), "twee"), new HeroSlideRequest(null, Guid.Parse(a.Id), "een", IsActive: false)]), - HeroTestRig.Admin, CancellationToken.None); - await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(c.Id), HeroTestRig.Admin, CancellationToken.None); - var source = new HeroMediaMigrationSource(rig.Repository); - - var assets = await source.ListAssetsAsync(CancellationToken.None); - var uses = await source.ListUsesAsync(CancellationToken.None); - - source.Module.Should().Be("hero"); - assets.Select(x => x.Id).Should().BeEquivalentTo([Guid.Parse(a.Id), Guid.Parse(b.Id), Guid.Parse(c.Id)]); - foreach (var asset in assets) - { - var row = rig.Db.HeroImages.Single(i => i.Id == asset.Id); - asset.OriginalSha256.Should().Be(row.OriginalSha256); - asset.OriginalKey.Should().Be(row.OriginalKey); - asset.SetHash.Should().Be(row.SetHash).And.NotBeEmpty(); - asset.Crops.Select(x => x.Ratio).Should().Equal("wide", "tall"); - } - - assets.Single(x => x.Id == Guid.Parse(c.Id)).DeletedAt.Should().NotBeNull(); - uses.Should().HaveCount(2); - uses.Single(u => u.AssetId == Guid.Parse(b.Id)).Should().Match(u => u.Position == 1 && u.Active && u.PlaceKind == "slide"); - uses.Single(u => u.AssetId == Guid.Parse(a.Id)).Should().Match(u => u.Position == 2 && !u.Active); - uses.Select(u => u.SetHash).Should().OnlyContain(h => h.Length == 16); - } - - [Fact] - public async Task Reading_changes_nothing() - { - using var rig = new HeroTestRig(); - await rig.Upload(HeroTestRig.Photo()); - var source = new HeroMediaMigrationSource(rig.Repository); - - await source.ListAssetsAsync(CancellationToken.None); - await source.ListUsesAsync(CancellationToken.None); - - rig.Db.ChangeTracker.HasChanges().Should().BeFalse(); - } -} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryMariaDbTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryMariaDbTests.cs deleted file mode 100644 index ba7a6c3..0000000 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryMariaDbTests.cs +++ /dev/null @@ -1,185 +0,0 @@ -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; - -/// -/// 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 -/// ConnectionStrings__DefaultConnection is set (CI sets it); otherwise it does nothing. -/// -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() - .UseMySQL(connectionString) - .ReplaceService() - .ReplaceService() - .ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)) - .Options, - new HeroStorage(inLibrary)); - - private static MediaDbContext OpenMedia(string connectionString) - => new(new DbContextOptionsBuilder().UseMySQL(connectionString).ReplaceService().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.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(); - await migrator.MigrateAsync("20261117071410_RemoveHeroScheduleAndSwitch"); - (await Count(db, "FK_HeroSlides_HeroImages_ImageId")).Should().NotBe(0); - } - finally - { - await DropAsync(connection); - } - } - - private static async Task 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(); - } -} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTableTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTableTests.cs new file mode 100644 index 0000000..8978c94 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTableTests.cs @@ -0,0 +1,186 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using MySql.Data.MySqlClient; +using SlpModularCms.Core.Hosting; +using SlpModularCms.Modules.Hero.Data; +using SlpModularCms.Modules.Hero.Data.Entities; +using SlpModularCms.Modules.Media.Data; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// +/// Phase 3c against a real MariaDB in a database of its own: the hero's photos are rows of the library's MediaAssets table, the +/// migration that drops HeroImages only runs when the library holds every photo, and its Down brings back the (empty) table. +/// Runs when ConnectionStrings__DefaultConnection is set (CI sets it); otherwise it does nothing. +/// +public class HeroPhotosInLibraryTableTests +{ + private static string? ConnectionString() + { + var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection"); + return string.IsNullOrWhiteSpace(baseString) + ? null + : new MySqlConnectionStringBuilder(baseString) { Database = "hero_phase3c_test_" + Guid.NewGuid().ToString("N")[..12] }.ConnectionString; + } + + private static HeroDbContext OpenHero(string connectionString) + => new(new DbContextOptionsBuilder().UseMySQL(connectionString).ReplaceService().Options); + + private static MediaDbContext OpenMedia(string connectionString) + => new(new DbContextOptionsBuilder().UseMySQL(connectionString).ReplaceService().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", + }; + + private static async Task ScalarAsync(HeroDbContext db, string sql) + { + var connection = db.Database.GetDbConnection(); + if (connection.State != System.Data.ConnectionState.Open) + { + await connection.OpenAsync(); + } + + await using var command = connection.CreateCommand(); + command.CommandText = sql; + return Convert.ToInt64(await command.ExecuteScalarAsync()); + } + + private static Task TableExists(HeroDbContext db, string table) + => ScalarAsync(db, $"SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '{table}'"); + + [Fact] + public async Task On_a_new_database_the_hero_writes_and_reads_rows_of_MediaAssets_and_HeroImages_is_gone() + { + var connection = ConnectionString(); + if (connection is null) + { + return; + } + + try + { + await using var db = OpenHero(connection); + HeroImagesDropGuard.Verify(db); + await db.Database.MigrateAsync(); + await using (var media = OpenMedia(connection)) + { + await media.Database.MigrateAsync(); + } + + (await TableExists(db, "HeroImages")).Should().Be(0); + + 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(); + + 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"); + + var read = await new Repositories.HeroRepository(OpenHero(connection)).GetReadOnlyAsync(HeroLanguages.Default, CancellationToken.None); + read!.Slides.Single().Image!.Id.Should().Be(photo.Id); + } + finally + { + await DropAsync(connection); + } + } + + [Fact] + public async Task HeroImages_is_only_dropped_when_the_library_holds_every_photo_and_the_down_brings_back_the_empty_table() + { + var connection = ConnectionString(); + if (connection is null) + { + return; + } + + try + { + await using var db = OpenHero(connection); + var migrations = db.Database.GetMigrations().ToList(); + var dropIndex = migrations.FindIndex(m => m.EndsWith("_DropHeroImagesTable", StringComparison.Ordinal)); + dropIndex.Should().BeGreaterThan(0); + var migrator = db.GetService(); + await migrator.MigrateAsync(migrations[dropIndex - 1]); + await using (var media = OpenMedia(connection)) + { + await media.Database.MigrateAsync(); + } + + // A photo the library does not have: refused, nothing is dropped. + var sha = new string('c', 64); + var id = Guid.NewGuid(); + await db.Database.ExecuteSqlRawAsync( + "INSERT INTO `HeroImages` (Id, OriginalSha256, OriginalKey, SourceFormat, Width, Height, Bytes, FocusX, FocusY, Zoom, SetHash, Variants, CreatedAt, CreatedByUserId) " + + $"VALUES ('{id}', '{sha}', 'originals/c.jpg', 'jpg', 10, 10, 10, 0.5, 0.5, 1, '0123456789abcdef', '[]', NOW(), '{Guid.NewGuid()}')"); + var refused = () => HeroImagesDropGuard.Verify(db); + refused.Should().Throw().Which.Message.Should().Contain("not in the library"); + (await TableExists(db, "HeroImages")).Should().Be(1); + + // A photo that differs in its set: refused as well. + await db.Database.ExecuteSqlRawAsync( + "INSERT INTO `MediaAssets` (Id, Module, OriginalSha256, OriginalKey, SourceFormat, Width, Height, Bytes, DefaultFocusX, DefaultFocusY, DefaultZoom, CreatedAt, CreatedByUserId, SetHash, Variants) " + + $"VALUES ('{id}', 'hero', '{sha}', 'originals/c.jpg', 'jpg', 10, 10, 10, 0.5, 0.5, 1, NOW(), '{Guid.NewGuid()}', 'ffffffffffffffff', '[]')"); + var differs = () => HeroImagesDropGuard.Verify(db); + differs.Should().Throw(); + + // The library holds it as it is: the guard passes and the migration drops the table. + await db.Database.ExecuteSqlRawAsync($"UPDATE `MediaAssets` SET SetHash = '0123456789abcdef' WHERE Id = '{id}'"); + HeroImagesDropGuard.Verify(db); + await db.Database.MigrateAsync(); + (await TableExists(db, "HeroImages")).Should().Be(0); + (await ScalarAsync(db, "SELECT COUNT(*) FROM `MediaAssets`")).Should().Be(1, "the library rows are untouched"); + + // Down: the table is back, empty (the data comes from the restore point). + await migrator.MigrateAsync(migrations[dropIndex - 1]); + (await TableExists(db, "HeroImages")).Should().Be(1); + (await ScalarAsync(db, "SELECT COUNT(*) FROM `HeroImages`")).Should().Be(0); + } + finally + { + await DropAsync(connection); + } + } + + 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(); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTests.cs deleted file mode 100644 index b35043f..0000000 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosInLibraryTests.cs +++ /dev/null @@ -1,111 +0,0 @@ -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; - -/// Phase 3b: the hero's photos live in the library's table (rows of module "hero"), and HeroImages stays a complete copy. -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()).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()).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); - } -} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosModelTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosModelTests.cs new file mode 100644 index 0000000..2d54bcc --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroPhotosModelTests.cs @@ -0,0 +1,33 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using SlpModularCms.Modules.Hero.Data; +using SlpModularCms.Modules.Hero.Data.Entities; +using Xunit; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// Phase 3c: the hero's photos are rows of the library's table, not of a table of the hero. +public class HeroPhotosModelTests +{ + [Fact] + public void The_model_maps_the_photos_to_the_library_table_with_a_module_column_and_a_filter_on_it() + { + using var rig = new HeroTestRig(); + var photo = rig.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(); + photo.GetQueryFilter().Should().NotBeNull(); + } + + [Fact] + public async Task A_new_photo_belongs_to_the_hero_module() + { + using var rig = new HeroTestRig(); + var image = await rig.Upload(HeroTestRig.Photo()); + + rig.Db.Entry(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(image.Id))).Property(HeroDbContext.ModuleColumn).CurrentValue.Should().Be("hero"); + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroReadFromLibraryTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroReadFromLibraryTests.cs deleted file mode 100644 index fca74c3..0000000 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroReadFromLibraryTests.cs +++ /dev/null @@ -1,124 +0,0 @@ -using FluentAssertions; -using Microsoft.Extensions.Logging.Abstractions; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Hero.Models; -using SlpModularCms.Modules.Hero.Services; -using Xunit; - -namespace SlpModularCms.Modules.Hero.Tests; - -/// Phase 2, step C: the library screen reads the hero's photos from the library tables (on), or compares (compare). -public class HeroReadFromLibraryTests -{ - private sealed class FakeReader(MediaReadMode mode, List assets, List uses, bool fails = false) : IMediaLibraryReader - { - public MediaReadMode Mode => mode; - - public Task> ListAssetsAsync(string module, CancellationToken cancellationToken) - => fails ? throw new InvalidOperationException("library down") : Task.FromResult>(assets.ToList()); - - public Task> ListUsesAsync(string module, CancellationToken cancellationToken) - => fails ? throw new InvalidOperationException("library down") : Task.FromResult>(uses.ToList()); - } - - private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides); - - /// A hero with two photos, one in a slide, one not; the library tables made equal to it. - private static async Task<(HeroTestRig Rig, List Assets, List Uses, Guid Used, Guid Unused)> ArrangeAsync() - { - var rig = new HeroTestRig(); - var used = Guid.Parse((await rig.Upload(HeroTestRig.Photo())).Id); - var unused = Guid.Parse((await rig.Upload(HeroTestRig.Photo(seed: 7))).Id); - await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, used, "een")), HeroTestRig.Admin, CancellationToken.None); - var source = new HeroMediaMigrationSource(rig.Repository); - return (rig, (await source.ListAssetsAsync(CancellationToken.None)).ToList(), (await source.ListUsesAsync(CancellationToken.None)).ToList(), used, unused); - } - - private static HeroMediaUsageProvider Provider(HeroTestRig rig, IMediaLibraryReader? reader) => new(rig.Repository, rig.Images, reader, NullLogger.Instance); - - [Fact] - public async Task With_the_switch_off_the_heros_own_tables_answer_and_the_library_is_not_asked() - { - var (rig, assets, uses, used, _) = await ArrangeAsync(); - using var _r = rig; - assets.RemoveAll(a => a.Id == used); - - var photos = await Provider(rig, new FakeReader(MediaReadMode.Off, assets, uses, fails: true)).ListPhotosAsync(CancellationToken.None); - - photos.Should().HaveCount(2); - } - - [Fact] - public async Task On_answers_from_the_library_tables_with_the_thumbnail_of_the_hero() - { - var (rig, assets, uses, used, unused) = await ArrangeAsync(); - using var _r = rig; - // The library knows a file name the hero does not (proof that the answer comes from the library) and lacks the unused photo. - assets[assets.FindIndex(a => a.Id == used)] = assets.Single(a => a.Id == used) with { OriginalFileName = "uit-de-bibliotheek.jpg" }; - assets.RemoveAll(a => a.Id == unused); - var provider = Provider(rig, new FakeReader(MediaReadMode.On, assets, uses)); - - var photos = await provider.ListPhotosAsync(CancellationToken.None); - - photos.Should().ContainSingle().Which.Should().Match(p => p.Id == used && p.OriginalFileName == "uit-de-bibliotheek.jpg" && p.ThumbnailUrl != null && p.ThumbnailUrl.StartsWith("/media/hero/")); - (await provider.ListUsesAsync([used, unused], CancellationToken.None)).Should().ContainSingle().Which.Should().Match(u => u.PhotoId == used && u.Position == 1 && u.PlaceKind == "slide"); - } - - [Fact] - public async Task On_splits_the_library_into_photos_and_trash_by_the_deleted_date() - { - var (rig, assets, uses, used, unused) = await ArrangeAsync(); - using var _r = rig; - assets[assets.FindIndex(a => a.Id == unused)] = assets.Single(a => a.Id == unused) with { DeletedAt = DateTimeOffset.UtcNow }; - var provider = Provider(rig, new FakeReader(MediaReadMode.On, assets, uses)); - - (await provider.ListPhotosAsync(CancellationToken.None)).Select(p => p.Id).Should().Equal(used); - (await provider.ListTrashAsync(CancellationToken.None)).Select(p => p.Id).Should().Equal(unused); - } - - [Fact] - public async Task On_falls_back_to_the_heros_own_tables_when_the_library_cannot_be_read() - { - var (rig, assets, uses, _, _) = await ArrangeAsync(); - using var _r = rig; - var provider = Provider(rig, new FakeReader(MediaReadMode.On, assets, uses, fails: true)); - - (await provider.ListPhotosAsync(CancellationToken.None)).Should().HaveCount(2); - (await provider.ListUsesAsync([Guid.NewGuid()], CancellationToken.None)).Should().BeEmpty(); - (await provider.ListTrashAsync(CancellationToken.None)).Should().BeEmpty(); - } - - [Fact] - public async Task Compare_still_answers_from_the_heros_tables_and_the_check_lists_every_difference() - { - var (rig, assets, uses, used, unused) = await ArrangeAsync(); - using var _r = rig; - assets.RemoveAll(a => a.Id == unused); - assets[assets.FindIndex(a => a.Id == used)] = assets.Single(a => a.Id == used) with { Bytes = 1 }; - uses[0] = uses[0] with { Position = 9 }; - var provider = Provider(rig, new FakeReader(MediaReadMode.Compare, assets, uses)); - - (await provider.ListPhotosAsync(CancellationToken.None)).Should().HaveCount(2, "compare never changes the answer"); - var check = await provider.CheckAsync(CancellationToken.None); - - check.Matches.Should().BeFalse(); - check.Mode.Should().Be(MediaReadMode.Compare); - check.Photos.Should().Be(2); - check.Uses.Should().Be(1); - check.Differences.Should().Contain(d => d.Contains(unused.ToString()) && d.Contains("missing in the library")); - check.Differences.Should().Contain(d => d.Contains(used.ToString()) && d.Contains("hash, key or size")); - check.Differences.Should().Contain(d => d.StartsWith("use ") && d.Contains("position")); - } - - [Fact] - public async Task The_check_agrees_when_the_library_is_a_mirror_of_the_hero() - { - var (rig, assets, uses, _, _) = await ArrangeAsync(); - using var _r = rig; - - var check = await Provider(rig, new FakeReader(MediaReadMode.Compare, assets, uses)).CheckAsync(CancellationToken.None); - - check.Matches.Should().BeTrue(); - check.Differences.Should().BeEmpty(); - } -} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs index 6f25f18..cb4a26a 100644 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroTestRig.cs @@ -39,15 +39,12 @@ public sealed class HeroTestRig : IDisposable public HeroMediaCleanup Cleanup { get; } public HeroOptions HeroOptions { get; } = new(); - public IHeroLegacyImageMirror LegacyMirror { get; } - - public HeroTestRig(int maxPublishedSlides = 50, SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, bool photosInLibrary = false) + public HeroTestRig(int maxPublishedSlides = 50) { HeroOptions.MaxPublishedSlides = maxPublishedSlides; Media = new MediaOptions { Directory = Directory, BusyWaitSeconds = 0 }; var mediaOptions = Options.Create(Media); - Db = new HeroDbContext(new DbContextOptionsBuilder().UseInMemoryDatabase("hero-" + Guid.NewGuid()).ReplaceService().Options, new HeroStorage(photosInLibrary)); - LegacyMirror = new HeroLegacyImageMirror(Db, NullLogger.Instance); + Db = new HeroDbContext(new DbContextOptionsBuilder().UseInMemoryDatabase("hero-" + Guid.NewGuid()).Options); Repository = new HeroRepository(Db); Store = new FileSystemMediaStore(mediaOptions); Processor = new MagickImageProcessor(mediaOptions, NullLogger.Instance); @@ -55,10 +52,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.Instance, Time, IndexCache, Signal, librarySync, LegacyMirror); - Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger.Instance, Time, IndexCache, null, librarySync, LegacyMirror); + Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger.Instance, Time, IndexCache, Signal); + Service = new HeroService(Repository, Images, Options.Create(HeroOptions), NullLogger.Instance, Time, IndexCache); Public = new PublicHeroService(Repository, Options.Create(HeroOptions), Time); - Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger.Instance, Time, librarySync, LegacyMirror); + Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger.Instance, Time); } /// A photo-like fixture: the left half red, the right half blue, with a camera tag. diff --git a/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs b/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs index f5520c3..5eb885e 100644 --- a/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs +++ b/src/SlpModularCms.Modules.Hero/Data/Entities/HeroEntities.cs @@ -112,37 +112,6 @@ public class HeroImage }; } -/// -/// The same photo as , always stored in HeroImages. Only used while the photos live in the -/// library (phase 3b): then HeroImages is kept up to date from the library rows so the switch can be turned back. -/// -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 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; } -} - /// One file of the current set: the ratio, its pixel size, the type and the file name inside the set. public record HeroVariant(string Ratio, int Width, int Height, string Type, string FileName, long Bytes); diff --git a/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs b/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs index 60f6c7c..b12fe04 100644 --- a/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs +++ b/src/SlpModularCms.Modules.Hero/Data/HeroDbContext.cs @@ -6,14 +6,8 @@ using SlpModularCms.Modules.Hero.Services; namespace SlpModularCms.Modules.Hero.Data; -public class HeroDbContext(DbContextOptions options, HeroStorage? storage = null) : DbContext(options) +public class HeroDbContext(DbContextOptions options) : DbContext(options) { - /// True while the photos live in the library's table (phase 3b); see . - public bool PhotosInLibrary { get; } = storage?.PhotosInLibrary ?? false; - - /// The photos as they are always kept in HeroImages (only mapped while is on). - public DbSet LegacyHeroImages => Set(); - public DbSet Heroes => Set(); public DbSet HeroSlides => Set(); public DbSet HeroImages => Set(); @@ -69,7 +63,14 @@ public class HeroDbContext(DbContextOptions options, HeroStorage? modelBuilder.Entity(entity => { - entity.ToTable("HeroImages"); + // The hero's photos are rows of the library's table (module "hero"). The library (Media module) owns that table and its + // migrations, the hero's context only excludes it from its own. + entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations()); + entity.Property(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(e, ModuleColumn) == HeroMapping.Area); entity.HasKey(e => e.Id); entity.Property(e => e.OriginalSha256).IsRequired().HasMaxLength(64); entity.HasIndex(e => e.OriginalSha256).IsUnique(); @@ -93,8 +94,6 @@ public class HeroDbContext(DbContextOptions options, HeroStorage? v => v.ToList())); }); - ConfigureStorage(modelBuilder); - modelBuilder.Entity(entity => { entity.ToTable("HeroRetiredFiles"); @@ -105,39 +104,6 @@ public class HeroDbContext(DbContextOptions options, HeroStorage? }); } - private void ConfigureStorage(ModelBuilder modelBuilder) - { - if (!PhotosInLibrary) - { - modelBuilder.Ignore(); - return; - } - - // The hero's photos are rows of the library's table, module "hero". The library owns that table and its migrations. - modelBuilder.Entity(entity => - { - entity.ToTable("MediaAssets", table => table.ExcludeFromMigrations()); - entity.Property(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(e, ModuleColumn) == HeroMapping.Area); - }); - - // And HeroImages stays a complete copy, so switching back loses nothing. - modelBuilder.Entity(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>(json, (JsonSerializerOptions?)null) ?? new List()) - .HasColumnType("longtext"); - }); - } - public const string ModuleColumn = "Module"; public override int SaveChanges(bool acceptAllChangesOnSuccess) @@ -155,11 +121,6 @@ public class HeroDbContext(DbContextOptions options, HeroStorage? /// A photo the hero adds to the library's table belongs to the hero module. private void MarkNewPhotosAsHero() { - if (!PhotosInLibrary) - { - return; - } - foreach (var entry in ChangeTracker.Entries().Where(e => e.State == EntityState.Added)) { entry.Property(ModuleColumn).CurrentValue = HeroMapping.Area; diff --git a/src/SlpModularCms.Modules.Hero/Data/HeroImagesDropGuard.cs b/src/SlpModularCms.Modules.Hero/Data/HeroImagesDropGuard.cs new file mode 100644 index 0000000..1153250 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Data/HeroImagesDropGuard.cs @@ -0,0 +1,75 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace SlpModularCms.Modules.Hero.Data; + +/// +/// Runs before the migrations at start-up. The migration DropHeroImagesTable destroys HeroImages; this refuses to let +/// it run (it throws, the application does not start, nothing is dropped) unless every photo of HeroImages is in the +/// library's MediaAssets (module hero) with the same hash, set and variants. A database where HeroImages is empty +/// (a new one) passes without the library table having to exist yet. +/// +public static class HeroImagesDropGuard +{ + public static void Verify(HeroDbContext db) + { + // A database that does not exist yet (a new environment) has nothing to protect; Migrate creates it. + if (!db.Database.CanConnect()) + { + return; + } + + if (!db.Database.GetPendingMigrations().Any(id => id.EndsWith("_DropHeroImagesTable", StringComparison.Ordinal))) + { + return; + } + + if (!Exists(db, "HeroImages") || Scalar(db, "SELECT COUNT(*) FROM `HeroImages`") == 0) + { + return; + } + + 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."); + } + + var missing = Scalar(db, @"SELECT COUNT(*) FROM `HeroImages` h + LEFT JOIN `MediaAssets` m ON m.`Id` = h.`Id` AND m.`Module` = 'hero' + WHERE m.`Id` IS NULL + OR NOT (m.`OriginalSha256` <=> h.`OriginalSha256`) + OR NOT (m.`SetHash` <=> h.`SetHash`) + 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."); + } + } + + private static bool Exists(HeroDbContext db, string table) + => Scalar(db, $"SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '{table}'") > 0; + + private static long Scalar(HeroDbContext db, string sql) + { + var connection = db.Database.GetDbConnection(); + var wasOpen = connection.State == System.Data.ConnectionState.Open; + if (!wasOpen) + { + connection.Open(); + } + + try + { + using var command = connection.CreateCommand(); + command.CommandText = sql; + return Convert.ToInt64(command.ExecuteScalar()); + } + finally + { + if (!wasOpen) + { + connection.Close(); + } + } + } +} diff --git a/src/SlpModularCms.Modules.Hero/Data/HeroStorage.cs b/src/SlpModularCms.Modules.Hero/Data/HeroStorage.cs deleted file mode 100644 index 6b0b7c8..0000000 --- a/src/SlpModularCms.Modules.Hero/Data/HeroStorage.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; - -namespace SlpModularCms.Modules.Hero.Data; - -/// -/// Where the hero's photos are stored (media library phase 3b). Off (default): the hero's own table HeroImages. On -/// (Media__Library__HeroPhotosInLibrary=true): the library's MediaAssets table (rows of module hero), with -/// HeroImages kept up to date as a safety net so the switch can go back. The choice is made when the application starts. -/// -public sealed record HeroStorage(bool PhotosInLibrary); - -/// EF builds the model once per key: the two storage modes have different models. -public sealed class HeroModelCacheKeyFactory : IModelCacheKeyFactory -{ - public object Create(DbContext context, bool designTime) - => (context.GetType(), context is HeroDbContext { PhotosInLibrary: true }, designTime); -} diff --git a/src/SlpModularCms.Modules.Hero/HeroModule.cs b/src/SlpModularCms.Modules.Hero/HeroModule.cs index 79e0025..54e50ec 100644 --- a/src/SlpModularCms.Modules.Hero/HeroModule.cs +++ b/src/SlpModularCms.Modules.Hero/HeroModule.cs @@ -1,8 +1,6 @@ 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; @@ -28,21 +26,12 @@ 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().GetValue("Media:Library:HeroPhotosInLibrary"))); services.AddDbContext((serviceProvider, options) => { var config = serviceProvider.GetRequiredService(); options.UseMySQL(config.GetConnectionString("DefaultConnection")!); options.ReplaceService(); - options.ReplaceService(); - if (serviceProvider.GetRequiredService().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(); services.AddCmsTextsFrom(typeof(HeroModule).Assembly); services.AddOptions().Configure((options, configuration) => @@ -58,8 +47,6 @@ public class HeroModule : IModule services.AddScoped(); services.AddScoped(); services.AddScoped(provider => provider.GetRequiredService()); - services.AddScoped(provider => provider.GetRequiredService()); - services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); @@ -73,7 +60,7 @@ public class HeroModule : IModule { using var scope = app.ApplicationServices.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); + HeroImagesDropGuard.Verify(db); db.Database.Migrate(); - HeroPhotoStorageGuard.VerifyAsync(db, CancellationToken.None).GetAwaiter().GetResult(); } } diff --git a/src/SlpModularCms.Modules.Hero/Migrations/20261117231338_DropHeroImagesTable.Designer.cs b/src/SlpModularCms.Modules.Hero/Migrations/20261117231338_DropHeroImagesTable.Designer.cs new file mode 100644 index 0000000..66a97fd --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Migrations/20261117231338_DropHeroImagesTable.Designer.cs @@ -0,0 +1,304 @@ +// +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("20261117231338_DropHeroImagesTable")] + partial class DropHeroImagesTable + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Button1Label") + .HasMaxLength(100) + .HasColumnType("varchar(100)"); + + b.Property("Button1Target") + .HasMaxLength(500) + .HasColumnType("varchar(500)"); + + b.Property("Button2Label") + .HasMaxLength(100) + .HasColumnType("varchar(100)"); + + b.Property("Button2Target") + .HasMaxLength(500) + .HasColumnType("varchar(500)"); + + b.Property("Display") + .IsRequired() + .HasMaxLength(20) + .HasColumnType("varchar(20)"); + + b.Property("Language") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("LastModifiedByUserId") + .HasColumnType("char(36)"); + + b.Property("Lead") + .HasMaxLength(1000) + .HasColumnType("varchar(1000)"); + + b.Property("Tagline") + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("varchar(1000)"); + + b.Property("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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Bytes") + .HasColumnType("bigint"); + + b.Property("CreatedAt") + .HasColumnType("datetime"); + + b.Property("CreatedByUserId") + .HasColumnType("char(36)"); + + b.Property("DeletedAt") + .HasColumnType("datetime"); + + b.Property("FocusX") + .HasColumnType("double") + .HasColumnName("DefaultFocusX"); + + b.Property("FocusY") + .HasColumnType("double") + .HasColumnName("DefaultFocusY"); + + b.Property("Height") + .HasColumnType("int"); + + b.Property("LastUsedAt") + .HasColumnType("datetime"); + + b.Property("Module") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(20) + .HasColumnType("varchar(20)") + .HasDefaultValue("hero"); + + b.Property("OriginalFileName") + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("OriginalKey") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("OriginalSha256") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("varchar(64)"); + + b.Property("SetHash") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("varchar(16)"); + + b.Property("SourceFormat") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("TallFocusX") + .HasColumnType("double"); + + b.Property("TallFocusY") + .HasColumnType("double"); + + b.Property("TallZoom") + .HasColumnType("double"); + + b.Property("Variants") + .IsRequired() + .HasColumnType("longtext"); + + b.Property("WideFocusX") + .HasColumnType("double"); + + b.Property("WideFocusY") + .HasColumnType("double"); + + b.Property("WideZoom") + .HasColumnType("double"); + + b.Property("Width") + .HasColumnType("int"); + + b.Property("Zoom") + .HasColumnType("double") + .HasColumnName("DefaultZoom"); + + b.HasKey("Id"); + + b.HasIndex("OriginalSha256") + .IsUnique(); + + b.ToTable("MediaAssets", null, t => + { + t.ExcludeFromMigrations(); + }); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Key") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("varchar(200)"); + + b.Property("Kind") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("varchar(10)"); + + b.Property("RetiredAt") + .HasColumnType("datetime"); + + b.HasKey("Id"); + + b.ToTable("HeroRetiredFiles", (string)null); + }); + + modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroSlide", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("char(36)"); + + b.Property("Alt") + .IsRequired() + .HasMaxLength(600) + .HasColumnType("varchar(600)"); + + b.Property("Decorative") + .HasColumnType("tinyint(1)"); + + b.Property("FocusX") + .HasColumnType("double"); + + b.Property("FocusY") + .HasColumnType("double"); + + b.Property("HeroId") + .HasColumnType("char(36)"); + + b.Property("ImageId") + .HasColumnType("char(36)"); + + b.Property("IsActive") + .HasColumnType("tinyint(1)"); + + b.Property("SetHash") + .HasMaxLength(16) + .HasColumnType("varchar(16)"); + + b.Property("SortOrder") + .HasColumnType("int"); + + b.Property("TallFocusX") + .HasColumnType("double"); + + b.Property("TallFocusY") + .HasColumnType("double"); + + b.Property("TallZoom") + .HasColumnType("double"); + + b.Property("Variants") + .HasColumnType("longtext"); + + b.Property("WideFocusX") + .HasColumnType("double"); + + b.Property("WideFocusY") + .HasColumnType("double"); + + b.Property("WideZoom") + .HasColumnType("double"); + + b.Property("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 + } + } +} diff --git a/src/SlpModularCms.Modules.Hero/Migrations/20261117231338_DropHeroImagesTable.cs b/src/SlpModularCms.Modules.Hero/Migrations/20261117231338_DropHeroImagesTable.cs new file mode 100644 index 0000000..eb01987 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero/Migrations/20261117231338_DropHeroImagesTable.cs @@ -0,0 +1,67 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SlpModularCms.Modules.Hero.Migrations +{ + /// + /// Phase 3c of the media library: the hero's photos live in the library's MediaAssets table, so HeroImages goes. + /// DESTRUCTIVE: the table and its rows are dropped. runs before this migration and + /// refuses to continue (the application does not start, nothing is dropped) unless every photo of HeroImages is in + /// MediaAssets with the same hash, set and variants. Down only brings back the EMPTY table; the data comes + /// back from the restore point (database dump) that is taken before this migration runs. + /// The model snapshot is the generated one (a slide's photo is a relation to the library table); the foreign key itself is + /// deliberately not created in the database (see DropSlideImageForeignKey): the photo lives in another module's table. + /// + public partial class DropHeroImagesTable : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable(name: "HeroImages"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "HeroImages", + columns: table => new + { + Id = table.Column(type: "char(36)", nullable: false), + OriginalSha256 = table.Column(type: "varchar(64)", maxLength: 64, nullable: false), + OriginalKey = table.Column(type: "varchar(200)", maxLength: 200, nullable: false), + SourceFormat = table.Column(type: "varchar(10)", maxLength: 10, nullable: false), + Width = table.Column(type: "int", nullable: false), + Height = table.Column(type: "int", nullable: false), + Bytes = table.Column(type: "bigint", nullable: false), + FocusX = table.Column(type: "double", nullable: false), + FocusY = table.Column(type: "double", nullable: false), + Zoom = table.Column(type: "double", nullable: false), + WideFocusX = table.Column(type: "double", nullable: true), + WideFocusY = table.Column(type: "double", nullable: true), + WideZoom = table.Column(type: "double", nullable: true), + TallFocusX = table.Column(type: "double", nullable: true), + TallFocusY = table.Column(type: "double", nullable: true), + TallZoom = table.Column(type: "double", nullable: true), + SetHash = table.Column(type: "varchar(16)", maxLength: 16, nullable: false), + Variants = table.Column(type: "longtext", nullable: false), + CreatedAt = table.Column(type: "datetime", nullable: false), + CreatedByUserId = table.Column(type: "char(36)", nullable: false), + OriginalFileName = table.Column(type: "varchar(200)", maxLength: 200, nullable: true), + LastUsedAt = table.Column(type: "datetime", nullable: true), + DeletedAt = table.Column(type: "datetime", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_HeroImages", x => x.Id); + }) + .Annotation("MySQL:Charset", "utf8mb4"); + + migrationBuilder.CreateIndex( + name: "IX_HeroImages_OriginalSha256", + table: "HeroImages", + column: "OriginalSha256", + unique: true); + } + } +} diff --git a/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs b/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs index 5803ae9..ecc15cc 100644 --- a/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs +++ b/src/SlpModularCms.Modules.Hero/Migrations/HeroDbContextModelSnapshot.cs @@ -97,10 +97,12 @@ namespace SlpModularCms.Modules.Hero.Migrations .HasColumnType("datetime"); b.Property("FocusX") - .HasColumnType("double"); + .HasColumnType("double") + .HasColumnName("DefaultFocusX"); b.Property("FocusY") - .HasColumnType("double"); + .HasColumnType("double") + .HasColumnName("DefaultFocusY"); b.Property("Height") .HasColumnType("int"); @@ -108,6 +110,13 @@ namespace SlpModularCms.Modules.Hero.Migrations b.Property("LastUsedAt") .HasColumnType("datetime"); + b.Property("Module") + .IsRequired() + .ValueGeneratedOnAdd() + .HasMaxLength(20) + .HasColumnType("varchar(20)") + .HasDefaultValue("hero"); + b.Property("OriginalFileName") .HasMaxLength(200) .HasColumnType("varchar(200)"); @@ -158,14 +167,18 @@ namespace SlpModularCms.Modules.Hero.Migrations .HasColumnType("int"); b.Property("Zoom") - .HasColumnType("double"); + .HasColumnType("double") + .HasColumnName("DefaultZoom"); b.HasKey("Id"); b.HasIndex("OriginalSha256") .IsUnique(); - b.ToTable("HeroImages", (string)null); + b.ToTable("MediaAssets", null, t => + { + t.ExcludeFromMigrations(); + }); }); modelBuilder.Entity("SlpModularCms.Modules.Hero.Data.Entities.HeroRetiredFile", b => diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs index 4eed113..9e97095 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroImageService.cs @@ -113,24 +113,8 @@ public class HeroImageService( ILogger logger, TimeProvider? time = null, IIndexHtmlCache? indexCache = null, - HeroRenderSignal? signal = null, - IMediaLibrarySync? librarySync = null, - IHeroLegacyImageMirror? legacyMirror = null) : IHeroImageService + HeroRenderSignal? signal = null) : IHeroImageService { - /// Mirrors the hero into the media library's tables (write-through, off unless switched on). Never throws. - 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; public async Task UploadAsync(Stream upload, long declaredLength, Guid callerId, CancellationToken cancellationToken, string? originalFileName = null) @@ -153,7 +137,6 @@ public class HeroImageService( if (existing is not null && await FilesExistAsync(existing, cancellationToken)) { await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); // The same photo again also gives up the failed state: the renderer tries it again when it is still pending. signal?.Reset(existing.Id); signal?.Notify(); @@ -193,7 +176,6 @@ public class HeroImageService( } await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); signal?.Notify(); logger.LogInformation("Hero image {ImageId} {Action} by user {UserId} ({Width}x{Height}); its variants are cut in the background", image.Id, existing is null ? "uploaded" : "repaired", callerId, image.Width, image.Height); @@ -240,7 +222,6 @@ public class HeroImageService( ApplyFocus(image, request); await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); signal?.Reset(imageId); signal?.Notify(); @@ -261,7 +242,6 @@ public class HeroImageService( Apply(request.Wide, v => (slide.WideFocusX, slide.WideFocusY, slide.WideZoom) = v); Apply(request.Tall, v => (slide.TallFocusX, slide.TallFocusY, slide.TallZoom) = v); await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); signal?.Reset(slideId); signal?.Notify(); @@ -285,7 +265,6 @@ public class HeroImageService( } await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); signal?.Reset(slideId); indexCache?.Invalidate(); @@ -475,7 +454,6 @@ public class HeroImageService( } await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); indexCache?.Invalidate(); logger.LogInformation("Hero slide {SlideId}: {Variants} variants cut in {Seconds:0.0} s", slide.Id, variants.Count, watch.Elapsed.TotalSeconds); return slide.Id; @@ -529,7 +507,6 @@ public class HeroImageService( } await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); indexCache?.Invalidate(); logger.LogInformation("Hero image {ImageId}: {Variants} variants cut in {Seconds:0.0} s", image.Id, variants.Count, watch.Elapsed.TotalSeconds); return image.Id; @@ -548,7 +525,6 @@ public class HeroImageService( repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = now }); repository.RemoveImage(image); await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); logger.LogInformation("Hero image {ImageId} removed by user {UserId}", imageId, callerId); } @@ -572,7 +548,6 @@ public class HeroImageService( image.DeletedAt = now; await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); indexCache?.Invalidate(); logger.LogInformation("Hero image {ImageId} moved to the trash by user {UserId} ({Slides} slides removed)", imageId, callerId, slides.Count); @@ -597,7 +572,6 @@ public class HeroImageService( var (setHash, originalKey) = (image.SetHash, image.OriginalKey); repository.RemoveImage(image); await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); if (setHash.Length > 0 && !setShared) { @@ -624,7 +598,6 @@ public class HeroImageService( image.DeletedAt = null; await repository.SaveChangesAsync(cancellationToken); - await SyncLibraryAsync(); signal?.Notify(); logger.LogInformation("Hero image {ImageId} restored from the trash by user {UserId}", imageId, callerId); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroLegacyImageMirror.cs b/src/SlpModularCms.Modules.Hero/Services/HeroLegacyImageMirror.cs deleted file mode 100644 index 9d66dc9..0000000 --- a/src/SlpModularCms.Modules.Hero/Services/HeroLegacyImageMirror.cs +++ /dev/null @@ -1,107 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; -using SlpModularCms.Modules.Hero.Data; -using SlpModularCms.Modules.Hero.Data.Entities; - -namespace SlpModularCms.Modules.Hero.Services; - -/// Keeps HeroImages a complete copy of the hero's photos while they live in the library's table (phase 3b). -public interface IHeroLegacyImageMirror -{ - /// Does nothing unless the photos live in the library. Never throws: the library rows are the truth then. - Task SyncAsync(CancellationToken cancellationToken); -} - -public sealed class HeroLegacyImageMirror(HeroDbContext db, ILogger 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; - } -} - -/// -/// Run at start-up when the photos are to live in the library: the library rows must already be a complete copy of -/// HeroImages (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. -/// -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(); - 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."); - } - } -} diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs index 0ddde60..024c6f6 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMediaCleanup.cs @@ -18,9 +18,7 @@ public class HeroMediaCleanup( IMediaStore store, IOptions options, ILogger logger, - TimeProvider? time = null, - SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, - IHeroLegacyImageMirror? legacyMirror = null) + TimeProvider? time = null) { private readonly TimeProvider _time = time ?? TimeProvider.System; @@ -44,25 +42,14 @@ public class HeroMediaCleanup( // 1b. Photos that have been in the trash for the trash period: their files go now (they are retired, immediately due). var trashCutoff = now.AddDays(-Math.Max(0, options.Value.TrashDays)); - var trashedGone = false; foreach (var image in await repository.GetTrashedBeforeAsync(trashCutoff, cancellationToken)) { - trashedGone = true; repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "set", Key = image.SetHash, RetiredAt = cutoff.AddSeconds(-1) }); repository.AddRetired(new HeroRetiredFile { Id = Guid.NewGuid(), Kind = "original", Key = image.OriginalKey, RetiredAt = cutoff.AddSeconds(-1) }); repository.RemoveImage(image); } 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); - } // 2. Retired files whose retention has passed, unless an image uses them again. var referencedSets = await repository.GetReferencedSetHashesAsync(cancellationToken); diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs deleted file mode 100644 index fd2adcc..0000000 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaMigrationSource.cs +++ /dev/null @@ -1,87 +0,0 @@ -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Hero.Data.Entities; -using SlpModularCms.Modules.Hero.Repositories; - -namespace SlpModularCms.Modules.Hero.Services; - -/// -/// Offers the hero's photos (also those in the trash) and slides to the media library's copy, with the same ids, hashes and -/// set hashes the hero uses. Read-only: the hero's tables are never changed by the copy. -/// -public class HeroMediaMigrationSource(IHeroRepository repository) : IMediaMigrationSource -{ - public string Module => HeroMapping.Area; - - public async Task> ListAssetsAsync(CancellationToken cancellationToken) - => (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).Select(ToSnapshot).ToList(); - - public async Task> ListUsesAsync(CancellationToken cancellationToken) - { - var images = (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).ToDictionary(i => i.Id); - var slides = await repository.GetAllSlidesReadOnlyAsync(cancellationToken); - var uses = new List(); - foreach (var hero in slides.GroupBy(s => s.HeroId)) - { - var ordered = hero.OrderBy(s => s.SortOrder).ThenBy(s => s.Id).ToList(); - for (var index = 0; index < ordered.Count; index++) - { - var slide = ordered[index]; - var setHash = string.Empty; - IReadOnlyList? crops = null; - if (images.TryGetValue(slide.ImageId, out var image)) - { - // The slide as it shows the photo: its own crop and set when it has them (crop per use), else the photo's. - slide.Image = image; - var shown = HeroMapping.Effective(slide) ?? image; - setHash = shown.SetHash; - if (slide.HasOwnCrop) - { - crops = [Crop("wide", shown), Crop("tall", shown)]; - } - } - - var variantsJson = slide.HasOwnCrop && slide.Variants is { Count: > 0 } ? Json(slide.Variants) : null; - uses.Add(new MediaUseSnapshot(slide.Id, slide.ImageId, Module, HeroMediaUsageProvider.SlideKind, index + 1, slide.IsActive, setHash, crops, variantsJson)); - } - } - - return uses; - } - - private MediaAssetSnapshot ToSnapshot(HeroImage i) - => new( - i.Id, - Module, - i.OriginalSha256, - i.OriginalKey, - i.SourceFormat, - i.Width, - i.Height, - i.Bytes, - i.FocusX, - i.FocusY, - i.Zoom, - i.SetHash, - [Crop("wide", i), Crop("tall", i)], - i.CreatedAt, - i.CreatedByUserId, - i.OriginalFileName, - i.LastUsedAt, - i.DeletedAt, - Json(i.Variants), - i.WideFocusX, - i.WideFocusY, - i.WideZoom, - i.TallFocusX, - i.TallFocusY, - i.TallZoom); - - /// The variants as the hero stores them (the same serializer as its database column), so they can go back unchanged. - public static string Json(IEnumerable variants) => System.Text.Json.JsonSerializer.Serialize(variants.ToList(), (System.Text.Json.JsonSerializerOptions?)null); - - private static MediaCropSnapshot Crop(string ratio, HeroImage image) - { - var focus = image.FocusFor(ratio); - return new MediaCropSnapshot(ratio, focus.X, focus.Y, focus.Zoom); - } -} diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs b/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs index e1137f7..86f96de 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroMediaUsageProvider.cs @@ -1,4 +1,3 @@ -using Microsoft.Extensions.Logging; using SlpModularCms.Core.Media; using SlpModularCms.Core.Media.Library; using SlpModularCms.Modules.Hero.Data.Entities; @@ -10,59 +9,21 @@ namespace SlpModularCms.Modules.Hero.Services; /// Tells the media library which hero images exist and where they are used (one use per slide, with its position in the /// hero), and moves them to the trash and back for it. The files are removed by after the trash period. /// -public class HeroMediaUsageProvider( - IHeroRepository repository, - IHeroImageService images, - IMediaLibraryReader? reader = null, - ILogger? logger = null) : IMediaUsageProvider, IMediaReadVerifier +public class HeroMediaUsageProvider(IHeroRepository repository, IHeroImageService images) : IMediaUsageProvider { public const string SlideKind = "slide"; public string Module => HeroMapping.Area; - private MediaReadMode Mode => reader?.Mode ?? MediaReadMode.Off; - public async Task> ListPhotosAsync(CancellationToken cancellationToken) - { - if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: false, cancellationToken) is { } fromLibrary) - { - return fromLibrary; - } - - await CompareInTheLogAsync(cancellationToken); - return (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); - } + => (await repository.GetAllImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); public async Task> ListTrashAsync(CancellationToken cancellationToken) - { - if (Mode == MediaReadMode.On && await TryReadPhotosAsync(trash: true, cancellationToken) is { } fromLibrary) - { - return fromLibrary; - } - - return (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); - } + => (await repository.GetTrashedImagesReadOnlyAsync(cancellationToken)).Select(ToPhoto).ToList(); public async Task> ListUsesAsync(IReadOnlyCollection photoIds, CancellationToken cancellationToken) { var wanted = photoIds.ToHashSet(); - if (Mode == MediaReadMode.On) - { - try - { - return (await reader!.ListUsesAsync(Module, cancellationToken)) - .Where(u => wanted.Contains(u.AssetId)) - .GroupBy(u => u.AssetId) - .SelectMany(g => g.OrderBy(u => u.Position)) - .Select(u => new MediaUse(Module, u.AssetId, u.PlaceKind, u.Position, u.Active)) - .ToList(); - } - catch (Exception exception) when (exception is not OperationCanceledException) - { - logger?.LogError(exception, "Reading the hero uses from the media library failed; the hero's own tables are used instead."); - } - } - var slides = await repository.GetAllSlidesReadOnlyAsync(cancellationToken); // The position of a slide is its place in its hero, in display order (1-based), also for slides that are off. @@ -83,64 +44,6 @@ public class HeroMediaUsageProvider( return uses; } - /// - /// The photos of the library tables (identity, size, name, dates), with the thumbnail from the hero (the variants are not in - /// the library tables until phase 3). Null when the library cannot be read: the hero's own tables are used then. - /// - private async Task?> TryReadPhotosAsync(bool trash, CancellationToken cancellationToken) - { - try - { - var assets = (await reader!.ListAssetsAsync(Module, cancellationToken)).Where(a => (a.DeletedAt is not null) == trash).ToList(); - var own = (await repository.GetAllImagesIncludingTrashedReadOnlyAsync(cancellationToken)).ToDictionary(i => i.Id); - var photos = assets.Select(a => new MediaPhoto( - Module, a.Id, a.Bytes, a.Width, a.Height, a.CreatedAt, own.TryGetValue(a.Id, out var image) ? Thumbnail(image) : null, a.OriginalFileName, a.LastUsedAt, a.DeletedAt)); - return (trash ? photos.OrderByDescending(p => p.DeletedAt) : photos.OrderByDescending(p => p.CreatedAt)).ToList(); - } - catch (Exception exception) when (exception is not OperationCanceledException) - { - logger?.LogError(exception, "Reading the hero photos from the media library failed; the hero's own tables are used instead."); - return null; - } - } - - /// In compare mode: every difference between the hero's tables and the library tables is a warning in the log. - private async Task CompareInTheLogAsync(CancellationToken cancellationToken) - { - if (Mode != MediaReadMode.Compare) - { - return; - } - - try - { - var check = await CheckAsync(cancellationToken); - foreach (var difference in check.Differences.Take(20)) - { - logger?.LogWarning("Media library read check (hero): {Difference}", difference); - } - - if (check.Differences.Count > 20) - { - logger?.LogWarning("Media library read check (hero): {More} more differences not listed.", check.Differences.Count - 20); - } - } - catch (Exception exception) when (exception is not OperationCanceledException) - { - logger?.LogError(exception, "The media library read check of the hero failed."); - } - } - - public async Task CheckAsync(CancellationToken cancellationToken) - { - var source = new HeroMediaMigrationSource(repository); - var ownAssets = await source.ListAssetsAsync(cancellationToken); - var ownUses = await source.ListUsesAsync(cancellationToken); - var libraryAssets = await reader!.ListAssetsAsync(Module, cancellationToken); - var libraryUses = await reader.ListUsesAsync(Module, cancellationToken); - return new MediaReadCheck(Module, Mode, ownAssets.Count, ownUses.Count, MediaReadComparer.Compare(ownAssets, ownUses, libraryAssets, libraryUses)); - } - public async Task TrashAsync(Guid photoId, Guid callerId, CancellationToken cancellationToken) { try diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs index 5245edf..5b89b24 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroService.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroService.cs @@ -30,9 +30,7 @@ public class HeroService( ILogger logger, TimeProvider? time = null, IIndexHtmlCache? indexCache = null, - ITextCatalog? texts = null, - SlpModularCms.Core.Media.Library.IMediaLibrarySync? librarySync = null, - IHeroLegacyImageMirror? legacyMirror = null) : IHeroService + ITextCatalog? texts = null) : IHeroService { private readonly TimeProvider _time = time ?? TimeProvider.System; private readonly ITextCatalog _texts = texts ?? TextCatalog.Default; @@ -157,15 +155,6 @@ 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); - } logger.LogInformation("Hero {HeroId} {Action} by user {LastModifiedByUserId} ({Slides} slides)", hero.Id, created ? "Created" : "Updated", callerId, keep.Count); diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaAssetsTableTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaAssetsTableTests.cs new file mode 100644 index 0000000..1166186 --- /dev/null +++ b/src/SlpModularCms.Modules.Media.Tests/MediaAssetsTableTests.cs @@ -0,0 +1,62 @@ +using FluentAssertions; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Migrations; +using MySql.Data.MySqlClient; +using SlpModularCms.Core.Hosting; +using SlpModularCms.Modules.Media.Data; + +namespace SlpModularCms.Modules.Media.Tests; + +/// The library's photo table against a real MariaDB (runs when ConnectionStrings__DefaultConnection is set; CI sets it). +public class MediaAssetsTableTests +{ + [Fact] + public async Task The_migrations_create_the_photo_table_with_everything_the_hero_needs_to_publish() + { + var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection"); + if (string.IsNullOrWhiteSpace(baseString)) + { + return; + } + + var connectionString = new MySqlConnectionStringBuilder(baseString) { Database = "media_table_test_" + Guid.NewGuid().ToString("N")[..12] }.ConnectionString; + try + { + await using var db = new MediaDbContext(new DbContextOptionsBuilder().UseMySQL(connectionString).ReplaceService().Options); + await db.Database.MigrateAsync(); + var id = Guid.NewGuid(); + db.MediaAssets.Add(new Data.Entities.MediaAsset + { + Id = id, + Module = "hero", + OriginalSha256 = new string('a', 64), + OriginalKey = "originals/a.jpg", + SourceFormat = "jpg", + Width = 10, + Height = 10, + Bytes = 10, + CreatedAt = DateTimeOffset.UtcNow, + CreatedByUserId = Guid.NewGuid(), + SetHash = "0123456789abcdef", + Variants = "[]", + WideFocusX = 0.2, + TallZoom = 1.3, + }); + await db.SaveChangesAsync(); + + var row = await db.MediaAssets.AsNoTracking().SingleAsync(a => a.Id == id); + (row.Module, row.SetHash, row.Variants, row.WideFocusX, row.TallZoom).Should().Be(("hero", "0123456789abcdef", "[]", 0.2, 1.3)); + } + finally + { + 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(); + } + } +} diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs deleted file mode 100644 index 234415f..0000000 --- a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryCopierTests.cs +++ /dev/null @@ -1,130 +0,0 @@ -using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging.Abstractions; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Media.Data; -using SlpModularCms.Modules.Media.Services; - -namespace SlpModularCms.Modules.Media.Tests; - -public class MediaLibraryCopierTests : IDisposable -{ - private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); - private readonly MediaDbContext _db = new(new DbContextOptionsBuilder().UseInMemoryDatabase("media-" + Guid.NewGuid()).Options); - - public void Dispose() => _db.Dispose(); - - private sealed class Source(string module, List assets, List uses) : IMediaMigrationSource - { - public string Module => module; - - public Task> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult>(assets.ToList()); - - public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses.ToList()); - } - - private static MediaAssetSnapshot Asset(string module = "hero", string? sha = null, string setHash = "0123456789abcdef", DateTimeOffset? deleted = null) - => new(Guid.NewGuid(), module, sha ?? Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.4, 0.6, 1.5, setHash, - [new MediaCropSnapshot("wide", 0.4, 0.6, 1.5), new MediaCropSnapshot("tall", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", Now.AddDays(-2), deleted); - - private static MediaUseSnapshot Use(MediaAssetSnapshot asset, int position = 1, bool active = true) - => new(Guid.NewGuid(), asset.Id, asset.Module, "slide", position, active, asset.SetHash); - - private MediaLibraryCopier Copier(params IMediaMigrationSource[] sources) => new(_db, sources, NullLogger.Instance); - - [Fact] - public async Task Copies_the_photos_and_uses_with_the_same_ids_hashes_and_set_hashes() - { - var a = Asset(); - var b = Asset(deleted: Now.AddDays(-1)); - var use = Use(a, 3, active: false); - - var report = await Copier(new Source("hero", [a, b], [use])).CopyAsync(CancellationToken.None); - - report.Should().Match(r => r.Assets == 2 && r.Uses == 1 && r.Verified); - var copy = await _db.MediaAssets.Include(x => x.Uses).SingleAsync(x => x.Id == a.Id); - copy.OriginalSha256.Should().Be(a.OriginalSha256); - copy.OriginalKey.Should().Be(a.OriginalKey); - copy.OriginalFileName.Should().Be("zomer.jpg"); - copy.LastUsedAt.Should().Be(a.LastUsedAt); - copy.DefaultFocusX.Should().Be(0.4); - copy.Uses.Should().ContainSingle().Which.Should().Match(u => u.Id == use.Id && u.SetHash == a.SetHash && u.Position == 3 && !u.Active && u.PlaceKind == "slide"); - copy.Uses[0].Crops.Should().Contain("wide").And.Contain("0.4"); - (await _db.MediaAssets.SingleAsync(x => x.Id == b.Id)).DeletedAt.Should().Be(b.DeletedAt); - } - - [Fact] - public async Task Copying_twice_gives_the_same_result() - { - var a = Asset(); - var copier = Copier(new Source("hero", [a], [Use(a)])); - - await copier.CopyAsync(CancellationToken.None); - var second = await copier.CopyAsync(CancellationToken.None); - - second.Should().Match(r => r.Assets == 1 && r.Uses == 1 && r.Verified); - (await _db.MediaAssets.CountAsync()).Should().Be(1); - (await _db.MediaUses.CountAsync()).Should().Be(1); - } - - [Fact] - public async Task The_copy_mirrors_the_source_a_changed_photo_is_updated_and_a_removed_one_leaves() - { - var a = Asset(); - var gone = Asset(); - var assets = new List { a, gone }; - var uses = new List { Use(a), Use(gone) }; - var copier = Copier(new Source("hero", assets, uses)); - await copier.CopyAsync(CancellationToken.None); - - assets.Remove(gone); - uses.RemoveAll(u => u.AssetId == gone.Id); - assets[0] = a with { SetHash = "fedcba9876543210", OriginalFileName = "nieuw.jpg" }; - uses[0] = uses[0] with { SetHash = "fedcba9876543210" }; - await copier.CopyAsync(CancellationToken.None); - - (await _db.MediaAssets.ToListAsync()).Should().ContainSingle().Which.OriginalFileName.Should().Be("nieuw.jpg"); - (await _db.MediaUses.ToListAsync()).Should().ContainSingle().Which.SetHash.Should().Be("fedcba9876543210"); - } - - [Fact] - public async Task Photos_of_a_module_without_a_source_are_left_alone() - { - var other = Asset(module: "gallery"); - _db.MediaAssets.Add(new Data.Entities.MediaAsset { Id = other.Id, Module = "gallery", OriginalSha256 = other.OriginalSha256, OriginalKey = "k", SourceFormat = "jpg" }); - await _db.SaveChangesAsync(); - var a = Asset(); - - await Copier(new Source("hero", [a], [])).CopyAsync(CancellationToken.None); - - (await _db.MediaAssets.CountAsync()).Should().Be(2); - (await _db.MediaAssets.AnyAsync(x => x.Id == other.Id)).Should().BeTrue(); - } - - [Fact] - public async Task Two_sources_are_copied_together() - { - var hero = Asset(); - var gallery = Asset(module: "gallery"); - - var report = await Copier(new Source("hero", [hero], [Use(hero)]), new Source("gallery", [gallery], [])).CopyAsync(CancellationToken.None); - - report.Assets.Should().Be(2); - report.Uses.Should().Be(1); - } - [Fact] - public async Task Copies_and_verifies_the_variants_the_set_hash_and_the_crop_overrides_of_a_photo_and_of_a_use() - { - var variants = "[{\"Ratio\":\"wide\",\"Width\":480,\"Height\":270,\"Type\":\"image/webp\",\"FileName\":\"wide-480.webp\",\"Bytes\":1234}]"; - var a = Asset() with { VariantsJson = variants, WideFocusX = 0.1, WideFocusY = 0.2, WideZoom = 1.3, TallFocusX = 0.7 }; - var use = Use(a) with { VariantsJson = variants, SetHash = "fedcba9876543210" }; - - await Copier(new Source("hero", [a], [use])).CopyAsync(CancellationToken.None); - - var photo = await _db.MediaAssets.SingleAsync(); - photo.Variants.Should().Be(variants); - photo.SetHash.Should().Be(a.SetHash); - (photo.WideFocusX, photo.WideFocusY, photo.WideZoom, photo.TallFocusX, photo.TallFocusY).Should().Be((0.1, 0.2, 1.3, 0.7, null)); - (await _db.MediaUses.SingleAsync()).Variants.Should().Be(variants); - } -} diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryMariaDbTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryMariaDbTests.cs deleted file mode 100644 index 85c9212..0000000 --- a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryMariaDbTests.cs +++ /dev/null @@ -1,113 +0,0 @@ -using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Migrations; -using SlpModularCms.Core.Hosting; -using Microsoft.Extensions.Logging.Abstractions; -using MySql.Data.MySqlClient; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Media.Data; -using SlpModularCms.Modules.Media.Services; - -namespace SlpModularCms.Modules.Media.Tests; - -/// -/// The copy against a real MariaDB in a database of its own (created and dropped here): the migration, the foreign key and the -/// rollback of a failed copy. Runs when ConnectionStrings__DefaultConnection is set (CI sets it); otherwise it does nothing. -/// -public class MediaLibraryMariaDbTests -{ - private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); - - private static string? ConnectionString() - { - var baseString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection"); - if (string.IsNullOrWhiteSpace(baseString)) - { - return null; - } - - var builder = new MySqlConnectionStringBuilder(baseString) { Database = "media_library_test_" + Guid.NewGuid().ToString("N")[..12] }; - return builder.ConnectionString; - } - - private static MediaDbContext Open(string connectionString) - => new(new DbContextOptionsBuilder().UseMySQL(connectionString).ReplaceService().Options); - - private sealed class Source(List assets, List uses) : IMediaMigrationSource - { - public string Module => "hero"; - - public Task> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult>(assets); - - public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses); - } - - private static MediaAssetSnapshot Asset(string sha) - => new(Guid.NewGuid(), "hero", sha, "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.4, 0.6, 1.5, "0123456789abcdef", - [new MediaCropSnapshot("wide", 0.4, 0.6, 1.5)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", Now.AddDays(-2), null); - - [Fact] - public async Task The_migration_creates_the_tables_and_a_verified_copy_survives_a_second_run() - { - var connection = ConnectionString(); - if (connection is null) - { - return; - } - - await using var db = Open(connection); - try - { - await db.Database.MigrateAsync(); - var a = Asset(new string('a', 64)); - var b = Asset(new string('b', 64)); - var uses = new List { new(Guid.NewGuid(), a.Id, "hero", "slide", 1, true, a.SetHash) }; - var copier = new MediaLibraryCopier(db, [new Source([a, b], uses)], NullLogger.Instance); - - var first = await copier.CopyAsync(CancellationToken.None); - var second = await copier.CopyAsync(CancellationToken.None); - - first.Should().Match(r => r.Assets == 2 && r.Uses == 1 && r.Verified); - second.Verified.Should().BeTrue(); - (await db.MediaAssets.CountAsync()).Should().Be(2); - (await db.MediaUses.CountAsync()).Should().Be(1); - } - finally - { - await db.Database.EnsureDeletedAsync(); - } - } - - [Fact] - public async Task A_failed_copy_is_rolled_back_completely() - { - var connection = ConnectionString(); - if (connection is null) - { - return; - } - - await using var db = Open(connection); - try - { - await db.Database.MigrateAsync(); - var good = Asset(new string('c', 64)); - await new MediaLibraryCopier(db, [new Source([good], [])], NullLogger.Instance).CopyAsync(CancellationToken.None); - - // Two photos with the same hash break the unique index: the whole copy must be undone, the earlier copy stays. - var one = Asset(new string('d', 64)); - var two = Asset(new string('d', 64)); - var failing = new MediaLibraryCopier(db, [new Source([good, one, two], [])], NullLogger.Instance); - - var act = () => failing.CopyAsync(CancellationToken.None); - - await act.Should().ThrowAsync(); - await using var check = Open(connection); - (await check.MediaAssets.Select(x => x.Id).ToListAsync()).Should().Equal(good.Id); - } - finally - { - await db.Database.EnsureDeletedAsync(); - } - } -} diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryReaderTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibraryReaderTests.cs deleted file mode 100644 index 8a71e44..0000000 --- a/src/SlpModularCms.Modules.Media.Tests/MediaLibraryReaderTests.cs +++ /dev/null @@ -1,85 +0,0 @@ -using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging.Abstractions; -using Microsoft.Extensions.Options; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Media.Data; -using SlpModularCms.Modules.Media.Services; - -namespace SlpModularCms.Modules.Media.Tests; - -/// Phase 2, step C: reading the library tables back, per module, and the mode from the setting. -public class MediaLibraryReaderTests : IDisposable -{ - private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); - private readonly MediaDbContext _db = new(new DbContextOptionsBuilder().UseInMemoryDatabase("media-read-" + Guid.NewGuid()).Options); - - public void Dispose() => _db.Dispose(); - - private sealed class Source(string module, List assets, List uses) : IMediaMigrationSource - { - public string Module => module; - - public Task> ListAssetsAsync(CancellationToken cancellationToken) => Task.FromResult>(assets); - - public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses); - } - - private static MediaAssetSnapshot Asset(string module) - => new(Guid.NewGuid(), module, Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.5, 0.5, 1, "0123456789abcdef", - [new MediaCropSnapshot("wide", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", Now.AddDays(-2), null); - - [Fact] - public async Task Reads_back_what_was_mirrored_for_one_module_and_agrees_with_the_comparer() - { - var hero = Asset("hero"); - var other = Asset("gallery"); - var use = new MediaUseSnapshot(Guid.NewGuid(), hero.Id, "hero", "slide", 2, false, "fedcba9876543210"); - await new MediaLibraryCopier(_db, [new Source("hero", [hero], [use]), new Source("gallery", [other], [])], NullLogger.Instance).CopyAsync(CancellationToken.None); - var reader = new MediaLibraryReader(_db, Options.Create(new MediaLibraryOptions { ReadFromLibrary = MediaReadMode.Compare })); - - var assets = await reader.ListAssetsAsync("hero", CancellationToken.None); - var uses = await reader.ListUsesAsync("hero", CancellationToken.None); - - reader.Mode.Should().Be(MediaReadMode.Compare); - assets.Should().ContainSingle().Which.Should().Match(a => a.Id == hero.Id && a.OriginalFileName == "zomer.jpg" && a.Bytes == 2_000_000); - uses.Should().ContainSingle().Which.Should().Match(u => u.Id == use.Id && u.Position == 2 && !u.Active && u.SetHash == "fedcba9876543210"); - MediaReadComparer.Compare([hero], [use], assets, uses).Should().BeEmpty(); - } - - [Fact] - public void The_mode_is_off_by_default() - => new MediaLibraryReader(_db, Options.Create(new MediaLibraryOptions())).Mode.Should().Be(MediaReadMode.Off); - - [Fact] - public void The_comparer_reports_missing_extra_and_changed_rows() - { - var a = Asset("hero"); - var b = Asset("hero"); - var useA = new MediaUseSnapshot(Guid.NewGuid(), a.Id, "hero", "slide", 1, true, "aaaa"); - var ghost = new MediaUseSnapshot(Guid.NewGuid(), a.Id, "hero", "slide", 2, true, "aaaa"); - - var differences = MediaReadComparer.Compare([a, b], [useA], [a with { Bytes = 5 }, Asset("hero")], [useA with { Active = false }, ghost]); - - differences.Should().Contain(d => d.Contains(b.Id.ToString()) && d.Contains("missing in the library")); - differences.Should().Contain(d => d.Contains(a.Id.ToString()) && d.Contains("hash, key or size")); - differences.Should().Contain(d => d.Contains("in the library but not in the module")); - differences.Should().Contain(d => d.StartsWith("use ") && d.Contains("differs")); - differences.Should().Contain(d => d.Contains(ghost.Id.ToString())); - } - [Fact] - public async Task Reads_back_the_variants_and_set_hash_and_the_comparer_notices_a_difference_in_them() - { - var hero = Asset("hero") with { VariantsJson = "[{\"Ratio\":\"wide\"}]", WideFocusX = 0.3 }; - await new MediaLibraryCopier(_db, [new Source("hero", [hero], [])], NullLogger.Instance).CopyAsync(CancellationToken.None); - var reader = new MediaLibraryReader(_db, Options.Create(new MediaLibraryOptions())); - - var back = (await reader.ListAssetsAsync("hero", CancellationToken.None)).Single(); - - back.VariantsJson.Should().Be(hero.VariantsJson); - back.SetHash.Should().Be(hero.SetHash); - back.WideFocusX.Should().Be(0.3); - MediaReadComparer.Compare([hero], [], [back], []).Should().BeEmpty(); - MediaReadComparer.Compare([hero with { VariantsJson = "[]" }], [], [back], []).Should().Contain(d => d.Contains("set hash or variants differ")); - } -} diff --git a/src/SlpModularCms.Modules.Media.Tests/MediaLibrarySyncTests.cs b/src/SlpModularCms.Modules.Media.Tests/MediaLibrarySyncTests.cs deleted file mode 100644 index a2e2b6a..0000000 --- a/src/SlpModularCms.Modules.Media.Tests/MediaLibrarySyncTests.cs +++ /dev/null @@ -1,85 +0,0 @@ -using FluentAssertions; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging.Abstractions; -using Microsoft.Extensions.Options; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Media.Data; -using SlpModularCms.Modules.Media.Services; - -namespace SlpModularCms.Modules.Media.Tests; - -/// Phase 2, step A: the write-through mirrors one module into the library tables, only when it is switched on, and never throws. -public class MediaLibrarySyncTests : IDisposable -{ - private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); - private readonly MediaDbContext _db = new(new DbContextOptionsBuilder().UseInMemoryDatabase("media-sync-" + Guid.NewGuid()).Options); - - public void Dispose() => _db.Dispose(); - - private sealed class Source(string module, Func> assets, Func> uses, bool fails = false) : IMediaMigrationSource - { - public string Module => module; - - public Task> ListAssetsAsync(CancellationToken cancellationToken) - => fails ? throw new InvalidOperationException("source failed") : Task.FromResult>(assets()); - - public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses()); - } - - private static MediaAssetSnapshot Asset(string module) - => new(Guid.NewGuid(), module, Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.5, 0.5, 1, "0123456789abcdef", - [new MediaCropSnapshot("wide", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", null, null); - - private MediaLibrarySync Sync(bool on, params IMediaMigrationSource[] sources) - => new(new MediaLibraryCopier(_db, sources, NullLogger.Instance), Options.Create(new MediaLibraryOptions { DualWrite = on }), NullLogger.Instance); - - [Fact] - public async Task Does_nothing_while_the_switch_is_off() - { - var a = Asset("hero"); - - await Sync(false, new Source("hero", () => [a], () => [])).SyncAsync("hero", CancellationToken.None); - - (await _db.MediaAssets.CountAsync()).Should().Be(0); - } - - [Fact] - public async Task Mirrors_only_the_given_module_and_follows_the_changes_of_the_source() - { - var hero = Asset("hero"); - var other = Asset("gallery"); - var heroAssets = new List { hero }; - var sync = Sync(true, new Source("hero", () => heroAssets.ToList(), () => []), new Source("gallery", () => [other], () => [])); - - await sync.SyncAsync("hero", CancellationToken.None); - (await _db.MediaAssets.Select(a => a.Module).ToListAsync()).Should().Equal("hero"); - - var second = Asset("hero"); - heroAssets.Add(second); - heroAssets.Remove(hero); - await sync.SyncAsync("hero", CancellationToken.None); - - (await _db.MediaAssets.Select(a => a.Id).ToListAsync()).Should().Equal(second.Id); - } - - [Fact] - public async Task Stores_the_crops_of_a_use_when_the_source_gives_them() - { - var hero = Asset("hero"); - var use = new MediaUseSnapshot(Guid.NewGuid(), hero.Id, "hero", "slide", 1, true, "fedcba9876543210", [new MediaCropSnapshot("wide", 0.1, 0.9, 1.4)]); - - await Sync(true, new Source("hero", () => [hero], () => [use])).SyncAsync("hero", CancellationToken.None); - - var row = await _db.MediaUses.SingleAsync(); - row.SetHash.Should().Be("fedcba9876543210"); - row.Crops.Should().Contain("0.9").And.Contain("1.4"); - } - - [Fact] - public async Task Never_throws_when_the_source_fails() - { - var act = () => Sync(true, new Source("hero", () => [], () => [], fails: true)).SyncAsync("hero", CancellationToken.None); - - await act.Should().NotThrowAsync(); - } -} diff --git a/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs b/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs deleted file mode 100644 index aaa7826..0000000 --- a/src/SlpModularCms.Modules.Media/MediaLibraryOptions.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace SlpModularCms.Modules.Media; - -/// Settings of the library's own tables (Media:Library:*). Everything is off by default: a deploy of the code alone only adds the empty tables. -public class MediaLibraryOptions -{ - public const string SectionName = "Media:Library"; - - /// - /// Copy the photos and uses of the modules into the library's tables once at start-up and verify the copy - /// (Media__Library__CopyOnStartup=true). Safe to repeat: the copy mirrors the source. Never touches the modules' own data. - /// - public bool CopyOnStartup { get; set; } - - /// - /// Write-through (phase 2, step A, Media__Library__DualWrite=true): after every change a module saved to its own tables, - /// the library tables are mirrored too. Off by default; the modules' own tables stay the truth. - /// - public bool DualWrite { get; set; } - - /// - /// Where the library screen reads the photos from (phase 2, step C, Media__Library__ReadFromLibrary): Off (the - /// module's own tables, the default), Compare (still those, but differences with the library tables are logged and - /// shown by GET media-library/read-check) or On (the library tables; needs DualWrite). - /// - public SlpModularCms.Core.Media.Library.MediaReadMode ReadFromLibrary { get; set; } = SlpModularCms.Core.Media.Library.MediaReadMode.Off; -} diff --git a/src/SlpModularCms.Modules.Media/MediaModule.cs b/src/SlpModularCms.Modules.Media/MediaModule.cs index 3797c46..d6d37c4 100644 --- a/src/SlpModularCms.Modules.Media/MediaModule.cs +++ b/src/SlpModularCms.Modules.Media/MediaModule.cs @@ -4,21 +4,16 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using SlpModularCms.Core.Media.Library; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using SlpModularCms.Core.Hosting; using SlpModularCms.Core.Modules; using SlpModularCms.Modules.Media.Data; -using SlpModularCms.Modules.Media.Services; namespace SlpModularCms.Modules.Media; /// -/// The library's own tables (photos and where they are used). Phase 1 of the migration plan: only the tables, and a copy -/// of the modules' photos behind Media:Library:CopyOnStartup (off by default). Nothing reads from these tables yet. +/// The library's own table of photos (MediaAssets, with the module that owns each photo). Since phase 3 of the media +/// library it is where the hero keeps its photos; the screen, the trash and the clean-up work through the modules' providers. +/// The module owns the table and its migrations. /// [ExcludeFromCodeCoverage] public class MediaModule : IModule @@ -34,14 +29,6 @@ public class MediaModule : IModule options.UseMySQL(config.GetConnectionString("DefaultConnection")!); options.ReplaceService(); }); - - services.AddOptions().Configure((options, configuration) => - configuration.GetSection(MediaLibraryOptions.SectionName).Bind(options)); - services.AddScoped(); - services.Replace(ServiceDescriptor.Scoped()); - services.Replace(ServiceDescriptor.Scoped()); - services.Replace(ServiceDescriptor.Singleton()); - services.AddHostedService(); } public void UseModule(IApplicationBuilder app) @@ -51,27 +38,3 @@ public class MediaModule : IModule db.Database.Migrate(); } } - -/// Runs the verified copy once at start-up when Media:Library:CopyOnStartup is on; a failure is logged and never stops the host. -public sealed class MediaLibraryCopyService(IServiceScopeFactory scopes, IOptions options, ILogger logger) : IHostedService -{ - public async Task StartAsync(CancellationToken cancellationToken) - { - if (!options.Value.CopyOnStartup) - { - return; - } - - try - { - using var scope = scopes.CreateScope(); - await scope.ServiceProvider.GetRequiredService().CopyAsync(cancellationToken); - } - catch (Exception exception) when (exception is not OperationCanceledException) - { - logger.LogError(exception, "The media library copy failed and was rolled back; the modules' own data is untouched."); - } - } - - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; -} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs deleted file mode 100644 index 418dfca..0000000 --- a/src/SlpModularCms.Modules.Media/Services/MediaLibraryCopier.cs +++ /dev/null @@ -1,214 +0,0 @@ -using System.Text.Json; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Media.Data; -using SlpModularCms.Modules.Media.Data.Entities; - -namespace SlpModularCms.Modules.Media.Services; - -/// What a copy did and what the verification found. -public sealed record MediaCopyReport(int Assets, int Uses, IReadOnlyList Differences) -{ - public bool Verified => Differences.Count == 0; -} - -public sealed class MediaCopyVerificationException(MediaCopyReport report) - : Exception($"The copy of the media library does not match its source: {string.Join("; ", report.Differences.Take(5))}") -{ - public MediaCopyReport Report { get; } = report; -} - -/// -/// Copies the photos and uses of every module () into the library's own tables and -/// verifies the copy against the source (migration plan, decision 13). The copy MIRRORS the source: re-running it gives -/// the same result, rows of a module that no longer exist at the source are removed from the copy. The modules' own data -/// is never changed. A difference after copying rolls the whole copy back and raises . -/// -public sealed class MediaLibraryCopier(MediaDbContext db, IEnumerable sources, ILogger logger) -{ - private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web); - - /// Mirror only this module (the write-through); null = every module (the verified copy). - public async Task CopyAsync(CancellationToken cancellationToken, string? onlyModule = null) - { - var assets = new List(); - var uses = new List(); - var modules = new List(); - foreach (var source in sources.Where(s => onlyModule is null || s.Module == onlyModule)) - { - modules.Add(source.Module); - assets.AddRange(await source.ListAssetsAsync(cancellationToken)); - uses.AddRange(await source.ListUsesAsync(cancellationToken)); - } - - var transaction = db.Database.IsRelational() ? await db.Database.BeginTransactionAsync(cancellationToken) : null; - try - { - await MirrorAsync(assets, uses, modules, cancellationToken); - await db.SaveChangesAsync(cancellationToken); - - var report = await VerifyAsync(assets, uses, modules, cancellationToken); - if (!report.Verified) - { - throw new MediaCopyVerificationException(report); - } - - if (transaction is not null) - { - await transaction.CommitAsync(cancellationToken); - } - - logger.LogInformation("Media library copy verified: {Assets} photos and {Uses} uses from {Modules}.", report.Assets, report.Uses, string.Join(", ", modules)); - return report; - } - catch - { - if (transaction is not null) - { - await transaction.RollbackAsync(CancellationToken.None); - } - - db.ChangeTracker.Clear(); - throw; - } - finally - { - if (transaction is not null) - { - await transaction.DisposeAsync(); - } - } - } - - private async Task MirrorAsync(IReadOnlyList assets, IReadOnlyList uses, IReadOnlyList modules, CancellationToken cancellationToken) - { - var existingAssets = new Dictionary(); - var existingUses = new Dictionary(); - foreach (var module in modules) - { - // One equality query per module: the MySQL provider cannot translate a list parameter in an IN clause. - foreach (var asset in await db.MediaAssets.Where(a => a.Module == module).ToListAsync(cancellationToken)) - { - existingAssets[asset.Id] = asset; - } - - foreach (var use in await db.MediaUses.Where(u => u.Module == module).ToListAsync(cancellationToken)) - { - existingUses[use.Id] = use; - } - } - - foreach (var snapshot in assets) - { - if (!existingAssets.Remove(snapshot.Id, out var row)) - { - row = new MediaAsset { Id = snapshot.Id }; - db.MediaAssets.Add(row); - } - - row.Module = snapshot.Module; - row.OriginalSha256 = snapshot.OriginalSha256; - row.OriginalKey = snapshot.OriginalKey; - row.SourceFormat = snapshot.SourceFormat; - row.Width = snapshot.Width; - row.Height = snapshot.Height; - row.Bytes = snapshot.Bytes; - row.DefaultFocusX = snapshot.FocusX; - row.DefaultFocusY = snapshot.FocusY; - row.DefaultZoom = snapshot.Zoom; - row.CreatedAt = snapshot.CreatedAt; - row.CreatedByUserId = snapshot.CreatedByUserId; - row.OriginalFileName = snapshot.OriginalFileName; - row.LastUsedAt = snapshot.LastUsedAt; - row.DeletedAt = snapshot.DeletedAt; - row.SetHash = snapshot.SetHash; - row.Variants = snapshot.VariantsJson; - (row.WideFocusX, row.WideFocusY, row.WideZoom) = (snapshot.WideFocusX, snapshot.WideFocusY, snapshot.WideZoom); - (row.TallFocusX, row.TallFocusY, row.TallZoom) = (snapshot.TallFocusX, snapshot.TallFocusY, snapshot.TallZoom); - } - - // Uses reference their asset; the crops of a use are the crops of its photo (the hero's wide and tall). - var cropsByAsset = assets.ToDictionary(a => a.Id, a => JsonSerializer.Serialize(a.Crops, Json)); - foreach (var snapshot in uses) - { - if (!existingUses.Remove(snapshot.Id, out var row)) - { - row = new MediaAssetUse { Id = snapshot.Id }; - db.MediaUses.Add(row); - } - - row.AssetId = snapshot.AssetId; - row.Module = snapshot.Module; - row.PlaceKind = snapshot.PlaceKind; - row.Position = snapshot.Position; - row.Active = snapshot.Active; - row.SetHash = snapshot.SetHash; - row.Variants = snapshot.VariantsJson; - row.Crops = snapshot.Crops is not null ? JsonSerializer.Serialize(snapshot.Crops, Json) : cropsByAsset.TryGetValue(snapshot.AssetId, out var crops) ? crops : "[]"; - } - - // What is left no longer exists at the source. - db.MediaUses.RemoveRange(existingUses.Values); - db.MediaAssets.RemoveRange(existingAssets.Values); - } - - private async Task VerifyAsync(IReadOnlyList assets, IReadOnlyList uses, IReadOnlyList modules, CancellationToken cancellationToken) - { - var differences = new List(); - var copiedAssets = new Dictionary(); - var copiedUses = new Dictionary(); - foreach (var module in modules) - { - foreach (var asset in await db.MediaAssets.AsNoTracking().Where(a => a.Module == module).ToListAsync(cancellationToken)) - { - copiedAssets[asset.Id] = asset; - } - - foreach (var use in await db.MediaUses.AsNoTracking().Where(u => u.Module == module).ToListAsync(cancellationToken)) - { - copiedUses[use.Id] = use; - } - } - - if (copiedAssets.Count != assets.Count) - { - differences.Add($"photos: source {assets.Count}, copy {copiedAssets.Count}"); - } - - if (copiedUses.Count != uses.Count) - { - differences.Add($"uses: source {uses.Count}, copy {copiedUses.Count}"); - } - - foreach (var asset in assets) - { - if (!copiedAssets.TryGetValue(asset.Id, out var copy)) - { - differences.Add($"photo {asset.Id} is missing in the copy"); - } - else if (copy.OriginalSha256 != asset.OriginalSha256 || copy.OriginalKey != asset.OriginalKey || copy.Bytes != asset.Bytes) - { - differences.Add($"photo {asset.Id}: hash, key or size differs"); - } - else if (copy.SetHash != asset.SetHash || copy.Variants != asset.VariantsJson) - { - differences.Add($"photo {asset.Id}: set hash or variants differ"); - } - } - - foreach (var use in uses) - { - if (!copiedUses.TryGetValue(use.Id, out var copy)) - { - differences.Add($"use {use.Id} is missing in the copy"); - } - else if (copy.SetHash != use.SetHash || copy.AssetId != use.AssetId || copy.Variants != use.VariantsJson) - { - differences.Add($"use {use.Id}: set hash, variants or photo differs"); - } - } - - return new MediaCopyReport(copiedAssets.Count, copiedUses.Count, differences); - } -} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibraryReader.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibraryReader.cs deleted file mode 100644 index 7272877..0000000 --- a/src/SlpModularCms.Modules.Media/Services/MediaLibraryReader.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Options; -using SlpModularCms.Core.Media.Library; -using SlpModularCms.Modules.Media.Data; - -namespace SlpModularCms.Modules.Media.Services; - -/// Reads a module's photos and uses back from the library's own tables (phase 2, step C). -public sealed class MediaLibraryReader(MediaDbContext db, IOptions options) : IMediaLibraryReader -{ - public MediaReadMode Mode => options.Value.ReadFromLibrary; - - public async Task> ListAssetsAsync(string module, CancellationToken cancellationToken) - => (await db.MediaAssets.AsNoTracking().Where(a => a.Module == module).ToListAsync(cancellationToken)) - .Select(a => new MediaAssetSnapshot( - a.Id, a.Module, a.OriginalSha256, a.OriginalKey, a.SourceFormat, a.Width, a.Height, a.Bytes, - a.DefaultFocusX, a.DefaultFocusY, a.DefaultZoom, a.SetHash ?? string.Empty, [], a.CreatedAt, a.CreatedByUserId, a.OriginalFileName, a.LastUsedAt, a.DeletedAt, - a.Variants, a.WideFocusX, a.WideFocusY, a.WideZoom, a.TallFocusX, a.TallFocusY, a.TallZoom)) - .ToList(); - - public async Task> ListUsesAsync(string module, CancellationToken cancellationToken) - => (await db.MediaUses.AsNoTracking().Where(u => u.Module == module).ToListAsync(cancellationToken)) - .Select(u => new MediaUseSnapshot(u.Id, u.AssetId, u.Module, u.PlaceKind, u.Position, u.Active, u.SetHash, null, u.Variants)) - .ToList(); -} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibrarySwitchState.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibrarySwitchState.cs deleted file mode 100644 index 3d9b975..0000000 --- a/src/SlpModularCms.Modules.Media/Services/MediaLibrarySwitchState.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Microsoft.Extensions.Options; -using SlpModularCms.Core.Media.Library; - -namespace SlpModularCms.Modules.Media.Services; - -/// The migration switches as configured right now (read from the options on every call). -public sealed class MediaLibrarySwitchState(IOptionsMonitor options) : IMediaLibrarySwitchState -{ - public MediaLibrarySwitches Current => new(options.CurrentValue.CopyOnStartup, options.CurrentValue.DualWrite, options.CurrentValue.ReadFromLibrary); -} diff --git a/src/SlpModularCms.Modules.Media/Services/MediaLibrarySync.cs b/src/SlpModularCms.Modules.Media/Services/MediaLibrarySync.cs deleted file mode 100644 index f0925ad..0000000 --- a/src/SlpModularCms.Modules.Media/Services/MediaLibrarySync.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using SlpModularCms.Core.Media.Library; - -namespace SlpModularCms.Modules.Media.Services; - -/// -/// The write-through (): mirrors one module into the library tables with the same verified -/// copy the start-up copy uses, one at a time. Off unless Media:Library:DualWrite is on. Never throws: the module's -/// own data is the truth and a failed mirror is logged and repaired by the next sync. -/// -public sealed class MediaLibrarySync(MediaLibraryCopier copier, IOptions options, ILogger logger) : IMediaLibrarySync -{ - // Two requests (or the background renderer) must not mirror at the same moment: one transaction at a time. - private static readonly SemaphoreSlim Gate = new(1, 1); - - public async Task SyncAsync(string module, CancellationToken cancellationToken) - { - if (!options.Value.DualWrite) - { - return; - } - - await Gate.WaitAsync(CancellationToken.None); - try - { - await copier.CopyAsync(CancellationToken.None, module); - } - catch (Exception exception) when (exception is not OperationCanceledException) - { - logger.LogError(exception, "The write-through of {Module} to the media library failed; the module's own data is untouched and the next change repairs it.", module); - } - finally - { - Gate.Release(); - } - } -}