fix(db): enforce UNIQUE on edges so INSERT OR IGNORE actually dedupes#102
Open
andreinknv wants to merge 1 commit into
Open
fix(db): enforce UNIQUE on edges so INSERT OR IGNORE actually dedupes#102andreinknv wants to merge 1 commit into
andreinknv wants to merge 1 commit into
Conversation
The edges table has `id INTEGER PRIMARY KEY AUTOINCREMENT` and no other
UNIQUE constraint. The codebase uses
INSERT OR IGNORE INTO edges (source, target, kind, ...) VALUES (...)
clearly intending dedup, but the only candidate key for OR IGNORE was
the autoincrement id (which never conflicts) — so the OR IGNORE was a
silent no-op. Any code path that re-emits the same edge (resolver retries,
partial-failure re-runs, framework extractors that double-emit) silently
inserted duplicates, inflating call graphs in codegraph_callers/callees.
This change adds a real UNIQUE index on the natural key:
UNIQUE INDEX idx_edges_unique
ON edges(source, target, kind, COALESCE(line, -1), COALESCE(col, -1))
COALESCE keeps two NULL line/col values comparable as equal — SQLite
treats raw NULLs in a UNIQUE index as distinct, which would otherwise
defeat dedup for edges that don't carry a line/col (1-indexed everywhere
in this codebase, so -1 is a safe sentinel).
Migration v4 first deduplicates pre-existing rows (DELETE ... WHERE id
NOT IN (SELECT MIN(id) FROM edges GROUP BY source, target, kind,
COALESCE(line, -1), COALESCE(col, -1))) then creates the index. Both run
inside the migration transaction wrapper so a crash leaves the DB
consistent.
CURRENT_SCHEMA_VERSION bumped to 4. Two existing version-pinned tests
updated to match.
## Files changed
| File | Change |
|---|---|
| src/db/schema.sql | Add UNIQUE INDEX idx_edges_unique for fresh installs |
| src/db/migrations.ts | Bump version to 4; add migration v4 (dedup + index) |
| __tests__/edges-unique.test.ts | 7 regression tests |
| __tests__/foundation.test.ts | Update expected schema version |
| __tests__/pr19-improvements.test.ts | Update expected schema version |
## Test plan
- [x] npm test: 387/387 pass on macOS (one pre-existing fs.watch flake under parallel load, passes in isolation)
- [x] npx tsc --noEmit clean
- [x] Independent reviewer pass before pushing — APPROVE; nits-only
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Apr 26, 2026
This was referenced May 6, 2026
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
edgestable hasid INTEGER PRIMARY KEY AUTOINCREMENTand no other UNIQUE constraint. The codebase usesINSERT OR IGNORE INTO edges ...clearly intending dedup, but autoincrement IDs never conflict, so theOR IGNOREwas silently a no-op. Any path that re-emits the same edge (resolver retries, partial-failure re-runs, framework extractors that double-emit) accumulated duplicates → inflated call graphs incodegraph_callers/codegraph_calleesresults.What changed
src/db/schema.sqladdsUNIQUE INDEX idx_edges_unique ON edges(source, target, kind, COALESCE(line, -1), COALESCE(col, -1))for fresh installs.src/db/migrations.ts): existing databases first dedupe pre-existing duplicates (DELETE ... WHERE id NOT IN (SELECT MIN(id) ... GROUP BY ...)) then create the index. Both run inside the migration transaction wrapper, so a crash leaves the DB consistent.COALESCE(line, -1), COALESCE(col, -1)in the unique key: SQLite treats raw NULLs in a UNIQUE index as distinct, which would defeat dedup for edges that don't carry a line/column. The codebase's line numbers are 1-indexed and column numbers are non-negative everywhere —-1is a safe sentinel (verified via grep acrosssrc/).CURRENT_SCHEMA_VERSIONbumped 3 → 4. Two existing version-pinned tests updated.Files changed
src/db/schema.sqlUNIQUE INDEX idx_edges_uniquefor fresh installssrc/db/migrations.ts__tests__/edges-unique.test.ts__tests__/foundation.test.ts__tests__/pr19-improvements.test.tsTest coverage
COALESCEinsertEdgesdedupes within one callrunMigrations(db, 3), assert dedup occurred and the constraint is now enforcedTest plan
npm test: 387/387 pass on macOS (one pre-existing fs.watch flake under parallel load, passes in isolation — unrelated to this change)npx tsc --noEmitclean🤖 Generated with Claude Code