152 lines
4.5 KiB
MySQL
152 lines
4.5 KiB
MySQL
|
|
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 "shared_space" (
|
||
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid_v7(),
|
||
|
|
"kind" text NOT NULL,
|
||
|
|
"createdAt" timestamp without time zone NOT NULL,
|
||
|
|
"joinByLink" boolean NOT NULL DEFAULT false,
|
||
|
|
"membersInvite" boolean NOT NULL DEFAULT false
|
||
|
|
);
|
||
|
|
|
||
|
|
--
|
||
|
|
-- ACTION CREATE TABLE
|
||
|
|
--
|
||
|
|
CREATE TABLE "shared_space_invitation" (
|
||
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid_v7(),
|
||
|
|
"sharedSpaceId" uuid NOT NULL,
|
||
|
|
"authUserId" uuid NOT NULL,
|
||
|
|
"role" text NOT NULL,
|
||
|
|
"invitedBy" uuid NOT NULL,
|
||
|
|
"invitedAt" timestamp without time zone NOT NULL
|
||
|
|
);
|
||
|
|
|
||
|
|
-- Indexes
|
||
|
|
CREATE UNIQUE INDEX "shared_space_invitation_space_person_idx" ON "shared_space_invitation" USING btree ("sharedSpaceId", "authUserId");
|
||
|
|
CREATE INDEX "shared_space_invitation_person_idx" ON "shared_space_invitation" USING btree ("authUserId");
|
||
|
|
|
||
|
|
--
|
||
|
|
-- ACTION CREATE TABLE
|
||
|
|
--
|
||
|
|
CREATE TABLE "shared_space_member" (
|
||
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid_v7(),
|
||
|
|
"sharedSpaceId" uuid NOT NULL,
|
||
|
|
"authUserId" uuid NOT NULL,
|
||
|
|
"role" text NOT NULL,
|
||
|
|
"joinedAt" timestamp without time zone NOT NULL
|
||
|
|
);
|
||
|
|
|
||
|
|
-- Indexes
|
||
|
|
CREATE UNIQUE INDEX "shared_space_member_space_person_idx" ON "shared_space_member" USING btree ("sharedSpaceId", "authUserId");
|
||
|
|
CREATE INDEX "shared_space_member_person_idx" ON "shared_space_member" USING btree ("authUserId");
|
||
|
|
|
||
|
|
--
|
||
|
|
-- ACTION CREATE FOREIGN KEY
|
||
|
|
--
|
||
|
|
ALTER TABLE ONLY "shared_space_invitation"
|
||
|
|
ADD CONSTRAINT "shared_space_invitation_fk_0"
|
||
|
|
FOREIGN KEY("sharedSpaceId")
|
||
|
|
REFERENCES "shared_space"("id")
|
||
|
|
ON DELETE CASCADE
|
||
|
|
ON UPDATE NO ACTION;
|
||
|
|
|
||
|
|
--
|
||
|
|
-- ACTION CREATE FOREIGN KEY
|
||
|
|
--
|
||
|
|
ALTER TABLE ONLY "shared_space_member"
|
||
|
|
ADD CONSTRAINT "shared_space_member_fk_0"
|
||
|
|
FOREIGN KEY("sharedSpaceId")
|
||
|
|
REFERENCES "shared_space"("id")
|
||
|
|
ON DELETE CASCADE
|
||
|
|
ON UPDATE NO ACTION;
|
||
|
|
|
||
|
|
|
||
|
|
--
|
||
|
|
-- MIGRATION VERSION FOR halkyon_todo
|
||
|
|
--
|
||
|
|
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
|
||
|
|
VALUES ('halkyon_todo', '20261111091721303-shared-spaces', now())
|
||
|
|
ON CONFLICT ("module")
|
||
|
|
DO UPDATE SET "version" = '20261111091721303-shared-spaces', "timestamp" = now();
|
||
|
|
|
||
|
|
--
|
||
|
|
-- MIGRATION VERSION FOR halkyon_base
|
||
|
|
--
|
||
|
|
INSERT INTO "serverpod_migrations" ("module", "version", "timestamp")
|
||
|
|
VALUES ('halkyon_base', '20261111091525523-shared-spaces', now())
|
||
|
|
ON CONFLICT ("module")
|
||
|
|
DO UPDATE SET "version" = '20261111091525523-shared-spaces', "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;
|