drop the existing-row-not-found guard from the 0.45.0 picture-bytes migration.

This commit is contained in:
Yiorgis Gozadinos 2026-04-30 15:48:03 +03:00
parent b290bbe82d
commit 4c9ef81fc6
No known key found for this signature in database

View file

@ -120,6 +120,8 @@ async def _apply_extract_picture_bytes(store: Store) -> None: # pragma: no cove
if updates: if updates:
# Find the matching items rows so we can preserve their # Find the matching items rows so we can preserve their
# position/label/text/page_numbers and just attach bytes. # position/label/text/page_numbers and just attach bytes.
# Earlier migrations (v0.40.0) populate items rows for every
# picture self_ref, so the lookup below is total.
self_refs = [u[0] for u in updates] self_refs = [u[0] for u in updates]
ref_clause = ", ".join(f"'{escape_sql_string(r)}'" for r in self_refs) ref_clause = ", ".join(f"'{escape_sql_string(r)}'" for r in self_refs)
existing_items = await ( existing_items = await (
@ -129,35 +131,26 @@ async def _apply_extract_picture_bytes(store: Store) -> None: # pragma: no cove
) )
existing_by_ref = {r["self_ref"]: r for r in existing_items} existing_by_ref = {r["self_ref"]: r for r in existing_items}
new_records: list[DocumentItemRecord] = [] new_records = [
for ref, img_bytes in updates: DocumentItemRecord(
existing = existing_by_ref.get(ref) document_id=doc_id,
if existing is None: position=existing_by_ref[ref]["position"],
# No item row for this self_ref — likely a legacy DB self_ref=ref,
# where v0.40.0 didn't run. Skip; rerun migrations. label=existing_by_ref[ref].get("label", ""),
continue text=existing_by_ref[ref].get("text", ""),
new_records.append( page_numbers=existing_by_ref[ref].get("page_numbers", "[]"),
DocumentItemRecord( picture_data=img_bytes,
document_id=doc_id,
position=existing["position"],
self_ref=ref,
label=existing.get("label", ""),
text=existing.get("text", ""),
page_numbers=existing.get("page_numbers", "[]"),
picture_data=img_bytes,
)
) )
for ref, img_bytes in updates
]
if new_records: await (
await ( store.document_items_table.merge_insert(["document_id", "self_ref"])
store.document_items_table.merge_insert( .when_matched_update_all()
["document_id", "self_ref"] .when_not_matched_insert_all()
) .execute(new_records)
.when_matched_update_all() )
.when_not_matched_insert_all() wrote_items = True
.execute(new_records)
)
wrote_items = True
new_structure_bytes = compress_json(json.dumps(data)) new_structure_bytes = compress_json(json.dumps(data))
await store.documents_table.update( await store.documents_table.update(