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).
45 lines
1.6 KiB
PL/PgSQL
45 lines
1.6 KiB
PL/PgSQL
-- 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;
|