Files
HealthLog/prisma/migrations/0219_v125_env_home_since/migration.sql
T
Marc-André Bombeck 6aad4a465e feat(environment): add home effective-from and reserve a DEVICE location source
Add `User.homeSince` (nullable effective-from instant) and a `DEVICE` value to
the location-source enum (migration 0219). `homeSince` records when the home
location became effective so day resolution can attribute a past day to the
home only on/after that instant, rather than to the current home for every past
day. Existing accounts with a home get `homeSince` stamped to the migration
instant; their deep past is filled by explicit location periods instead.

`DEVICE` is reserved for a future client-supplied coarse per-day location: the
enum value and the wire contract accept it now so the resolver can rank it
later without rework. Nothing writes it server-side yet.
2026-06-28 17:35:45 +02:00

37 lines
1.8 KiB
SQL

-- v1.25 (W-ENV) — conservative past-location handling for the environmental
-- context module.
--
-- Adds `users.home_since` (effective-from instant for the home location) and a
-- `DEVICE` value to the location-source enum. Day resolution attributes a past
-- day to the home location ONLY when the day is on/after `home_since`; earlier
-- days are skipped rather than fabricated from the current home. The deep past
-- is filled by explicit dated location periods instead.
--
-- `DEVICE` is the reserved forward path for a future client-supplied coarse
-- per-day location (iOS v2). The value exists so the resolver and the wire
-- contract already accept it; nothing writes it server-side yet.
--
-- Backfill of existing accounts: any row that already has a home location gets
-- `home_since` stamped to the migration instant. This is conservative — the
-- exact day a pre-upgrade account moved into its home is unknown, so history
-- before the upgrade is left to explicit location periods rather than guessed.
--
-- Additive; no existing weather row touched. Idempotent guards so reruns are
-- safe on prod.
--
-- Reversibility (down):
-- ALTER TABLE "users" DROP COLUMN IF EXISTS "home_since";
-- -- (the `DEVICE` enum value cannot be dropped in place; it is inert until a
-- -- future client writes it, so a roll-back leaves it harmless.)
-- AlterTable: effective-from instant for the home location (nullable).
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "home_since" TIMESTAMP(3);
-- Backfill: stamp existing home rows so they resolve from the upgrade forward.
UPDATE "users"
SET "home_since" = CURRENT_TIMESTAMP
WHERE "home_lat" IS NOT NULL AND "home_since" IS NULL;
-- AlterEnum: reserve the DEVICE source for the iOS v2 forward path.
ALTER TYPE "environment_location_source" ADD VALUE IF NOT EXISTS 'DEVICE';