Card belongs to a studyset and carries only its position, a fractional index (last writer wins), so cards can be reordered and bulk imports keep their order. Its content lives in CardRevision rows, which are never updated except to blank them (ADR 0001, rule 2): parents lists the revisions a revision replaces (decided: a list, so a revision with two parents resolves a conflict), authorId the writer, reportId the report it answers once reports exist, and the envelope holds the content. Deciding the list of parents and the position now matters: changing a synced table later locks out older app versions. The migration (created by CLI) only adds: the two tables, an index on spaceId for each (space deletion cascades into them), one for a studyset's cards in order and one for a card's revisions, and the foreign keys, from the space with ON DELETE CASCADE and from the studyset and the card with ON DELETE CASCADE, DEFERRABLE INITIALLY DEFERRED, since synced rows can arrive in any order. The client migration creates the same tables on each device. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
141 lines
3.8 KiB
PL/PgSQL
141 lines
3.8 KiB
PL/PgSQL
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 "card" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid_v7(),
|
|
"spaceId" bigint,
|
|
"studysetId" uuid NOT NULL,
|
|
"position" text NOT NULL
|
|
);
|
|
|
|
-- Indexes
|
|
CREATE INDEX "card_space_idx" ON "card" USING btree ("spaceId");
|
|
CREATE INDEX "card_studyset_idx" ON "card" USING btree ("studysetId", "position");
|
|
|
|
--
|
|
-- ACTION CREATE TABLE
|
|
--
|
|
CREATE TABLE "card_revision" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid_v7(),
|
|
"spaceId" bigint,
|
|
"cardId" uuid NOT NULL,
|
|
"parents" json NOT NULL,
|
|
"authorId" uuid NOT NULL,
|
|
"reportId" uuid,
|
|
"keyId" text,
|
|
"payload" bytea NOT NULL,
|
|
"nonce" bytea
|
|
);
|
|
|
|
-- Indexes
|
|
CREATE INDEX "card_revision_space_idx" ON "card_revision" USING btree ("spaceId");
|
|
CREATE INDEX "card_revision_card_idx" ON "card_revision" USING btree ("cardId");
|
|
|
|
--
|
|
-- ACTION CREATE FOREIGN KEY
|
|
--
|
|
ALTER TABLE ONLY "card"
|
|
ADD CONSTRAINT "card_fk_0"
|
|
FOREIGN KEY("spaceId")
|
|
REFERENCES "offline_sync_spaces"("id")
|
|
ON DELETE CASCADE
|
|
ON UPDATE NO ACTION;
|
|
ALTER TABLE ONLY "card"
|
|
ADD CONSTRAINT "card_fk_1"
|
|
FOREIGN KEY("studysetId")
|
|
REFERENCES "studyset"("id")
|
|
ON DELETE CASCADE
|
|
ON UPDATE NO ACTION
|
|
DEFERRABLE INITIALLY DEFERRED;
|
|
|
|
--
|
|
-- ACTION CREATE FOREIGN KEY
|
|
--
|
|
ALTER TABLE ONLY "card_revision"
|
|
ADD CONSTRAINT "card_revision_fk_0"
|
|
FOREIGN KEY("spaceId")
|
|
REFERENCES "offline_sync_spaces"("id")
|
|
ON DELETE CASCADE
|
|
ON UPDATE NO ACTION;
|
|
ALTER TABLE ONLY "card_revision"
|
|
ADD CONSTRAINT "card_revision_fk_1"
|
|
FOREIGN KEY("cardId")
|
|
REFERENCES "card"("id")
|
|
ON DELETE CASCADE
|
|
ON UPDATE NO ACTION
|
|
DEFERRABLE INITIALLY DEFERRED;
|
|
|
|
|
|
--
|
|
-- MIGRATION VERSION FOR halkyon_learn
|
|
--
|
|
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
|
|
VALUES ('halkyon_learn', '20260930122235111-cards', now())
|
|
ON CONFLICT ("module")
|
|
DO UPDATE SET "version" = '20260930122235111-cards', "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 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;
|