check_circular_dependency

Function check_circular_dependency 

Source
pub async fn check_circular_dependency(
    pool: &SqlitePool,
    blocking_task_id: i64,
    blocked_task_id: i64,
) -> Result<bool>
Expand description

Check if adding a dependency would create a circular dependency.

This function implements a depth-first search using SQLite’s recursive CTE to detect cycles in the dependency graph.

§Algorithm

To check if we can add “blocked_task depends on blocking_task”:

  1. Start from blocking_task (the new prerequisite)
  2. Traverse what blocking_task depends on (its blocking tasks)
  3. If we ever reach blocked_task, adding this dependency would create a cycle

§Example

Existing: A depends on B (stored as: blocking=B, blocked=A) Trying to add: B depends on A (would be: blocking=A, blocked=B)

Check: Does A depend on B?

  • Start from A (new blocking task)
  • Find what A depends on: B
  • We reached B (new blocked task) → Cycle detected!

§Performance

  • Time complexity: O(V + E) where V is tasks and E is dependencies
  • Expected: <10ms for graphs with 10,000 tasks
  • Depth limit: 100 levels to prevent infinite loops

§Arguments

  • pool - Database connection pool
  • blocking_task_id - ID of the task that must be completed first
  • blocked_task_id - ID of the task that depends on the blocking task

§Returns

  • Ok(true) if adding this dependency would create a cycle
  • Ok(false) if the dependency is safe to add
  • Err if database query fails