Skip to content

[BUG] SQLite Foreign Keys Not Enabled - Orphaned Chunks Remain After File Deletion #228

@Crimsonyx412

Description

@Crimsonyx412

Project

vgrep

Description

SQLite foreign keys are disabled by default. The schema in src/core/db.rs defines FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE CASCADE but the code never executes PRAGMA foreign_keys = ON;. This means the cascade delete constraint is ignored, leaving orphaned chunks in the database when files are deleted.

Error Message

N/A - Silent data corruption. No error is raised; orphan data simply accumulates.

Debug Logs

N/A - This bug does not produce log output. Can be verified by querying:
sqlite3 ~/.vgrep/projects/*.db "SELECT COUNT(*) FROM chunks WHERE file_id NOT IN (SELECT id FROM files);"

System Information

vgrep version: 0.1.0
Rust: stable (as per rust-toolchain.toml)
SQLite: bundled via rusqlite 0.32

Screenshots

No response

Steps to Reproduce

  1. Run vgrep index on a directory containing source files
  2. Open the SQLite database directly: sqlite3 ~/.vgrep/projects/<hash>.db
  3. Execute: DELETE FROM files WHERE id = 1;
  4. Query orphaned chunks: SELECT COUNT(*) FROM chunks WHERE file_id = 1;
  5. Observe that chunks still exist despite their parent file being deleted

Expected Behavior

When a file row is deleted from the files table, all associated chunks in the chunks table should be automatically deleted via the ON DELETE CASCADE foreign key constraint.

Actual Behavior

Chunks with file_id referencing deleted files remain in the database indefinitely. SQLite silently ignores the foreign key constraint because PRAGMA foreign_keys defaults to OFF.

Additional Context

File: src/core/db.rs
Function: init_schema() (lines 55-83)
Fix direction: Add PRAGMA foreign_keys = ON; as the first statement in init_schema().

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginvalidThis doesn't seem right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions