Skip to content

[BUG] INSERT OR REPLACE Changes File ID on Update Breaking Data Integrity #230

@Crimsonyx412

Description

@Crimsonyx412

Project

vgrep

Description

The insert_file() function uses INSERT OR REPLACE which, when updating an existing file, deletes the old row and creates a new one with a new auto-incremented ID. This changes the file's ID and can break the relationship with existing chunks if the deletion timing is off.

Error Message

N/A - Silent data integrity issue.

Debug Logs

# Can observe by checking file IDs before and after re-indexing:
sqlite3 ~/.vgrep/projects/*.db "SELECT id, path FROM files;"
# IDs change on each re-index with --force

System Information

OS: Ubuntu 22.04
vgrep version: 0.1.0
SQLite: bundled via rusqlite 0.32

Screenshots

No response

Steps to Reproduce

  1. Run vgrep index on a directory
  2. Query file IDs: sqlite3 ~/.vgrep/projects/*.db "SELECT id, path FROM files LIMIT 5;"
  3. Run vgrep index --force
  4. Query file IDs again
  5. Observe that all file IDs have changed

Expected Behavior

File IDs should remain stable for the same path to maintain referential integrity. Updates should modify the existing row rather than delete and recreate.

Actual Behavior

INSERT OR REPLACE deletes the existing row and inserts a new one with a new auto-incremented ID.

Additional Context

File: src/core/db.rs
Function: insert_file() (lines 107-117)
Problematic code:

self.conn.execute(
    "INSERT OR REPLACE INTO files (path, hash, indexed_at) VALUES (?, ?, ?)",
    params![path_str.as_ref(), hash, now],
)?;
Ok(self.conn.last_insert_rowid())  // Returns new ID, not original

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvalidValid issuevgrep

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions