Preserve ingestion data across rebuilds

This commit is contained in:
george
2026-06-06 12:44:02 +01:00
parent f3509a363e
commit 7707a6306d
6 changed files with 194 additions and 38 deletions
+51
View File
@@ -245,6 +245,57 @@ def clear_library_documents(library_id: str) -> Dict[str, Any]:
conn.close()
def replace_library_documents(
library_id: str,
chunks: List[Dict[str, Any]],
) -> Dict[str, Any]:
"""Atomically replace all document chunks for a library."""
conn = get_connection()
try:
now = datetime.utcnow().isoformat()
conn.execute("BEGIN")
cursor = conn.execute(
"DELETE FROM documents WHERE library_id = ?", (library_id,)
)
deleted = cursor.rowcount
conn.executemany(
"""
INSERT INTO documents
(id, library_id, path, title, content, chunk_index,
token_estimate, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
[
(
chunk["id"],
library_id,
chunk["path"],
chunk.get("title"),
chunk.get("content"),
chunk.get("chunk_index"),
chunk.get("token_estimate", 0),
now,
)
for chunk in chunks
],
)
conn.commit()
return {
"success": True,
"deleted": deleted,
"inserted": len(chunks),
"library_id": library_id,
}
except Exception as e:
conn.rollback()
return {"success": False, "error": str(e)}
finally:
conn.close()
def delete_library(library_id: str) -> Dict[str, Any]:
"""Delete a library row and its document chunks."""
conn = get_connection()