Commit graph

3 commits

Author SHA1 Message Date
lekss361
ac18ef32f9 fix(db): retire ALL legacy cad_buildings index names before rename (#168 hotfix2)
PR #173 fixed cad_buildings_pkey but missed 5 other indexes. Same root cause:
PG does NOT auto-rename indexes on table rename → all 6 backing indexes
remain on cad_buildings_old_apr26 with original names.

Add 5 more `ALTER INDEX IF EXISTS ... RENAME TO ..._old_apr26_*` statements
for: geom_gist, quarter_idx, objdoc_idx, complex_idx, purpose_idx.

Migration 92 still NOT in _schema_migrations (4× rollback now). Next deploy
will re-apply cleanly.
2026-05-15 14:05:23 +03:00
lekss361
7b2d1782e9
fix(db): retire legacy cad_buildings_pkey before rename — fix migration 92 (#168 hotfix) (#173)
Migration 92_cad_bulk_layers.sql fails on deploy with:
  ERROR: relation "cad_buildings_pkey" already exists
  CONTEXT: ALTER INDEX cad_buildings_new_pkey RENAME TO cad_buildings_pkey

Root cause: K.1 renamed cad_buildings → cad_buildings_old_apr26, but PG
does NOT auto-rename the backing PK index on table rename. The legacy
cad_buildings_pkey index is still attached to cad_buildings_old_apr26.
K.3 then tries to rename cad_buildings_new_pkey to the same name → collision.

Fix: add an explicit `ALTER INDEX IF EXISTS cad_buildings_pkey RENAME TO
cad_buildings_old_apr26_pkey` BEFORE the new rename. Idempotent (IF EXISTS).

Migration 92 was rolled back in last 3 deploy attempts (not in
_schema_migrations), so this edit will re-apply cleanly on next deploy.

Co-authored-by: lekss361 <claudestars@proton.me>
2026-05-15 13:59:53 +03:00
lekss361
59f222eaf3
feat(db): bulk cadastre schema — 7 tables + cadastre_jobs (#168 PR1/5) (#169)
* feat(db): schema migration for bulk cadastre ingest layers (#168)

Add 7 cadastre layer tables + cadastre_jobs saga-state table.

Tables:
- cad_parcels (36368 ЗУ ЕГРН) replaces cad_parcels_geom
- cad_buildings v2 wide schema (replaces existing, backed up as cad_buildings_old_apr26)
- cad_constructions (36383 Сооружения + ЕГРН, flag is_egrn)
- cad_oncs (36384), cad_enk (39663), cad_zouit (4 categoryIds), cad_quarter_stats
- cadastre_jobs (resumable saga state, mirrors geo_jobs pattern)

GIST + BTREE indexes. Migration of cad_parcels_geom data via INSERT...SELECT.
Restore v_complex_full + v_complex_buildings views after table rename.

* fixup(db): address PR #169 bot review — 2 blockers + 3 lessers

Blocker #1: cad_parcels_geom DROP → back-compat VIEW
  Old DROP TABLE would break site-finder analyze fallback (per 83_*.sql COMMENT).
  Now: drop old BASE TABLE (guarded by table_type), create VIEW over cad_parcels
  with identical 4-column signature. Callers continue to work; PR2/5 will switch.

Blocker #2: K.3 redundant RENAME CONSTRAINT removed
  ALTER INDEX RENAME on PK index already renames the backing constraint in PG16.
  Second ALTER TABLE RENAME CONSTRAINT would throw 'does not exist' and abort tx.

Lesser #1: cadastre_jobs.status CHECK constraint
  Inline CHECK (status IN ('queued','running','done','failed','zombie','cancelled','paused'))
  per partial-index intent and admin UI plan.

Lesser #2: COMMENT ON TABLE for all 8 tables + view (keeps convention from 63, 83).

Lesser #3: set_updated_at() triggers on 7 tables with updated_at column.
  Reuses existing function (verified present). DO $$ guards on pg_trigger
  for idempotent re-apply. cadastre_jobs excluded (uses heartbeat_at instead).

---------

Co-authored-by: lekss361 <claudestars@proton.me>
2026-05-15 13:05:25 +03:00