gendesign/tradein-mvp/backend/data/sql/177_deals_city_region.sql
bot-backend 752cb1044b
All checks were successful
CI Trade-In / changes (pull_request) Successful in 7s
CI / changes (pull_request) Successful in 7s
CI Trade-In / frontend-checks (pull_request) Has been skipped
CI / backend-tests (pull_request) Has been skipped
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Has been skipped
CI Trade-In / backend-tests (pull_request) Successful in 51s
fix(tradein/ingest): раз-хардкод rosreestr DKP импорта на всю обл. 66
- scheduler.py import_rosreestr_dkp: снят фильтр city ILIKE '%катеринбург%', address из реального city источника, deals.region_code + новая deals.city заполняются (было: хардкод 'Екатеринбург,' + region_code NULL)
- migration 177: deals.city + индекс + бэкфилл существующих EKB-строк region_code=66/city
- guard city IS NOT NULL → address не NULL для ~15 null-city строк источника
- sync deploy/import-rosreestr.sh (ops-fallback) под тот же oblast-scope
- split dedup-теста: 077 (историческая) хранит EKB-фильтр, живой импорт — нет

Открывает +47183 не-ЕКБ сделок region 66, уже сидящих в источнике, ранее резавшихся на импорте.
2026-07-12 18:11:47 +03:00

51 lines
3.2 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 177_deals_city_region.sql
-- deals.city + region_code backfill — расширение Mera trade-in estimator с
-- Екатеринбурга на ВСЮ Свердловскую область (region_code=66, все города).
--
-- WHY:
-- Ночной import_rosreestr_dkp (scheduler.py, source='rosreestr_dkp_import')
-- до сих пор фильтровал `city ILIKE '%катеринбург%'` и хардкодил
-- 'Екатеринбург, ' в address, а deals.region_code никогда не заполнялся
-- (NULL на всех 49791 текущих строках). Foreign table
-- gendesign_rosreestr_deals уже отдаёт чистые city + region_code из
-- source-таблицы (migration 072) — снятие фильтра открывает +47183
-- не-ЕКБ сделок, уже сидящих в источнике.
--
-- WHAT:
-- - deals.region_code уже существует (002_core_tables.sql), но deals.city
-- отсутствовал (было только текстовое поле address с хардкод-префиксом
-- 'Екатеринбург, '). Добавляем city text — импортёр (scheduler.py,
-- тот же PR) теперь заполняет его из реального source.city.
-- - Индекс (city, deal_date) — под будущие per-city аналитические запросы
-- (аналог deals_source_idx для (source, deal_date)).
-- - Backfill существующих 49791 строк: все текущие deals с source='rosreestr'
-- импортированы ДО этой миграции старым EKB-only кодом, т.е. фактически
-- все они — Екатеринбург (то же допущение, что зашито в address
-- 'Екатеринбург, ' + street). Простановка region_code=66, city='Екатеринбург'
-- задним числом делает старые строки self-describing наравне с новыми.
--
-- SCOPE: только schema (city column + index) + backfill старых rosreestr-строк.
-- Новые импорты (после деплоя scheduler.py) заполняют оба поля сами.
--
-- IDEMPOTENCY / SAFETY:
-- - ADD COLUMN IF NOT EXISTS / CREATE INDEX IF NOT EXISTS — безопасный re-run.
-- - Backfill UPDATE ограничен `region_code IS NULL OR city IS NULL` —
-- повторный прогон no-op (после первого раза условие уже false).
--
-- Dependencies: 002_core_tables.sql (deals, deals.region_code)
BEGIN;
ALTER TABLE deals ADD COLUMN IF NOT EXISTS city text;
CREATE INDEX IF NOT EXISTS deals_city_deal_date_idx ON deals (city, deal_date);
-- Backfill: все существующие rosreestr-сделки импортированы старым EKB-only
-- кодом (до этой миграции) => гарантированно Екатеринбург, region_code=66.
UPDATE deals
SET region_code = 66,
city = 'Екатеринбург'
WHERE source = 'rosreestr'
AND (region_code IS NULL OR city IS NULL);
COMMIT;