Issue #3586343: Fix beta2 upgrade failures in updates 10006, 10008, and 10011.

Description

Fixes #3586343, where a beta2 → current 1.0.x upgrade can fail in one drush updb run in two places:

  1. ai_context_update_10006() — when legacy use-case scope values are migrated via $item->save(), the still-present NOT NULL type column is written as NULL because the code is already bundleless.
  2. ai_context_update_10011() — after update 10008 converts ai_context_item from bundled to bundleless, stale Content Moderation field-definition caches can remain in memory. Update 10011 then saves context items and hits presave errors such as unknown moderation_state.

This MR:

Update 10006 (type-column / entity-save failure):

  • Avoids entity API saves that rewrite the still-present type column while the code is already bundleless (migrate current-revision scope via direct database updates where needed, aligned with the existing revision-table approach), and/or otherwise preserves a valid type value until 10008 drops the column.
  • Adds Kernel coverage that simulates a beta2 type column plus legacy use-case values and asserts 10006 completes before 10008/10011.

Updates 10008 / 10011 (stale Content Moderation definitions):

  • Adds shared cache-reset helpers: _ai_context_reset_entity_type_and_bundle_caches() and _ai_context_reset_entity_definition_caches().
  • Uses the full entity-definition cache reset at the end of update 10008 instead of clearing only entity-type and bundle-info caches.
  • Removes the legacy bundled type field storage from last-installed definitions before calling updateEntityType() in update 10008.
  • Resets entity-definition caches once at the start of update 10011, before the batched entity saves.
  • Adds AiContextUpdate10008To10011UpgradeTest with:
    • a bundled beta2 schema simulation (entity type metadata, type columns, and legacy type field storage),
    • an assertion that moderated items save immediately after update 10008,
    • a separate test that update 10011 refreshes stale field definitions before backfill,
    • coverage of moderation state preservation, inherit_parent_scope backfill, scope indexes, workflow/bundle cleanup, and idempotent reruns.

Closes #3586343

Testing instructions

From the project root with DDEV running:

cd web/modules/contrib/ai_context
./lint.sh
cd ../../../..
ddev exec phpunit web/modules/contrib/ai_context/tests/src/Kernel/AiContextUpdate10008To10011UpgradeTest.php
ddev exec phpunit web/modules/contrib/ai_context/tests/src/Kernel/AiContextUpdate10011Test.php
ddev exec phpunit web/modules/contrib/ai_context/tests/src/Kernel/AiContextInstallUninstallTest.php --filter testUpdate10006MigratesUseCaseScopeValues

Manual beta2 upgrade verification (recommended before RTBC):

- Install AI Context 1.0.0-beta2 with at least one moderated context item.
- Assign a legacy use-case scope value that 10006 migrates (for example writing_words or working_in_canvas) so 10006 must update items.
- Check out this MR branch in web/modules/contrib/ai_context.
- Run a single ddev exec drush updb -y.
- Confirm updates 10006, 10008, and 10011 complete without error.
- Confirm existing items load, moderation states are valid, migrated use-case values are correct, and inherit_parent_scope was backfilled as expected.
- Load /admin/config/ai/context/items, /admin/config/ai/context/settings/general, and at least one context item view page; check logs for fatals.

## Checklist

- [x] I have updated the MR title to use format: `Issue #1234: My issue title.`
- [x] I have updated the MR description to include: `Closes #1234`
- [ ] I have performed a self-review of my own code
- [x] I have added or updated tests, or explained in the description why this change is not covered by tests
- [x] I have written testing instructions and verified them locally
- [x] This MR contains no breaking API or hook changes, or they are explicitly documented in the description

Not applicable

- I have updated documentation for any new or changed functionality
- I have noted any required post-merge steps (config imports, cache rebuilds, manual changes)

## AI Compliance

> [!note]
> Check the one that best describes your usage, or leave all unchecked if AI was not significantly used.
>
> * [x] AI **Assisted Code** \
> _Mainly written by a human; AI used for autocomplete or partial generation under full human supervision._
>
> * [ ] **AI Generated Code** \
> _Mainly generated by AI, reviewed and approved by a human before this MR was created._
>
> * [ ] **Vibe Coded** \
> _Generated by AI and only functionally reviewed before this MR was created._
Edited by Kristen Pol

Merge request reports

Loading