diff --git a/src/app/db/migrations/0257_low_songbird.sql b/src/app/db/migrations/0257_low_songbird.sql index 1647da23..18117e84 100644 --- a/src/app/db/migrations/0257_low_songbird.sql +++ b/src/app/db/migrations/0257_low_songbird.sql @@ -1,4 +1,4 @@ -CREATE INDEX CONCURRENTLY "ix_epc_building_part_epc_property" ON "epc_building_part" USING btree ("epc_property_id");--> statement-breakpoint -CREATE INDEX CONCURRENTLY "ix_epc_energy_element_epc_property" ON "epc_energy_element" USING btree ("epc_property_id");--> statement-breakpoint -CREATE INDEX CONCURRENTLY "ix_epc_floor_dimension_building_part" ON "epc_floor_dimension" USING btree ("epc_building_part_id");--> statement-breakpoint -CREATE INDEX CONCURRENTLY "ix_property_portfolio" ON "property" USING btree ("portfolio_id"); \ No newline at end of file +CREATE INDEX IF NOT EXISTS "ix_epc_building_part_epc_property" ON "epc_building_part" USING btree ("epc_property_id");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "ix_epc_energy_element_epc_property" ON "epc_energy_element" USING btree ("epc_property_id");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "ix_epc_floor_dimension_building_part" ON "epc_floor_dimension" USING btree ("epc_building_part_id");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "ix_property_portfolio" ON "property" USING btree ("portfolio_id"); diff --git a/src/app/db/sql/prebuild-reporting-indexes.sql b/src/app/db/sql/prebuild-reporting-indexes.sql new file mode 100644 index 00000000..2e6a9738 --- /dev/null +++ b/src/app/db/sql/prebuild-reporting-indexes.sql @@ -0,0 +1,21 @@ +-- Production pre-build for migration 0257 (reporting / EPC-graph FK indexes). +-- +-- The drizzle migrator (src/app/db/migrate.ts) runs migrations inside a +-- transaction, where CREATE INDEX CONCURRENTLY is not allowed. On a live, +-- loaded database, run this file first via psql — it builds the same four +-- indexes without blocking reads or writes — and 0257's +-- CREATE INDEX IF NOT EXISTS then no-ops. On dev/CI databases the tables are +-- small, so skipping this and letting 0257 build them directly is fine. +-- +-- psql "$DATABASE_URL" -f src/app/db/sql/prebuild-reporting-indexes.sql +-- +-- CONCURRENTLY waits out in-flight transactions between build phases, so +-- long-running reporting queries can stall it — prefer a quiet window. If a +-- build is interrupted it leaves an INVALID index (visible in \d+