fix(db): dedupe domrf_kn_documents + UNIQUE NULLS NOT DISTINCT (#1206) #1263
No reviewers
Labels
No labels
admin
analytics
auth
automation
bug
business
chore
ci
compliance
data
data-moat
docs
duplicate
dx
enhancement
Fable 5 ревью
feedback/max
generative
GG-форсайт
needs-discussion
needs-human
observability
pause-bots
performance
priority/p0
priority/p1
priority/p2
priority/p3
scope/backend
scope/db
scope/devops
scope/frontend
scope/qa
scrapers
security
site-finder
stage/1
stage/2
status/blocked
status/done
status/needs-analysis
status/needs-fix
status/qa
status/ready
status/review
status/wip
tech-debt
tradein
ux
week ревью 1
wontfix
вторичка
ИРД
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: lekss361/gendesign#1263
Loading…
Add table
Reference in a new issue
No description provided.
Delete branch "fix/domrf-documents-unique-nulls-not-distinct"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Closes #1206 (P2 data-integrity, prod).
domrf_kn_documentsкопила дубли при каждом sweep: UNIQUE безNULLS NOT DISTINCT,doc_numnullable → ON CONFLICT-арбитр не матчился для NULL.Bug
data/sql/112_22i_domrf_documents.sql:18объявилUNIQUE (obj_id, doc_type, doc_num, file_url)БЕЗNULLS NOT DISTINCT.doc_numnullable,extract_documents:125-134реально передаётdoc_num=None(документированный частый вход).NULL != NULLв уникальном индексе → дляdocNum=nullарбитрON CONFLICTвscrapers/documents.py:59НИКОГДА не матчится → каждый ежедневный sweepdomrf_kn.py:1903молча вставляет новую строку-дубль вместо UPDATE.analytics_queries.py:1068.Прецедент того же класса: м.110 (POI), м.125 (supply_layers).
Fix (Миграция 140, SQL-only)
ROW_NUMBER() OVER (PARTITION BY obj_id, doc_type, doc_num, file_url ORDER BY scraped_at DESC, id DESC)→ DELETEWHERE rn > 1 AND doc_num IS NULL. Winner = самая свежая (caller бампитscraped_atна каждом UPDATE).IF EXISTS(re-apply safe).uq_domrf_kn_documents_logical UNIQUE NULLS NOT DISTINCT (...)именованный + guard поpg_constraint.conname(паттерн м.125 → idempotent).Caller
scrapers/documents.pyНЕ меняется — после миграции ON CONFLICT-арбитр начнёт матчить NULL-doc_num строки автоматически. Никакой data-coercion (doc_num="") не нужно.Deploy
deploy.yml+_schema_migrationstracking.Closes #1206