Files
HealthLog/prisma/migrations/0264_whoop_owner_identity/migration.sql
T
Marc-André Bombeck 62eb431d66 release: v1.32.0 — hardening and scalability (#580)
* release: prepare v1.32.0

* test: stabilize water capture re-entry
2026-07-22 04:14:15 +02:00

27 lines
872 B
SQL

-- WHOOP provider identities are globally owned by one local account.
-- Existing duplicate identities keep the earliest connection; ties are broken
-- by the primary key so every database chooses the same winner.
ALTER TABLE "whoop_connections"
ALTER COLUMN "whoop_user_id" DROP NOT NULL;
WITH ranked_connections AS (
SELECT
"id",
ROW_NUMBER() OVER (
PARTITION BY "whoop_user_id"
ORDER BY "created_at" ASC, "id" ASC
) AS owner_rank
FROM "whoop_connections"
WHERE "whoop_user_id" IS NOT NULL
)
UPDATE "whoop_connections" AS connection
SET
"whoop_user_id" = NULL,
"updated_at" = CURRENT_TIMESTAMP
FROM ranked_connections AS ranked
WHERE connection."id" = ranked."id"
AND ranked.owner_rank > 1;
CREATE UNIQUE INDEX "whoop_connections_whoop_user_id_key"
ON "whoop_connections"("whoop_user_id");