Commit Graph
2 Commits
Author SHA1 Message Date
Dirk JulichandClaude Opus 4.6 dad4da74b9 Fix 500 on /api/v2/schedules/ from orphaned token cleanup schedule (#16660)
* Fix 500 error on /api/v2/schedules/ from orphaned token cleanup schedule

delete_clear_tokens_sjt() relied on cascade to remove the associated
schedule when deleting the cleanup_tokens SystemJobTemplate. In migration
context with polymorphic models, the cascade did not reliably fire,
leaving an orphaned schedule whose unified_job_template referenced a
missing row.

Explicitly delete associated schedules and clear stale next_schedule
references before deleting the SJT. Add defensive handling in
ScheduleSerializer.get_related() and get_summary_fields() so orphaned
schedules degrade gracefully instead of raising RelatedObjectDoesNotExist.

Fix the existing test assertion that used a JOIN-based query incapable of
detecting orphans, and add coverage for next_schedule reference cleanup.

Fixes: AAP-92564

* Add migration to clean up orphaned token cleanup schedules

Databases that already ran migration 0204 may have orphaned "Cleanup
Expired OAuth 2 Tokens" schedules left behind by the broken cascade.
Remove them and clear any stale UnifiedJobTemplate.next_schedule
references. Add a test that simulates the orphan scenario by deleting
the UJT via raw SQL and verifying the migration repairs it.

Ref: AAP-92564

* Re-parent migration 0212 for gateway backport to stable-2.7

Apply the gateway backport migration pattern (AAP-87599): re-parent
0212_cleanup_orphaned_token_schedules to depend on 0207_merge (the
common ancestor with tower/stable-2.7) instead of 0211. Add
0213_merge_0211_0212 to converge the parallel paths on devel.

This allows the backport to tower/stable-2.7 to use the identical
0212 migration file with no changes.

Ref: AAP-92564

* Test next_schedule cleanup on a surviving template

Use the cleanup_sessions SystemJobTemplate (which survives the deletion)
as the next_schedule reference holder, so the assertion actually proves
the UPDATE ... SET next_schedule = NULL logic works rather than being
masked by the referenced UJT itself being deleted.

Ref: AAP-92564

* Fix test: create surviving SJT instead of relying on migration data

The CI test DB does not have the cleanup_sessions SystemJobTemplate
from migration 0078. Use get_or_create to ensure the surviving UJT
exists within the test itself.

Ref: AAP-92564

* Renumber cleanup migration to 0208 and bump 0208-0211 to 0209-0212

Replace the gateway backport pattern (re-parented migration + merge
migration) with a simpler approach: slot the cleanup migration as 0208
directly after the merge point, and renumber the subsequent migrations.
This keeps the migration graph linear on all branches and preserves
the ability to backport 0209-0212 individually in the future.

Ref: AAP-92564

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-09-23 17:34:29 +02:00
Dirk JulichandClaude Opus 4.6 2a6dd41186 AAP-89067: Fix stale image purge when EE image reference form changes (#16664)
* AAP-89067: Fix stale image purge when EE image reference form changes

The _handle_image_cleanup signal compared images by string only, so two
references to the same underlying image (e.g. tag vs digest) were treated
as different. This caused podman rmi to strip all names from a still-needed
image, breaking environments especially in disconnected installs.

Add layered guards: skip cleanup for managed images (settings and
managed=True EE objects), detect tag-vs-digest reference form changes
for the same repository, and check other EEs for cross-reference form
conflicts on delete. Genuinely different versions (v1→v2) still get
cleaned up.

* Reduce cognitive complexity of _handle_image_cleanup

Extract the delete-path loop into _other_ee_shares_repo_with_different_ref
to flatten nesting and bring cognitive complexity under the SonarQube
threshold of 15.

* Extend managed-image guard to match by repo when ref forms differ

_is_managed_image previously only did exact string comparison against
managed settings, so repo@sha256:... would not match repo:tag in
CONTROL_PLANE_EXECUTION_ENVIRONMENT or GLOBAL_JOB_EXECUTION_ENVIRONMENTS.
Now also compares by repository + reference form (tag vs digest).

* Add docstring to _handle_image_cleanup

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-09-22 13:00:26 +02:00