Exercise 2: Confirming the Cascade — Possible Solution ==================================================================== SETUP ------------------------------ id: 1, slug: 'java', full_path: 'programming/java', parent_id: id: 2, slug: 'fundamentals', full_path: 'programming/java/fundamentals', parent_id: 1 id: 3, slug: 'chapter-1', full_path: 'programming/java/fundamentals/chapter-1', parent_id: 2 MOVING java UNDER A NEW PARENT ------------------------------ await movePage(pool, 1, <'reference' page's own id>); WHAT HAPPENS ------------------------------ 1. Page 1's own full_path is updated first, directly, to something like 'reference/java'. 2. The recursive CTE finds pages 2 and 3 as descendants of page 1 in one query, ordered by depth: page 2 (depth 1) comes before page 3 (depth 2). 3. The for loop processes page 2 first: its new full_path is built from pathById[1] (already 'reference/java', just written), giving 'reference/java/fundamentals'. This gets saved, and pathById[2] is recorded. 4. The loop then processes page 3: its new full_path is built from pathById[2] (just computed in the previous iteration), giving 'reference/java/fundamentals/chapter-1'. CONFIRMATION ------------------------------ All three pages now correctly reflect the new 'reference/java/...' branch - the cascade reached the grandchild (page 3), not just the directly-moved page or its immediate child, and the single linear pass processed both descendants in the correct order using only values already computed earlier in the same loop. WHY THIS WORKS AS AN ANSWER ------------------------------ It correctly traces the recursive CTE's own depth-ordered result set, and correctly shows the linear for loop building each descendant's new path from its own parent's already-updated path within the same pass, confirming the cascade reaches every level correctly.