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,30 +131,21 @@ 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:
existing = existing_by_ref.get(ref)
if existing is None:
# No item row for this self_ref — likely a legacy DB
# where v0.40.0 didn't run. Skip; rerun migrations.
continue
new_records.append(
DocumentItemRecord( DocumentItemRecord(
document_id=doc_id, document_id=doc_id,
position=existing["position"], position=existing_by_ref[ref]["position"],
self_ref=ref, self_ref=ref,
label=existing.get("label", ""), label=existing_by_ref[ref].get("label", ""),
text=existing.get("text", ""), text=existing_by_ref[ref].get("text", ""),
page_numbers=existing.get("page_numbers", "[]"), page_numbers=existing_by_ref[ref].get("page_numbers", "[]"),
picture_data=img_bytes, picture_data=img_bytes,
) )
) for ref, img_bytes in updates
]
if new_records:
await ( await (
store.document_items_table.merge_insert( store.document_items_table.merge_insert(["document_id", "self_ref"])
["document_id", "self_ref"]
)
.when_matched_update_all() .when_matched_update_all()
.when_not_matched_insert_all() .when_not_matched_insert_all()
.execute(new_records) .execute(new_records)