Skip to content

fix: wrap incremental_trace_flows deletions in a transaction #258

@VaibhavDangaich

Description

@VaibhavDangaich

Problem

In code_review_graph/flows.py:412-415, incremental_trace_flows performs
multiple DELETE operations (flow_memberships and flows) in a loop without
wrapping them in a BEGIN IMMEDIATE transaction:

for fid in affected_ids:
    conn.execute("DELETE FROM flow_memberships WHERE flow_id = ?", (fid,))
    conn.execute("DELETE FROM flows WHERE id = ?", (fid,))                    
conn.commit()                                             
                                                                              
If an error occurs mid-loop (e.g., disk full, locked DB), some flows will be
deleted while others remain, leaving the database in an inconsistent state.   
                
Expected Behavior                                                             
                
The deletion loop should be wrapped in a transaction with rollback on failure,
 matching the pattern already used by store_flows and store_communities in the
 same file:                                                                   
                
conn.execute("BEGIN IMMEDIATE")
try:                                                                          
    for fid in affected_ids:
        conn.execute("DELETE FROM flow_memberships WHERE flow_id = ?", (fid,))
        conn.execute("DELETE FROM flows WHERE id = ?", (fid,))
    conn.commit()                                                             
except Exception:
    conn.rollback()                                                           
    raise       

Impact

Partial deletions could corrupt flow data during incremental updates,         
especially on slower storage or under concurrent access.

so let me know if I can raise a PR !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions