Files
HealthLog/prisma/migrations/0476_token_apple_health_mirror/migration.sql
T
Marc-André Bombeck 8d10e5ab94 feat(measurements): resolve each reading's Apple Health write-back
A reading sent with a measurements:write token keeps its EXTERNAL source
and now points at the token that carried it (measurements.ingest_token_id).
Every stored row answers a server-resolved appleHealthMirror: true for
WITHINGS, IMPORT and MANUAL, the token's choice for EXTERNAL, false
otherwise. The plain list accepts appleHealthMirror=true so a client can
page exactly those rows.

Migration 0476 adds the columns and links existing EXTERNAL readings only
where one measurement token provably sent them; 0477 builds the index
concurrently. Backups carry the link and a restore keeps it only for a
token the account still holds. A guard holds every EXTERNAL writer to
setting the link.
2026-10-23 19:27:52 +02:00

81 lines
3.1 KiB
SQL

-- Per-token Apple Health write-back (#1272).
--
-- `api_tokens.mirror_to_apple_health` is the person's choice for a
-- `measurements:write` token; `mirror_changed_at` is when it last changed and
-- keys the `measurementMirror` domain of `/api/sync/changes`. Rows written by
-- such a token keep their honest `EXTERNAL` source and point at the token
-- through `measurements.ingest_token_id`; the server resolves the row's
-- `appleHealthMirror` from the token when it answers it.
--
-- Each step is a `DO` block with a bounded lock wait, and each is safe to run
-- again, so the file is marked for the boot's lock-timeout retry:
--
-- 1. the columns, and the foreign key added NOT VALID (no scan under the
-- table lock);
-- 2. the backfill: an existing EXTERNAL row is linked only where the
-- account has exactly one measurement token (revoked ones counted) and
-- the row is no older than that token, because only then is the token
-- provably the one that carried it. Every other existing row stays
-- unlinked and is not mirrored. The statement sets nothing but
-- `ingest_token_id`, so `updated_at` and `sync_version` are untouched;
-- 3. the foreign key validated, which scans without blocking writes.
--
-- The index the key and the feed walk use is built CONCURRENTLY in 0477, on
-- its own, because a concurrent build cannot share a file.
--
-- @retry-on-lock-timeout
DO $migration$
BEGIN
PERFORM set_config('lock_timeout', '5s', true);
ALTER TABLE "api_tokens"
ADD COLUMN IF NOT EXISTS "mirror_to_apple_health" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS "mirror_changed_at" TIMESTAMP(3);
ALTER TABLE "measurements"
ADD COLUMN IF NOT EXISTS "ingest_token_id" TEXT;
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'measurements_ingest_token_id_fkey'
) THEN
ALTER TABLE "measurements"
ADD CONSTRAINT "measurements_ingest_token_id_fkey"
FOREIGN KEY ("ingest_token_id") REFERENCES "api_tokens"("id")
ON DELETE SET NULL ON UPDATE CASCADE NOT VALID;
END IF;
END
$migration$;
DO $migration$
DECLARE
linked bigint;
BEGIN
PERFORM set_config('lock_timeout', '5s', true);
WITH sole_token AS (
SELECT t."user_id",
(array_agg(t."id"))[1] AS token_id,
(array_agg(t."created_at"))[1] AS created_at
FROM "api_tokens" t
WHERE t."permissions" = ARRAY['measurements:write']::text[]
GROUP BY t."user_id"
HAVING count(*) = 1
)
UPDATE "measurements" m
SET "ingest_token_id" = s.token_id
FROM sole_token s
WHERE m."user_id" = s."user_id"
AND m."source" = 'EXTERNAL'
AND m."ingest_token_id" IS NULL
AND m."created_at" >= s.created_at;
GET DIAGNOSTICS linked = ROW_COUNT;
RAISE NOTICE '0476: linked % EXTERNAL readings to their sole measurement token', linked;
END
$migration$;
DO $migration$
BEGIN
PERFORM set_config('lock_timeout', '5s', true);
ALTER TABLE "measurements"
VALIDATE CONSTRAINT "measurements_ingest_token_id_fkey";
END
$migration$;