Refactor: Move document indexing DB operations to store.ts

This refactoring extracts all document indexing database operations from
the indexFiles() function in qmd.ts into dedicated methods in store.ts:

- insertContent(): Insert content into content-addressable storage
- insertDocument(): Insert new document record
- findActiveDocument(): Find active document by collection and path
- updateDocumentTitle(): Update document title only
- updateDocument(): Update document hash, title, and modified_at
- deactivateDocument(): Mark document as inactive
- getActiveDocumentPaths(): Get all active document paths for collection
- cleanupOrphanedContent(): Remove unreferenced content hashes

Key improvements:
- Better separation of concerns (DB layer vs business logic)
- Reusable DB operations accessible throughout the codebase
- Fixed bug where document updates would fail with UNIQUE constraint error
- Fixed bug in updateCollections() that passed wrong parameters to indexFiles()
- Removed obsolete updateDisplayPaths() function from old schema

The indexing logic now uses updateDocument() instead of deactivate + insert
when content changes, which properly updates the existing document record
rather than violating the UNIQUE(collection_id, path) constraint.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Tobi Lutke 2025-12-12 16:46:21 -05:00
parent ecb7be69c2
commit 46d35de899
No known key found for this signature in database
2 changed files with 5 additions and 6 deletions

View File

@ -14,7 +14,7 @@
{"id":"qmd-e2c","title":"Implement 'qmd ls' command","description":"Add command to explore virtual file tree:\n- qmd ls → list all collections\n- qmd ls \u003ccollection\u003e → list files in collection\n- qmd ls \u003ccollection\u003e/\u003cpath\u003e → list files under path\nOutput: flat list of qmd:// paths","status":"closed","priority":1,"issue_type":"feature","created_at":"2025-12-12T15:29:53.859804-05:00","updated_at":"2025-12-12T15:55:12.777701-05:00","closed_at":"2025-12-12T15:55:12.777701-05:00","dependencies":[{"issue_id":"qmd-e2c","depends_on_id":"qmd-ama","type":"discovered-from","created_at":"2025-12-12T15:29:53.860535-05:00","created_by":"daemon"}]}
{"id":"qmd-i3t","title":"Move context management DB operations to store.ts","description":"Move path_contexts INSERT/DELETE/SELECT operations from addContext(), listContexts(), removeContext() to store.ts. Create methods like insertContext(), deleteContext(), etc.","status":"in_progress","priority":2,"issue_type":"task","created_at":"2025-12-12T16:36:21.561746-05:00","updated_at":"2025-12-12T16:39:16.024705-05:00","dependencies":[{"issue_id":"qmd-i3t","depends_on_id":"qmd-29c","type":"parent-child","created_at":"2025-12-12T16:37:02.866006-05:00","created_by":"daemon"}]}
{"id":"qmd-j9z","title":"Add unit tests for content addressable hashes","description":"add same file from multiple places and verify that they both point at same hash. drop one collection and the content stays.","status":"closed","priority":3,"issue_type":"task","created_at":"2025-12-12T15:39:15.459504-05:00","updated_at":"2025-12-12T16:21:35.473776-05:00","closed_at":"2025-12-12T16:21:35.473776-05:00"}
{"id":"qmd-kf8","title":"Move document indexing DB operations to store.ts","description":"Move INSERT/UPDATE/DELETE operations for documents and content tables from indexFiles() to store.ts. Create methods like insertDocument(), updateDocument(), deactivateDocuments(), etc.","status":"in_progress","priority":2,"issue_type":"task","created_at":"2025-12-12T16:36:14.558702-05:00","updated_at":"2025-12-12T16:39:14.859951-05:00","dependencies":[{"issue_id":"qmd-kf8","depends_on_id":"qmd-29c","type":"parent-child","created_at":"2025-12-12T16:37:02.770251-05:00","created_by":"daemon"}]}
{"id":"qmd-kf8","title":"Move document indexing DB operations to store.ts","description":"Move INSERT/UPDATE/DELETE operations for documents and content tables from indexFiles() to store.ts. Create methods like insertDocument(), updateDocument(), deactivateDocuments(), etc.","status":"closed","priority":2,"issue_type":"task","created_at":"2025-12-12T16:36:14.558702-05:00","updated_at":"2025-12-12T16:45:38.830978-05:00","closed_at":"2025-12-12T16:45:38.830978-05:00","dependencies":[{"issue_id":"qmd-kf8","depends_on_id":"qmd-29c","type":"parent-child","created_at":"2025-12-12T16:37:02.770251-05:00","created_by":"daemon"}]}
{"id":"qmd-ltg","title":"look for missing context","description":"i ran qmd context list and thats only one bit of context, i had a lot more. i think the path matching isn't quite working right","status":"open","priority":2,"issue_type":"task","created_at":"2025-12-12T16:42:57.324769-05:00","updated_at":"2025-12-12T16:42:57.324769-05:00"}
{"id":"qmd-p1h","title":"Create collection add|remove","description":"","status":"closed","priority":2,"issue_type":"task","created_at":"2025-12-10T10:57:00.717864-05:00","updated_at":"2025-12-12T16:12:00.557003-05:00","closed_at":"2025-12-12T16:12:00.557003-05:00"}
{"id":"qmd-rck","title":"move the source files to src/*, clean up teh directory","description":"","status":"open","priority":2,"issue_type":"task","created_at":"2025-12-12T16:40:19.198119-05:00","updated_at":"2025-12-12T16:40:19.198119-05:00"}

9
qmd.ts
View File

@ -52,6 +52,7 @@ import {
insertDocument,
findActiveDocument,
updateDocumentTitle,
updateDocument,
deactivateDocument,
getActiveDocumentPaths,
cleanupOrphanedContent,
@ -581,7 +582,7 @@ async function contextAdd(pathArg: string | undefined, contextText: string): Pro
// Find all collections and add context to each
const collections = getAllCollections(db);
for (const coll of collections) {
insertContext(db, coll.id, \'\', contextText);
insertContext(db, coll.id, '', contextText);
}
console.log(`${c.green}${c.reset} Added global context to ${collections.length} collection(s)`);
console.log(`${c.dim}Context: ${contextText}${c.reset}`);
@ -1403,12 +1404,10 @@ async function indexFiles(pwd?: string, globPattern: string = DEFAULT_GLOB, name
} else {
// Content changed - insert new content hash and update document
insertContent(db, hash, content, now);
deactivateDocument(db, collectionId, path);
updated++;
const stat = await Bun.file(filepath).stat();
insertDocument(db, collectionId, path, title, hash,
stat ? new Date(stat.birthtime).toISOString() : now,
updateDocument(db, existing.id, title, hash,
stat ? new Date(stat.mtime).toISOString() : now);
updated++;
}
} else {
// New document - insert content and document