Skip to content

Commit c652a4b

Browse files
committed
add backfill-note-keys
1 parent 4c301de commit c652a4b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

store/backend/manage.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def backfill_note_keys(limit, dry_run):
8282
updated = 0
8383
checked = 0
8484

85+
# Collect note keys across analyses first, then write back once per annotation.
8586
q = models.Annotation.query.order_by(models.Annotation.created_at)
8687
if limit:
8788
q = q.limit(limit)
@@ -94,13 +95,20 @@ def backfill_note_keys(limit, dry_run):
9495

9596
inferred: OrderedDict[str, dict] = OrderedDict()
9697

98+
# First pass: collect all keys present in any note with their first-seen order.
9799
for aa in annotation.annotation_analyses:
98100
note = aa.note or {}
99-
for key, value in note.items():
101+
for key in note.keys():
100102
if key not in inferred:
101103
inferred[key] = {"type": None, "order": len(inferred)}
102-
# Keep searching until we see a non-null sample for this key
103-
if value is None or inferred[key]["type"] is not None:
104+
105+
# Second pass: try to find a non-null sample for each key to set its type.
106+
for aa in annotation.annotation_analyses:
107+
note = aa.note or {}
108+
for key, value in note.items():
109+
if key not in inferred or inferred[key]["type"] is not None:
110+
continue
111+
if value is None:
104112
continue
105113
if isinstance(value, bool):
106114
inferred[key]["type"] = "boolean"

0 commit comments

Comments
 (0)