- 047_cian_history_sanitize.sql: dedup existing rows then add UNIQUE(listing_id, change_time) so ON CONFLICT DO NOTHING in cian_detail.save_detail_enrichment suppresses real duplicates, not just PK collisions. Idempotent via DO $$ pg_constraint guard. - cian_valuation._save_to_cache: fill price_rub/accuracy (mig 026), low_price/high_price (mig 044), house_id/listing_id (mig 044). COALESCE on house_id/listing_id preserves existing FK on cache refresh. - cian_valuation._load_from_cache: restore low_price/high_price from DB into sale_price_from/sale_price_to so callers get the full band. - estimate_via_cian_valuation: add house_id/listing_id kwargs (default None), pass through to _save_to_cache. No callsite changes needed (defaults preserve existing behavior; PR 4 can supply IDs when context is available).
29 lines
882 B
PL/PgSQL
29 lines
882 B
PL/PgSQL
-- 047_cian_history_sanitize.sql
|
|
-- Purpose: Add UNIQUE(listing_id, change_time) to offer_price_history so that
|
|
-- the `ON CONFLICT DO NOTHING` in cian_detail.save_detail_enrichment
|
|
-- is semantically correct (currently only suppresses PK conflicts).
|
|
-- Dependencies: 023_offer_price_history.sql
|
|
-- Deploy order: Apply after 046.
|
|
|
|
BEGIN;
|
|
|
|
-- Dedupe existing rows before adding constraint (safe if table empty in prod).
|
|
DELETE FROM offer_price_history a
|
|
USING offer_price_history b
|
|
WHERE a.id > b.id
|
|
AND a.listing_id = b.listing_id
|
|
AND a.change_time = b.change_time;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'offer_price_history_listing_change_uq'
|
|
) THEN
|
|
ALTER TABLE offer_price_history
|
|
ADD CONSTRAINT offer_price_history_listing_change_uq
|
|
UNIQUE (listing_id, change_time);
|
|
END IF;
|
|
END $$;
|
|
|
|
COMMIT;
|