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 → archivedshould overwritearchivedanddeletion_scheduleddescendants, not onlyancestor_inheritedones (ADR 003: "Allowed as restore to archived state") - Add
Namespace.restore_state_in_batchinNamespaces::Stateful, separate fromoverwrite_state_in_batchso 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_deletionpreserved key - For descendants that scheduled their own deletion, also clear
namespace_details.deletion_scheduled_atandstate_metadata['deletion_scheduled_by_user_id']so they are not still picked up for deletion
- Call
restore_state_in_batchfromNamespaces::StatePropagationServicewhen the propagation's source state isdeletion_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 likedeletion_scheduled, both inStatePrecedence.overwritable_statesand when choosing to restore, so itsdeletion_scheduleddescendants 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
- Precedence for
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
- ADR 003 — State preservation during propagation
- ADR 006: State Preservation
- Related: #599299 (closed)
- Blocks: #599305 (closed)
- Parent epic: &21922
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_scheduledfrom one inherited from its parent. The signal isnamespace_details.deletion_scheduled_at:set_deletion_schedule_datasets it, and bulk propagation never does. - Stopping the walk there means
Gitlab::Database::Namespaces::StatePropagationIteratorhas to check that column, not just the state (lateral queries anddetect_boundary). That needs a join tonamespace_detailsand probably a database review. - Until then, cancelling restores a subgroup that scheduled its own deletion like any other descendant.