-- 032_yandex_history.sql
-- Purpose: Enable source='yandex_valuation' on existing house_placement_history table by
--          updating the column COMMENT to document the new value and adding analytics-friendly
--          indexes for efficient filtered queries on the new source.
-- Dependencies: 017_house_placement_history.sql (table must already exist)
-- Deploy order: Apply after 031_houses_alter_yandex.sql
--
-- Sources:
--   - decisions/Schema_YandexRealty_Recon.md sec 12.5 + 13.2
--   - YandexRealtyScraper_v1_Implementation_Plan Stage 1
--
-- Note: Idempotent — does NOT alter table schema (no ADD/DROP COLUMN).
--       Only updates COMMENT metadata and creates indexes with IF NOT EXISTS.
--       Safe to re-run; COMMENT ON COLUMN is always an overwrite (no-error on repeat).

BEGIN;

-- Update column comment to document the new source enum value
COMMENT ON COLUMN house_placement_history.source IS
  'Source of history record: avito / avito_imv / cian / yandex_valuation';

-- Partial index for analytics queries filtered by source (mostly yandex_valuation)
CREATE INDEX IF NOT EXISTS hph_source_idx
  ON house_placement_history (source);

-- Composite index for "all history for a house from a specific source"
CREATE INDEX IF NOT EXISTS hph_house_source_idx
  ON house_placement_history (house_id, source, last_price_date DESC)
  WHERE house_id IS NOT NULL;

COMMIT;
