fix(cli): remove stale capture index rows#22
Open
zxy1553 wants to merge 1 commit intoEinsia:mainfrom
Open
Conversation
Made-with: Cursor
There was a problem hiding this comment.
Code Review
This pull request introduces synchronization between the capture buffer directory and the database index. It updates rebuild_captures_index to remove stale database entries and _clean_captures to delete records when files are unlinked. New tests have been added to verify these cleanup operations. The review feedback suggests wrapping database deletion loops in transactions to improve performance in SQLite.
|
|
||
| files = sorted(p for p in buf.iterdir() if p.is_file() and p.suffix == ".json") | ||
| file_ids = {p.stem for p in files} | ||
| with fts.cursor() as conn: |
There was a problem hiding this comment.
The loop performs multiple database deletions in autocommit mode, which is inefficient in SQLite. Wrapping the loop in a transaction (e.g., using with conn:) will improve performance by grouping these operations into a single commit.
Suggested change
| with fts.cursor() as conn: | |
| with fts.cursor() as conn, conn: |
| removed_stems.append(p.stem) | ||
| n += 1 | ||
| if removed_stems: | ||
| with fts.cursor() as conn: |
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
Fix stale rows in the
capturesSQLite table when capture JSON files are removed.Changes:
clean capturesnow deletes matchingcapturesrows after removingcapture-buffer/*.jsonrebuild-captures-indexnow removes rows whose capture files no longer existTests
python -m pytest tests/test_cli_captures_cleanup.py -qpython -m ruff check src/openchronicle/cli.py tests/test_cli_captures_cleanup.py