Skip to content

Commit bd4e58d

Browse files
authored
Add unique keys to schema (#178)
1 parent b44f888 commit bd4e58d

9 files changed

Lines changed: 150 additions & 34 deletions

File tree

packages/database/schema.puml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class "Account" [[{A user account on a platform}]] {
99
{field} id : integer
1010
{field} write_permission : boolean
1111
{field} active : boolean
12+
{field} account_local_id : string
1213
}
1314
class "Space" [[{A space on a platform representing a community engaged in a conversation}]] {
1415
{field} id : integer
@@ -104,7 +105,7 @@ class "AutomatedAgent" [[{An automated agent}]] {
104105
{field} id(i) : integer
105106
{field} type(i) : EntityType
106107
}
107-
"Account" --> "1" "Agent" : "person"
108+
"Account" --> "1" "Agent" : "agent"
108109
"Agent" ^-- "Person"
109110
"Agent" ^-- "AutomatedAgent"
110111
@enduml

packages/database/schema.svg

Lines changed: 1 addition & 1 deletion
Loading

packages/database/schema.yaml

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ classes:
120120
email:
121121
required: true
122122
# TODO: known skills, i.e. what processes can they confirm.
123+
unique_keys:
124+
person_email:
125+
unique_key_slots:
126+
- email
127+
person_orcid:
128+
unique_key_slots:
129+
- orcid
123130
AutomatedAgent:
124131
description: An automated agent
125132
is_a: Agent
@@ -132,22 +139,28 @@ classes:
132139
ifabsent: false
133140
version:
134141
range: string
135-
142+
unique_keys:
143+
automated_agent_name_version:
144+
unique_key_slots:
145+
- name
146+
- version
136147
Platform:
137148
description: A data platform where discourse happens
138149
slots:
139150
- id
140151
- name
141-
attributes:
142-
url:
143-
required: true
152+
- url
153+
unique_keys:
154+
platform_url:
155+
unique_key_slots:
156+
- url
144157
Account:
145158
description: A user account on a platform
146159
slots:
147160
- id
148161
- platform
149162
attributes:
150-
person:
163+
agent:
151164
range: Agent
152165
required: true
153166
write_permission:
@@ -157,6 +170,14 @@ classes:
157170
range: boolean
158171
required: true
159172
ifabsent: true
173+
account_local_id:
174+
required: true
175+
description: The identity of the person in this space
176+
unique_keys:
177+
account_platform_and_local_id:
178+
unique_key_slots:
179+
- platform
180+
- account_local_id
160181
Space:
161182
description: A space on a platform representing a community engaged in a conversation
162183
slots:
@@ -167,6 +188,10 @@ classes:
167188
platform:
168189
range: Platform
169190
required: true
191+
unique_keys:
192+
space_url:
193+
unique_key_slots:
194+
- url
170195
SpaceAccess:
171196
description: An access control entry for a space
172197
slots:
@@ -205,6 +230,12 @@ classes:
205230
part_of:
206231
description: This content is part of a larger content unit
207232
range: Content
233+
unique_keys:
234+
content_space_and_local_id:
235+
unique_key_slots:
236+
- space
237+
- source_local_id
238+
208239
# ContentDerivation:
209240
# description: A derivation relation between content units
210241
# attributes:
@@ -226,15 +257,24 @@ classes:
226257
- id
227258
- space
228259
- source_local_id
229-
- url
230260
- created
231261
- metadata
232262
- last_modified
233263
- author
234264
- contributors
235265
attributes:
266+
url:
267+
range: string
236268
contents:
237269
range: blob
270+
unique_keys:
271+
document_space_and_local_id:
272+
unique_key_slots:
273+
- space
274+
- source_local_id
275+
document_url:
276+
unique_key_slots:
277+
- url
238278
# Article:
239279
# description: an article
240280
# is_a: Document
@@ -303,6 +343,11 @@ classes:
303343
# Damn, concept schema is a concept, is it not?
304344
# Now, if a concept has a complex structwre based on a complex content...
305345
# AH, it should be based on occurences.
346+
unique_keys:
347+
concept_space_and_name:
348+
unique_key_slots:
349+
- space
350+
- name
306351

307352
ConceptSchema:
308353
is_a: Concept
@@ -379,6 +424,7 @@ slots:
379424
range: string
380425
url:
381426
range: string
427+
required: true
382428
platform:
383429
range: Platform
384430
required: true
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
-- Space
2+
3+
CREATE UNIQUE INDEX IF NOT EXISTS platform_url_idx ON public."Platform" USING btree (url);
4+
5+
ALTER TABLE public."Space" ALTER COLUMN url SET NOT NULL;
6+
7+
CREATE UNIQUE INDEX IF NOT EXISTS space_url_idx ON public."Space" USING btree (url);
8+
9+
-- Agents
10+
11+
ALTER TABLE public."AutomatedAgent" ALTER COLUMN version SET NOT NULL;
12+
13+
CREATE UNIQUE INDEX IF NOT EXISTS automated_agent_name_version_idx ON public."AutomatedAgent" USING btree (name, version);
14+
15+
CREATE UNIQUE INDEX IF NOT EXISTS person_email_idx ON public."Person" USING btree (email);
16+
CREATE UNIQUE INDEX IF NOT EXISTS person_orcid_idx ON public."Person" USING btree (orcid);
17+
18+
-- Account
19+
20+
ALTER TABLE public."Account" RENAME COLUMN "person_id" TO "agent_id";
21+
22+
ALTER TABLE public."Account" RENAME CONSTRAINT "Account_person_id_fkey" TO "Account_agent_id_fkey";
23+
24+
ALTER TABLE public."Account" ADD COLUMN account_local_id character varying;
25+
26+
UPDATE public."Account" SET account_local_id = (SELECT email FROM public."Person" AS p WHERE p.id = agent_id);
27+
28+
ALTER TABLE public."Account" ALTER COLUMN "account_local_id" SET NOT NULL;
29+
30+
CREATE UNIQUE INDEX IF NOT EXISTS account_platform_and_local_id_idx ON public."Account" USING btree (platform_id, account_local_id);
31+
32+
-- Document and Content
33+
34+
CREATE UNIQUE INDEX IF NOT EXISTS document_space_and_local_id_idx ON public."Document" USING btree (space_id, source_local_id) WHERE (space_id IS NOT NULL);
35+
CREATE UNIQUE INDEX IF NOT EXISTS document_url_idx ON public."Document" USING btree (url);
36+
37+
DROP INDEX IF EXISTS public."Content_space_and_id";
38+
39+
CREATE UNIQUE INDEX IF NOT EXISTS content_space_and_local_id_idx ON public."Content" USING btree (space_id, source_local_id) WHERE (space_id IS NOT NULL);
40+
41+
-- Concept
42+
43+
ALTER TABLE public."Concept" ALTER COLUMN "space_id" set not null;
44+
45+
CREATE UNIQUE INDEX IF NOT EXISTS concept_space_and_name_idx ON public."Concept" USING btree (space_id, name);
46+
47+
48+
-- SpaceAccess
49+
50+
ALTER TABLE public."SpaceAccess" DROP CONSTRAINT "SpaceAccess_account_id_space_id_key";
51+
52+
ALTER TABLE public."SpaceAccess" DROP CONSTRAINT "SpaceAccess_pkey";
53+
54+
ALTER TABLE public."SpaceAccess" DROP COLUMN "id";
55+
ALTER TABLE public."SpaceAccess" ALTER COLUMN "space_id" set not null;
56+
57+
CREATE UNIQUE INDEX IF NOT EXISTS "SpaceAccess_pkey" ON public."SpaceAccess" USING btree (space_id, account_id);
58+
59+
ALTER TABLE public."SpaceAccess" add constraint "SpaceAccess_pkey" PRIMARY KEY using index "SpaceAccess_pkey";
60+
61+
COMMENT ON COLUMN public."SpaceAccess".account_id IS 'The identity of the account in this space';

packages/database/supabase/schemas/account.sql

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ CREATE TABLE IF NOT EXISTS public."Account" (
33
'public.entity_id_seq'::regclass
44
) NOT NULL,
55
platform_id bigint NOT NULL,
6-
person_id bigint NOT NULL,
6+
agent_id bigint NOT NULL,
7+
account_local_id varchar NOT NULL,
78
write_permission boolean NOT NULL,
89
active boolean DEFAULT true NOT NULL
910
);
@@ -14,8 +15,8 @@ COMMENT ON TABLE public."Account" IS 'A user account on a platform';
1415

1516

1617
ALTER TABLE ONLY public."Account"
17-
ADD CONSTRAINT "Account_person_id_fkey" FOREIGN KEY (
18-
person_id
18+
ADD CONSTRAINT "Account_agent_id_fkey" FOREIGN KEY (
19+
agent_id
1920
) REFERENCES public."Agent" (id) ON UPDATE CASCADE ON DELETE CASCADE;
2021

2122
ALTER TABLE ONLY public."Account"
@@ -28,23 +29,16 @@ ADD CONSTRAINT "Account_platform_id_fkey" FOREIGN KEY (
2829
ALTER TABLE ONLY public."Account"
2930
ADD CONSTRAINT "Account_pkey" PRIMARY KEY (id);
3031

32+
CREATE UNIQUE INDEX account_platform_and_local_id_idx ON public."Account" USING btree (platform_id, account_local_id);
3133

3234
CREATE TABLE IF NOT EXISTS public."SpaceAccess" (
33-
id bigint DEFAULT nextval(
34-
'public.entity_id_seq'::regclass
35-
) NOT NULL,
3635
space_id bigint,
3736
account_id bigint NOT NULL,
3837
editor boolean NOT NULL
3938
);
4039

4140
ALTER TABLE ONLY public."SpaceAccess"
42-
ADD CONSTRAINT "SpaceAccess_account_id_space_id_key" UNIQUE (
43-
account_id, space_id
44-
);
45-
46-
ALTER TABLE ONLY public."SpaceAccess"
47-
ADD CONSTRAINT "SpaceAccess_pkey" PRIMARY KEY (id);
41+
ADD CONSTRAINT "SpaceAccess_pkey" PRIMARY KEY (space_id, account_id);
4842

4943

5044
ALTER TABLE public."SpaceAccess" OWNER TO "postgres";
@@ -53,6 +47,7 @@ COMMENT ON TABLE public."SpaceAccess" IS 'An access control entry for a space';
5347

5448
COMMENT ON COLUMN public."SpaceAccess".space_id IS 'The space in which the content is located';
5549

50+
COMMENT ON COLUMN public."SpaceAccess".account_id IS 'The identity of the account in this space';
5651

5752
ALTER TABLE ONLY public."SpaceAccess"
5853
ADD CONSTRAINT "SpaceAccess_account_id_fkey" FOREIGN KEY (

packages/database/supabase/schemas/agent.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS public."AutomatedAgent" (
1818
name character varying NOT NULL,
1919
metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
2020
deterministic boolean DEFAULT false,
21-
version character varying
21+
version character varying NOT NULL
2222
);
2323

2424
ALTER TABLE ONLY public."AutomatedAgent"
@@ -29,6 +29,7 @@ ADD CONSTRAINT automated_agent_id_fkey FOREIGN KEY (
2929
id
3030
) REFERENCES public."Agent" (id) ON UPDATE CASCADE ON DELETE CASCADE;
3131

32+
CREATE UNIQUE INDEX automated_agent_name_version_idx ON public."AutomatedAgent" USING btree (name, version);
3233

3334
ALTER TABLE public."AutomatedAgent" OWNER TO "postgres";
3435

@@ -49,6 +50,8 @@ ADD CONSTRAINT person_id_fkey FOREIGN KEY (
4950
id
5051
) REFERENCES public."Agent" (id) ON UPDATE CASCADE ON DELETE CASCADE;
5152

53+
CREATE UNIQUE INDEX person_email_idx ON public."Person" USING btree (email);
54+
CREATE UNIQUE INDEX person_orcid_idx ON public."Person" USING btree (orcid);
5255

5356
ALTER TABLE public."Person" OWNER TO "postgres";
5457

packages/database/supabase/schemas/concept.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ CREATE TABLE IF NOT EXISTS public."Concept" (
2323
author_id bigint,
2424
created timestamp without time zone NOT NULL,
2525
last_modified timestamp without time zone NOT NULL,
26-
space_id bigint,
26+
space_id bigint NOT NULL,
2727
arity smallint DEFAULT 0 NOT NULL,
2828
schema_id bigint,
2929
content jsonb DEFAULT '{}'::jsonb NOT NULL,
@@ -64,6 +64,8 @@ CREATE UNIQUE INDEX "Concept_represented_by" ON public."Concept" (
6464
represented_by_id
6565
);
6666

67+
CREATE UNIQUE INDEX concept_space_and_name_idx ON public."Concept" (space_id, name);
68+
6769

6870
ALTER TABLE ONLY public."Concept"
6971
ADD CONSTRAINT "Concept_author_id_fkey" FOREIGN KEY (

packages/database/supabase/schemas/content.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ ADD CONSTRAINT "Document_space_id_fkey" FOREIGN KEY (
4242
id
4343
) ON UPDATE CASCADE ON DELETE CASCADE;
4444

45+
CREATE UNIQUE INDEX document_space_and_local_id_idx ON public."Document" USING btree (space_id, source_local_id)
46+
NULLS DISTINCT WHERE space_id IS NOT NULL;
47+
48+
CREATE UNIQUE INDEX document_url_idx ON public."Document" USING btree (url);
49+
4550
ALTER TABLE public."Document" OWNER TO "postgres";
4651

4752
COMMENT ON COLUMN public."Document".space_id IS 'The space in which the content is located';
@@ -114,9 +119,9 @@ CREATE INDEX "Content_part_of" ON public."Content" USING btree (
114119

115120
CREATE INDEX "Content_space" ON public."Content" USING btree (space_id);
116121

117-
CREATE UNIQUE INDEX "Content_space_and_id" ON public."Content" USING btree (
122+
CREATE UNIQUE INDEX content_space_and_local_id_idx ON public."Content" USING btree (
118123
space_id, source_local_id
119-
) WHERE (source_local_id IS NOT NULL);
124+
) NULLS DISTINCT WHERE (space_id IS NOT NULL);
120125

121126
CREATE INDEX "Content_text" ON public."Content" USING pgroonga (text);
122127

packages/database/supabase/schemas/space.sql

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ CREATE TABLE IF NOT EXISTS public."Platform" (
66
url character varying NOT NULL
77
);
88

9-
ALTER TABLE public."Platform" OWNER TO "postgres";
9+
ALTER TABLE ONLY public."Platform"
10+
ADD CONSTRAINT "Platform_pkey" PRIMARY KEY (id);
11+
12+
CREATE UNIQUE INDEX platform_url_idx ON public."Platform" USING btree (url);
1013

1114
COMMENT ON TABLE public."Platform" IS
1215
'A data platform where discourse happens';
@@ -15,29 +18,29 @@ CREATE TABLE IF NOT EXISTS public."Space" (
1518
id bigint DEFAULT nextval(
1619
'public."entity_id_seq"'::regclass
1720
) NOT NULL,
18-
url character varying,
21+
url character varying NOT NULL,
1922
name character varying NOT NULL,
2023
platform_id bigint NOT NULL
2124
);
2225

23-
ALTER TABLE public."Space" OWNER TO "postgres";
24-
25-
COMMENT ON TABLE public."Space" IS
26-
'A space on a platform representing a community engaged in a conversation';
27-
28-
ALTER TABLE ONLY public."Platform"
29-
ADD CONSTRAINT "Platform_pkey" PRIMARY KEY (id);
30-
3126
ALTER TABLE ONLY public."Space"
3227
ADD CONSTRAINT "Space_pkey" PRIMARY KEY (id);
3328

29+
CREATE UNIQUE INDEX space_url_idx ON public."Space" USING btree (url);
30+
3431
ALTER TABLE ONLY public."Space"
3532
ADD CONSTRAINT "Space_platform_id_fkey" FOREIGN KEY (
3633
platform_id
3734
) REFERENCES public."Platform" (
3835
id
3936
) ON UPDATE CASCADE ON DELETE CASCADE;
4037

38+
COMMENT ON TABLE public."Space" IS
39+
'A space on a platform representing a community engaged in a conversation';
40+
41+
ALTER TABLE public."Platform" OWNER TO "postgres";
42+
ALTER TABLE public."Space" OWNER TO "postgres";
43+
4144
GRANT ALL ON TABLE public."Platform" TO anon;
4245
GRANT ALL ON TABLE public."Platform" TO authenticated;
4346
GRANT ALL ON TABLE public."Platform" TO service_role;

0 commit comments

Comments
 (0)