halkyon/apps/train/halkyon_train_server/migrations/20261103204323683-body-weights/migration.sql

114 lines
3.2 KiB
MySQL
Raw Normal View History

feat(train): weight, entered on Progress and read from Health Connect, sealed and synced (ADR 0030, D63) A person's weights are each a sealed `body_weight` row, as the days and the places: the weight in grams, when it was measured (UTC), where it came from and, for Health Connect's, the record's id and app are the payload; the server sees the space and the ciphertext alone. Progress gets a Weight card after Your body: the latest and its day, its line over time (held, each weight and its day), the plus to enter one (a comma taken as a point, 20 to 400 kg, when measured, today by default) and All, the list to delete one. The latest weight is also Your body's, which the band's and the zones' figures work from. In kilograms alone: Train is metric throughout and has no units setting yet. Health Connect's weight records are read in when switched on in its screen (READ_WEIGHT, asked as the others): each record once, its id kept in health_connect.json also after it is deleted here, never Train's own, and none within 10 minutes and 0.1 kg of a weight entered in Train. The migration (20261103204323683-body-weights): - creates (or replaces, unchanged) `gen_random_uuid_v7()`, the default of every UUID id, as each of Serverpod's migrations does; - CREATE TABLE "body_weight": "id" uuid primary key defaulting to a UUID v7, "spaceId" bigint (the sync space), "keyId" text (the key that sealed it), "payload" bytea NOT NULL (the sealed content), "nonce" bytea; - CREATE INDEX "body_weight_space_idx" on "spaceId", for a space's rows as the sync reads them; - a foreign key from "spaceId" to "offline_sync_spaces"("id"), ON DELETE CASCADE, so that an account's deletion takes its weights with its space; - the version rows of Train's and every module's migrations. Nothing existing changes, so nothing is lost. Tests: the row written and read back in its owner's space, and the denials: another person reads none of them, and writing one into another's space throws OfflineSyncSpaceMembershipException and leaves it empty; an account's deletion takes them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-11-03 22:11:57 +01:00
BEGIN;
--
-- Function: gen_random_uuid_v7()
-- Source: https://gist.github.com/kjmph/5bd772b2c2df145aa645b837da7eca74
-- License: MIT (copyright notice included on the generator source code).
--
create or replace function gen_random_uuid_v7()
returns uuid
as $$
begin
-- use random v4 uuid as starting point (which has the same variant we need)
-- then overlay timestamp
-- then set version 7 by flipping the 2 and 1 bit in the version 4 string
return encode(
set_bit(
set_bit(
overlay(uuid_send(gen_random_uuid())
placing substring(int8send(floor(extract(epoch from clock_timestamp()) * 1000)::bigint) from 3)
from 1 for 6
),
52, 1
),
53, 1
),
'hex')::uuid;
end
$$
language plpgsql
volatile;
--
-- ACTION CREATE TABLE
--
CREATE TABLE "body_weight" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid_v7(),
"spaceId" bigint,
"keyId" text,
"payload" bytea NOT NULL,
"nonce" bytea
);
-- Indexes
CREATE INDEX "body_weight_space_idx" ON "body_weight" USING btree ("spaceId");
--
-- ACTION CREATE FOREIGN KEY
--
ALTER TABLE ONLY "body_weight"
ADD CONSTRAINT "body_weight_fk_0"
FOREIGN KEY("spaceId")
REFERENCES "offline_sync_spaces"("id")
ON DELETE CASCADE
ON UPDATE NO ACTION;
--
-- MIGRATION VERSION FOR halkyon_train
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('halkyon_train', '20261103204323683-body-weights', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20261103204323683-body-weights', "timestamp" = now();
--
-- MIGRATION VERSION FOR halkyon_base
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('halkyon_base', '20261026140329453-assistant-budgets', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20261026140329453-assistant-budgets', "timestamp" = now();
--
-- MIGRATION VERSION FOR serverpod
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('serverpod', '20260824182259319', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20260824182259319', "timestamp" = now();
--
-- MIGRATION VERSION FOR serverpod_auth_idp
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('serverpod_auth_idp', '20260924105404509', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20260924105404509', "timestamp" = now();
--
-- MIGRATION VERSION FOR serverpod_offline_sync
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('serverpod_offline_sync', '20260914143806119', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20260914143806119', "timestamp" = now();
--
-- MIGRATION VERSION FOR halkyon_oidc
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('halkyon_oidc', '20261023180644988', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20261023180644988', "timestamp" = now();
--
-- MIGRATION VERSION FOR serverpod_auth_core
--
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
VALUES ('serverpod_auth_core', '20260924105232991', now())
ON CONFLICT ("module")
DO UPDATE SET "version" = '20260924105232991', "timestamp" = now();
COMMIT;