freshyo/apps/backend/dev-migration-region-bound-centers.sql
2026-10-15 22:02:24 +05:30

27 lines
1.2 KiB
SQL

-- DEV ONLY — the local dev DB has already run 0006, so this applies just the
-- region-bound centre addition that 0006 now carries for a fresh database.
--
-- Run once, against the dev database:
-- bunx wrangler d1 execute freshyo-dev \
-- --file=apps/backend/dev-migration-region-bound-centers.sql \
-- --config wrangler.dev.toml
--
-- Afterwards the app keeps these fresh: every bound create/update recomputes the
-- centre (boundCenter in superadmin-apis/regions.ts).
ALTER TABLE `region_bounds` ADD COLUMN `center_lat` real;
--> statement-breakpoint
ALTER TABLE `region_bounds` ADD COLUMN `center_lng` real;
--> statement-breakpoint
-- Bounding-box centre of each bound's GeoJSON. Reads the first feature's outer
-- ring (the shape every seeded bound currently uses).
UPDATE `region_bounds` SET
`center_lat` = (
SELECT (MIN(CAST(json_extract(value, '$[1]') AS REAL)) + MAX(CAST(json_extract(value, '$[1]') AS REAL))) / 2
FROM json_each(`region_bounds`.`geojson`, '$.features[0].geometry.coordinates[0]')
),
`center_lng` = (
SELECT (MIN(CAST(json_extract(value, '$[0]') AS REAL)) + MAX(CAST(json_extract(value, '$[0]') AS REAL))) / 2
FROM json_each(`region_bounds`.`geojson`, '$.features[0].geometry.coordinates[0]')
);