diff --git a/tradein-mvp/backend/data/sql/031_houses_alter_yandex.sql b/tradein-mvp/backend/data/sql/031_houses_alter_yandex.sql index 66885b72..d33421f3 100644 --- a/tradein-mvp/backend/data/sql/031_houses_alter_yandex.sql +++ b/tradein-mvp/backend/data/sql/031_houses_alter_yandex.sql @@ -3,7 +3,7 @@ -- Dependencies: -- - houses table: 009_houses.sql -- - 020_houses_alter_cian.sql (prior Cian columns must exist; no FK dependency, column-level only) --- Deploy order: Apply after 030_listings_alter_yandex.sql +-- Deploy order: Apply after 033_listings_alter_yandex.sql (renamed from duplicate-prefix 030 in PR feat/sql-rename-030-and-schema-debt) -- -- Sources: -- - decisions/Schema_YandexRealty_Recon.md sec 11.2 (Newbuilding ЖК landing fields) diff --git a/tradein-mvp/backend/data/sql/030_listings_alter_yandex.sql b/tradein-mvp/backend/data/sql/033_listings_alter_yandex.sql similarity index 98% rename from tradein-mvp/backend/data/sql/030_listings_alter_yandex.sql rename to tradein-mvp/backend/data/sql/033_listings_alter_yandex.sql index 3fbed965..e5464bde 100644 --- a/tradein-mvp/backend/data/sql/030_listings_alter_yandex.sql +++ b/tradein-mvp/backend/data/sql/033_listings_alter_yandex.sql @@ -1,4 +1,4 @@ --- 030_listings_alter_yandex.sql +-- 033_listings_alter_yandex.sql -- Purpose: Add Yandex Realty-specific columns to listings table (SERP + detail fields). -- Dependencies: listings table created in 002_core_tables.sql / 011_listings_alter.sql -- 019_listings_alter_cian.sql already adds views_total (int) for Cian. diff --git a/tradein-mvp/backend/data/sql/034_trade_in_estimates_geom.sql b/tradein-mvp/backend/data/sql/034_trade_in_estimates_geom.sql new file mode 100644 index 00000000..49f58e41 --- /dev/null +++ b/tradein-mvp/backend/data/sql/034_trade_in_estimates_geom.sql @@ -0,0 +1,45 @@ +-- 034_trade_in_estimates_geom.sql +-- Purpose: schema-consistency debt — add PostGIS geom column + GIST index + trigger +-- to trade_in_estimates (table currently has lat/lon but no geom). +-- Dependencies: 001_trade_in_estimates.sql (table must exist) +-- Deploy order: any time after 001. +-- +-- Why: rule `.claude/rules/sql.md` — any table with lat/lon should have geom + GIST +-- for future spatial queries. Currently estimator does not query trade_in_estimates +-- spatially, but views/audits will. Trigger keeps geom in sync with lat/lon writes. +-- +-- Idempotent: ADD COLUMN/INDEX IF NOT EXISTS; CREATE OR REPLACE FUNCTION; DROP+CREATE TRIGGER. + +BEGIN; + +ALTER TABLE trade_in_estimates + ADD COLUMN IF NOT EXISTS geom geometry(Point, 4326); + +-- Backfill from existing lat/lon (idempotent — only rows where geom is currently NULL). +UPDATE trade_in_estimates +SET geom = ST_SetSRID(ST_MakePoint(lon, lat), 4326) +WHERE geom IS NULL + AND lat IS NOT NULL + AND lon IS NOT NULL; + +CREATE INDEX IF NOT EXISTS trade_in_estimates_geom_idx + ON trade_in_estimates USING GIST (geom); + +CREATE OR REPLACE FUNCTION trade_in_estimates_set_geom() +RETURNS trigger AS $$ +BEGIN + IF NEW.lat IS NOT NULL AND NEW.lon IS NOT NULL THEN + NEW.geom := ST_SetSRID(ST_MakePoint(NEW.lon, NEW.lat), 4326); + ELSE + NEW.geom := NULL; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS trade_in_estimates_set_geom_trg ON trade_in_estimates; +CREATE TRIGGER trade_in_estimates_set_geom_trg + BEFORE INSERT OR UPDATE OF lat, lon ON trade_in_estimates + FOR EACH ROW EXECUTE FUNCTION trade_in_estimates_set_geom(); + +COMMIT; diff --git a/tradein-mvp/backend/data/sql/035_drop_duplicate_indexes.sql b/tradein-mvp/backend/data/sql/035_drop_duplicate_indexes.sql new file mode 100644 index 00000000..03ed4440 --- /dev/null +++ b/tradein-mvp/backend/data/sql/035_drop_duplicate_indexes.sql @@ -0,0 +1,20 @@ +-- 035_drop_duplicate_indexes.sql +-- Purpose: cleanup of redundant indexes identified in 2026-05-24 schema audit. +-- Dependencies: 028 (listing_sources_listing_idx), 050 (listing_sources_listing_idx2, houses_geom_idx2), 009 (houses_geom_idx). +-- Deploy order: any time after 050. +-- +-- Redundancy details: +-- * listing_sources_listing_idx2 (050) — exact duplicate of listing_sources_listing_idx (028). +-- * houses_geom_idx2 (050) — near-duplicate of houses_geom_idx (009); both are GIST on geom (full, non-partial). +-- The (050) variant adds no filter, so it's strictly redundant. +-- +-- Keep: listing_sources_listing_idx (028), houses_geom_idx (009). +-- +-- Idempotent: DROP INDEX IF EXISTS. + +BEGIN; + +DROP INDEX IF EXISTS listing_sources_listing_idx2; +DROP INDEX IF EXISTS houses_geom_idx2; + +COMMIT;