gendesign/tradein-mvp/backend/data/sql/026_external_valuations.sql
lekss361 1ad2565af1 feat(tradein): SQL migrations 019-028 — Cian schema + matching + cookies
- 019: ALTER listings (windows_view_type, wcs counts, ceiling_height,
       repair_type, views, living_area, bedrooms, balconies, loggias,
       description_minhash, cadastral_number, phones, bargain_allowed,
       sale_type, house_source/ext_id, metro_stations)
- 020: ALTER houses (BTI: series_name, entrances, flat_count, is_emergency,
       heat/gas/overlap types; Cian: cian_internal_house_id,
       management_company_id, transport_accessibility_rate,
       advantages/banks/builders/houses_by_turn jsonb + GIN indexes)
- 021: CREATE management_companies + FK to houses.management_company_id
- 022: CREATE agents table + agent_id_fk on listings
- 023: CREATE offer_price_history (per-listing price change timeline)
- 024: CREATE houses_price_dynamics (monthly price/sqm series)
- 025: CREATE house_reliability_checks (наш.дом.рф checks)
- 026: CREATE external_valuations (unified Avito IMV + Cian Valuation cache)
- 027: CREATE EXTENSION pgcrypto + cian_session_cookies (encrypted)
- 028: CREATE house_sources, listing_sources, house_address_aliases
       + address_fingerprint/canonical/merged_into alters

Refs: decisions/CianScraper_v1_Implementation_Plan.md Stage 1
      decisions/Schema_Cian_SERP_Inventory.md sec 16, 20.3, 26
      decisions/Cross_Source_Matching_Strategy.md sec 2
2026-05-23 15:14:49 +03:00

46 lines
1.9 KiB
PL/PgSQL

-- 026_external_valuations.sql
-- Purpose: Unified cache for external valuation APIs — Avito IMV and Cian Valuation Calculator.
-- Replaces/supersedes avito_imv_evaluations (018) for multi-source use.
-- 018 table kept for backward compat; new code writes here.
-- Dependencies:
-- - None (standalone cache table)
-- Deploy order: Apply after 025.
--
-- Sources: Schema_Cian_SERP_Inventory sec 26.3 (external_valuations DDL)
-- CianScraper_v1_Implementation_Plan Stage 1
BEGIN;
CREATE TABLE IF NOT EXISTS external_valuations (
id bigserial PRIMARY KEY,
source text NOT NULL, -- 'avito_imv' / 'cian_valuation'
cache_key text NOT NULL, -- sha256(address+area+rooms+floor+repair+deal)
-- Input snapshot
address text,
total_area numeric(8,2),
rooms_count int,
floor int,
total_floors int,
repair_type text, -- without/cosmetic/euro/design
deal_type text, -- sale/rent
-- Output
price_rub numeric, -- primary estimate price
accuracy numeric, -- accuracy % (Cian-specific; 0-100)
raw_payload jsonb, -- full API response
chart jsonb, -- [{date, price}, ...] monthly points
-- Cache TTL
fetched_at timestamptz NOT NULL DEFAULT NOW(),
expires_at timestamptz NOT NULL, -- fetched_at + 24h typically
UNIQUE (source, cache_key)
);
-- Cache lookup: valid entries only
CREATE INDEX IF NOT EXISTS ev_source_expires_idx
ON external_valuations (source, expires_at DESC);
-- Address-based lookup (for analytics / dedup)
CREATE INDEX IF NOT EXISTS ev_address_idx
ON external_valuations (address)
WHERE address IS NOT NULL;
COMMIT;