fix(tradein): 030 — UNIQUE constraint on avito_imv_evaluations.cache_key (unblocks PR #452) (#454)
All checks were successful
Deploy Trade-In / changes (push) Successful in 4s
Deploy Trade-In / build-frontend (push) Has been skipped
Deploy Trade-In / deploy (push) Successful in 20s
Deploy Trade-In / build-backend (push) Successful in 21s

Hotfix unblocking PR #452 (Stage 3 IMV estimator integration).

Adds 030_avito_imv_cache_key_unique.sql: creates UNIQUE INDEX imv_cache_key_uniq_idx
on avito_imv_evaluations(cache_key), allowing save_imv_evaluation's ON CONFLICT (cache_key)
DO UPDATE to resolve. Migration 018 only created a non-unique composite index, which would
have caused runtime error on first IMV cache write.

DELETE-USING dedup guards against historical duplicates (no-op on fresh table).
BEGIN/COMMIT atomic; IF NOT EXISTS idempotent; CONCURRENTLY unnecessary (table empty).

Lexicographic apply order with PR #453's 030_listings_alter_yandex.sql is safe — different
tables, no functional interaction.

Reviewed by deep-code-reviewer: APPROVE.
This commit is contained in:
lekss361 2026-05-23 13:04:21 +00:00
parent f452b132c9
commit 0db71033b4

View file

@ -0,0 +1,26 @@
-- 030_avito_imv_cache_key_unique.sql
-- Hotfix: добавить UNIQUE constraint на avito_imv_evaluations.cache_key
--
-- Stage 1 миграция 018 создала только non-unique `imv_cache_key_idx` —
-- но `save_imv_evaluation` использует `ON CONFLICT (cache_key) DO UPDATE`,
-- что требует UNIQUE/exclusion constraint, иначе runtime error:
-- ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
--
-- Закрывает blocker reviewer'а на PR #452 (Stage 3 IMV estimator integration).
--
-- Apply after: 018_avito_imv_evaluations.sql (Stage 1)
-- Required by: estimator IMV cache save flow (Stage 3, PR #452)
BEGIN;
-- Защитная очистка дубликатов по cache_key (если данные уже наливались).
-- На свежей БД (cache_key никогда не writev до этого PR) — no-op.
DELETE FROM avito_imv_evaluations a
USING avito_imv_evaluations b
WHERE a.id < b.id AND a.cache_key = b.cache_key;
-- Сам UNIQUE constraint через индекс (CONCURRENTLY не нужен — таблица свежая/пустая).
CREATE UNIQUE INDEX IF NOT EXISTS imv_cache_key_uniq_idx
ON avito_imv_evaluations (cache_key);
COMMIT;