Preserve ingestion data across rebuilds
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user