fix(tradein-sql): rename dup 030, add trade_in_estimates geom, drop dup indexes (#500)
Close 3 SQL schema findings from 2026-05-24 trade-in audit. - Rename 030_listings_alter_yandex.sql -> 033_*.sql (resolve dup prefix with 030_avito_imv_cache_key_unique.sql). - New 034_trade_in_estimates_geom.sql: PostGIS geom column + GIST index + backfill + BEFORE INSERT/UPDATE trigger. - New 035_drop_duplicate_indexes.sql: drop listing_sources_listing_idx2 (dup of 028) and houses_geom_idx2 (dup of 009). All idempotent. Deploy script uses ls|sort + no tracking table — rename is safe because original 030 already applied (IF NOT EXISTS no-op on re-run as 033).
This commit is contained in:
parent
ed49f6b953
commit
02b48b8d04
4 changed files with 67 additions and 2 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
45
tradein-mvp/backend/data/sql/034_trade_in_estimates_geom.sql
Normal file
45
tradein-mvp/backend/data/sql/034_trade_in_estimates_geom.sql
Normal file
|
|
@ -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;
|
||||
20
tradein-mvp/backend/data/sql/035_drop_duplicate_indexes.sql
Normal file
20
tradein-mvp/backend/data/sql/035_drop_duplicate_indexes.sql
Normal file
|
|
@ -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;
|
||||
Loading…
Add table
Reference in a new issue