Restore preserved descendant states when a deletion is cancelled

Summary

When a parent cancels a scheduled deletion, restore each descendant to the state saved under state_metadata['preserved_states']['schedule_deletion'] (written by #599299 (closed)) instead of setting every descendant to the propagation target. Also fix the precedence check so cancelling the deletion of an archived parent updates deletion_scheduled descendants.

Tasks

  • Fix Namespaces::Stateful::StatePrecedence.reversal? so any move to a lower-precedence state is a reversal. deletion_scheduled → archived should overwrite archived and deletion_scheduled descendants, not only ancestor_inherited ones (ADR 003: "Allowed as restore to archived state")
  • Add Namespace.restore_state_in_batch in Namespaces::Stateful, separate from overwrite_state_in_batch so it can be built in parallel with #599299 (closed):
    • Set each descendant to the higher-precedence of its preserved state and the parent's target, so a child is never less restricted than its parent
    • Fall back to the target when no state is preserved
    • Clear the schedule_deletion preserved key
    • For descendants that scheduled their own deletion, also clear namespace_details.deletion_scheduled_at and state_metadata['deletion_scheduled_by_user_id'] so they are not still picked up for deletion
  • Call restore_state_in_batch from Namespaces::StatePropagationService when the propagation's source state is deletion_scheduled
  • Handle a parent leaving deletion_in_progress (a failed or cancelled deletion moving it to active or archived, per ADR 003's transition table). Treat it like deletion_scheduled, both in StatePrecedence.overwritable_states and when choosing to restore, so its deletion_scheduled descendants are restored
  • Add specs covering:
    • Precedence for deletion_scheduled → archived
    • Restore to the preserved state
    • Fallback to the target when nothing is preserved
    • Restored state is never below the target
    • Preserved key and deletion schedule data are cleared
    • Worker: active parent cancelling deletion
    • Worker: archived parent cancelling deletion
    • Worker: descendant that scheduled its own deletion
    • Precedence and worker: parent leaving deletion_in_progress

This must be merged before #599305 (closed), which wires the state machine to create propagation records. Otherwise cancelling a deletion loses descendants' archived state.

References


Note to @smaglangit: create a follow-up issue for subgroups that scheduled their own deletion. When a parent cancels its deletion, such a subgroup should keep its deletion schedule, and propagation should stop there so its descendants stay deletion_scheduled.

Why it's a separate issue
  • Precedence alone can't tell a subgroup's own deletion_scheduled from one inherited from its parent. The signal is namespace_details.deletion_scheduled_at: set_deletion_schedule_data sets it, and bulk propagation never does.
  • Stopping the walk there means Gitlab::Database::Namespaces::StatePropagationIterator has to check that column, not just the state (lateral queries and detect_boundary). That needs a join to namespace_details and probably a database review.
  • Until then, cancelling restores a subgroup that scheduled its own deletion like any other descendant.
Edited by Shane Maglangit