Merge pull request 'fix(migration): 0045 guards json_each against scalar tagger_predictions' (#93) from dev into main
CI / lint (push) Successful in 2s
CI / frontend-build (push) Successful in 21s
CI / backend-lint-and-test (push) Successful in 27s
CI / integration (push) Successful in 3m13s
Build images / sign-extension (push) Successful in 2s
Build images / build-web (push) Successful in 6s
Build images / build-ml (push) Successful in 2m40s

This commit was merged in pull request #93.
This commit is contained in:
2026-06-10 20:33:27 -04:00
@@ -55,17 +55,26 @@ def upgrade() -> None:
# 0.70) right here so this is self-sufficient — it does NOT depend on the
# #764 prune having run, and extracting only the >=floor tail keeps
# image_prediction small (~tens of rows/image) even from the full JSON.
# MATERIALIZED so the json_typeof guard runs BEFORE json_each — some rows
# store tagger_predictions as a JSON scalar/null (not an object), and
# json_each throws "cannot deconstruct a scalar" on those. Filtering to
# objects in a materialized CTE keeps json_each from ever seeing them.
op.execute(
"""
WITH objs AS MATERIALIZED (
SELECT id, tagger_predictions AS preds
FROM image_record
WHERE tagger_predictions IS NOT NULL
AND json_typeof(tagger_predictions) = 'object'
)
INSERT INTO image_prediction (image_record_id, raw_name, category, score)
SELECT ir.id,
SELECT o.id,
je.key,
COALESCE(je.value ->> 'category', 'general'),
(je.value ->> 'confidence')::double precision
FROM image_record ir,
json_each(ir.tagger_predictions) je
WHERE ir.tagger_predictions IS NOT NULL
AND je.value ->> 'confidence' IS NOT NULL
FROM objs o,
json_each(o.preds) je
WHERE je.value ->> 'confidence' IS NOT NULL
AND (je.value ->> 'confidence')::double precision
>= COALESCE(
(SELECT tagger_store_floor FROM ml_settings WHERE id = 1),