Skip to content

Commit ebf22bc

Browse files
committed
Address PR #1372 review: refuse migration when DEFAULT_EMBEDDER is empty
Silently deleting NULL-embedder_path rows when settings.DEFAULT_EMBEDDER was misconfigured to empty was an irreversible failure mode. Now raise ValueError early with an actionable message naming the row count, so operators can either set DEFAULT_EMBEDDER or clean up manually before re-running the migration.
1 parent 0aad311 commit ebf22bc

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

opencontractserver/annotations/migrations/0068_enforce_embedder_path_not_null.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,27 @@
3131
def backfill_null_embedder_paths(apps, schema_editor):
3232
Embedding = apps.get_model("annotations", "Embedding")
3333

34-
default_embedder_path = getattr(settings, "DEFAULT_EMBEDDER", "") or ""
35-
if not default_embedder_path:
36-
logger.warning(
37-
"settings.DEFAULT_EMBEDDER is empty; NULL embedder_path rows will "
38-
"be deleted because no sensible backfill value is available."
39-
)
40-
4134
null_rows = Embedding.objects.filter(embedder_path__isnull=True)
4235
total = null_rows.count()
4336
if total == 0:
4437
logger.info("No Embedding rows with NULL embedder_path — nothing to backfill.")
4538
return
4639

40+
# Refuse to run if there's no default to backfill with — silently deleting
41+
# embedding rows because of a misconfigured env var would be irreversible.
42+
default_embedder_path = getattr(settings, "DEFAULT_EMBEDDER", "") or ""
43+
if not default_embedder_path:
44+
raise ValueError(
45+
f"settings.DEFAULT_EMBEDDER is empty but {total} Embedding row(s) "
46+
"have NULL embedder_path. Set DEFAULT_EMBEDDER (or manually clean "
47+
"up the NULL rows) before running this migration."
48+
)
49+
4750
backfilled = 0
4851
deleted = 0
4952

5053
# Use .iterator() to avoid loading the full set into memory on large tables.
5154
for emb in null_rows.iterator(chunk_size=500):
52-
if not default_embedder_path:
53-
emb.delete()
54-
deleted += 1
55-
continue
56-
5755
emb.embedder_path = default_embedder_path
5856
try:
5957
with transaction.atomic():

0 commit comments

Comments
 (0)