All checks were successful
Deploy production / deploy (push) Successful in 1m13s
33 lines
1.7 KiB
PL/PgSQL
33 lines
1.7 KiB
PL/PgSQL
CREATE TYPE "worldtodo"."location_precision" AS ENUM('exact', 'approximate', 'none');--> statement-breakpoint
|
|
ALTER TABLE "worldtodo"."tasks" ADD COLUMN "location_precision" "worldtodo"."location_precision";--> statement-breakpoint
|
|
-- Hand-written: location_precision follows scale and lat/lon (map pins, tracker #4641).
|
|
-- street/city = exact, region/country = approximate (rough centre), planet or no coordinates
|
|
-- = none. A trigger instead of a generated column so drizzle-kit can diff the enum column.
|
|
CREATE OR REPLACE FUNCTION worldtodo.set_location_precision() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
NEW.location_precision := CASE
|
|
WHEN NEW.lat IS NULL OR NEW.lon IS NULL OR NEW.scale = 'planet' THEN 'none'
|
|
WHEN NEW.scale IN ('street', 'city') THEN 'exact'
|
|
ELSE 'approximate'
|
|
END::worldtodo.location_precision;
|
|
RETURN NEW;
|
|
END $$;
|
|
--> statement-breakpoint
|
|
DROP TRIGGER IF EXISTS tasks_location_precision ON worldtodo.tasks;
|
|
--> statement-breakpoint
|
|
CREATE TRIGGER tasks_location_precision BEFORE INSERT OR UPDATE OF scale, lat, lon, location_precision
|
|
ON worldtodo.tasks FOR EACH ROW EXECUTE FUNCTION worldtodo.set_location_precision();
|
|
--> statement-breakpoint
|
|
-- Backfill (the trigger computes it) without bumping updated_at: touch_updated_at is paused
|
|
-- for this one statement when it exists.
|
|
DO $$
|
|
DECLARE has_touch boolean := EXISTS (
|
|
SELECT 1 FROM pg_trigger WHERE tgrelid = 'worldtodo.tasks'::regclass AND tgname = 'touch_updated_at'
|
|
);
|
|
BEGIN
|
|
IF has_touch THEN ALTER TABLE worldtodo.tasks DISABLE TRIGGER touch_updated_at; END IF;
|
|
UPDATE worldtodo.tasks SET location_precision = NULL;
|
|
IF has_touch THEN ALTER TABLE worldtodo.tasks ENABLE TRIGGER touch_updated_at; END IF;
|
|
END $$;
|