feat: optimize source_markdown migration by processing documents in batches to reduce memory usage and improve performance

This commit is contained in:
Anish Sarkar 2026-02-17 11:47:56 +05:30
parent e1087937e6
commit ef53203e4b

View file

@ -6,7 +6,7 @@ Create Date: 2026-02-17
Adds source_markdown column and converts only documents that have Adds source_markdown column and converts only documents that have
blocknote_document data. Uses a pure-Python BlockNote JSON Markdown blocknote_document data. Uses a pure-Python BlockNote JSON Markdown
converter. No external dependencies (no Node.js, no Celery, no HTTP calls). converter without external dependencies.
Documents without blocknote_document keep source_markdown = NULL and Documents without blocknote_document keep source_markdown = NULL and
get populated lazily by the editor route when a user first opens them. get populated lazily by the editor route when a user first opens them.
@ -50,22 +50,24 @@ def upgrade() -> None:
_populate_source_markdown(conn) _populate_source_markdown(conn)
def _populate_source_markdown(conn) -> None: def _populate_source_markdown(conn, batch_size: int = 500) -> None:
"""Populate source_markdown only for documents that have blocknote_document.""" """Populate source_markdown only for documents that have blocknote_document.
Processes in batches to avoid long-running transactions and high memory usage.
"""
from app.utils.blocknote_to_markdown import blocknote_to_markdown from app.utils.blocknote_to_markdown import blocknote_to_markdown
# Only fetch documents that have blocknote_document content # Get total count first
result = conn.execute( count_result = conn.execute(
sa.text(""" sa.text("""
SELECT id, title, blocknote_document SELECT count(*)
FROM documents FROM documents
WHERE source_markdown IS NULL WHERE source_markdown IS NULL
AND blocknote_document IS NOT NULL AND blocknote_document IS NOT NULL
""") """)
) )
rows = result.fetchall() total = count_result.scalar()
total = len(rows)
if total == 0: if total == 0:
print("✓ No documents with blocknote_document need migration") print("✓ No documents with blocknote_document need migration")
return return
@ -74,6 +76,25 @@ def _populate_source_markdown(conn) -> None:
migrated = 0 migrated = 0
failed = 0 failed = 0
offset = 0
while offset < total:
# Fetch one batch at a time
result = conn.execute(
sa.text("""
SELECT id, title, blocknote_document
FROM documents
WHERE source_markdown IS NULL
AND blocknote_document IS NOT NULL
ORDER BY id
LIMIT :limit OFFSET :offset
"""),
{"limit": batch_size, "offset": offset},
)
rows = result.fetchall()
if not rows:
break
for row in rows: for row in rows:
doc_id = row[0] doc_id = row[0]
@ -104,6 +125,9 @@ def _populate_source_markdown(conn) -> None:
) )
failed += 1 failed += 1
print(f" Batch complete: processed {min(offset + batch_size, total)}/{total}")
offset += batch_size
print( print(
f"✓ source_markdown migration complete: {migrated} migrated, " f"✓ source_markdown migration complete: {migrated} migrated, "
f"{failed} failed out of {total} total" f"{failed} failed out of {total} total"