-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseed.sql
573 lines (474 loc) · 17.6 KB
/
seed.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
---- Profiles ----
-- add public profiles table
create type profile_type as enum ('individual', 'org', 'amm', 'fund');
create table public.profiles (
id uuid not null,
username text not null unique,
bio text not null,
website text,
accreditation_status boolean not null,
full_name text not null,
avatar_url text,
type profile_type not null default 'individual',
long_description jsonb,
regranter_status boolean not null,
stripe_connect_id text,
primary key (id)
);
-- add RLS policies to profiles
alter table
public.profiles enable row level security;
create policy "Public profiles are viewable by everyone." on profiles for
select
using (true);
create policy "Users can insert their own profile." on profiles for
insert
with check (auth.uid() = id);
create policy "Users can update own profile." on profiles for
update
using (auth.uid() = id);
-- New user trigger
-- inserts a row into public.profiles
drop function if exists public.handle_new_user() cascade;
create function public.handle_new_user() returns trigger language plpgsql security definer
set
search_path = public as $ $ begin
insert into
public.profiles (id, username)
values
(new.id, new.id);
return new;
end;
$ $;
-- trigger the function every time a user is created
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
after
insert
on auth.users for each row execute procedure public.handle_new_user();
-- Expose user emails to admins
create view public.users as
select
id,
email
from
auth.users;
revoke all on public.users
from
anon,
authenticated;
--
--
--
---- Projects ----
create type project_type as enum ('grant', 'cert');
create type project_stage as enum ('active', 'proposal', 'not funded', 'hidden', 'complete', 'draft')
create table if not exists public.projects (
id uuid not null default gen_random_uuid(),
created_at timestamptz not null default now(),
slug text not null unique,
title text not null,
blurb text,
creator uuid not null references auth.users(id) on delete cascade,
min_funding float8 not null,
funding_goal float8 not null default 0,
founder_shares int8 not null,
amm_shares int8 not null,
auction_close date,
description jsonb,
type project_type not null default 'cert',
stage project_stage not null default 'proposal',
approved boolean,
signed_agreement boolean not null default false,
markets jsonb,
primary key (id)
);
-- add RLS policies to projects
CREATE POLICY "Enable insert for authenticated users only" ON "public"."projects" AS PERMISSIVE FOR
INSERT
TO authenticated WITH CHECK (true);
CREATE POLICY "Enable update for users based on creator" ON "public"."projects" FOR
UPDATE
USING (auth.uid() = creator);
CREATE POLICY "Enable read access for all users" ON "public"."projects" AS PERMISSIVE FOR
SELECT
TO public USING (true);
CREATE POLICY "Enable update for austin based on email" ON "public"."projects" AS PERMISSIVE FOR
UPDATE
TO public USING (auth.jwt() ->> 'email' = '[email protected]') WITH CHECK (auth.jwt() ->> 'email' = '[email protected]');
CREATE POLICY "Enable update for rachel based on email" ON "public"."projects" AS PERMISSIVE FOR
UPDATE
TO public USING (
auth.jwt() ->> 'email' = '[email protected]'
) WITH CHECK (
auth.jwt() ->> 'email' = '[email protected]'
);
CREATE POLICY "Enable update for saul based on email" ON "public"."projects" AS PERMISSIVE FOR
UPDATE
TO public USING (
auth.jwt() ->> 'email' = '[email protected]'
) WITH CHECK (
auth.jwt() ->> 'email' = '[email protected]'
);
-- add RLS policies to avatar bucket
CREATE POLICY "Give users access to own folder 1oj01fe_0" ON storage.objects FOR
SELECT
TO public USING (
bucket_id = 'avatars'
AND auth.uid() :: text = (storage.foldername(name)) [1]
);
CREATE POLICY "Allow users to add/change their avatar 1oj01fe_0" ON storage.objects FOR
INSERT
TO public WITH CHECK (
bucket_id = 'avatars'
AND auth.uid() :: text = (storage.foldername(name)) [1]
);
CREATE POLICY "Give users access to own folder 1oj01fe_1" ON storage.objects FOR
INSERT
TO public WITH CHECK (
bucket_id = 'avatars'
AND auth.uid() :: text = (storage.foldername(name)) [1]
);
--
--
--
---- Txns ----
create type txn_type as enum ("profile donation", "project donation", "user to user trade", "user to amm trade", "withdraw", "deposit", "cash to charity transfer", "inject amm liquidity", "mint cert", "mana deposit");
create table if not exists public.txns (
id uuid not null default gen_random_uuid(),
from_id uuid not null references auth.users(id) on delete cascade,
to_id uuid not null references auth.users(id) on delete cascade,
amount numeric not null,
token text not null,
created_at timestamp not null default now(),
type txn_type not null,
primary key (id)
);
alter table
public.txns enable row level security;
CREATE POLICY "Enable read access for all users" ON "public"."txns" AS PERMISSIVE FOR
SELECT
TO public USING (true);
-- Allow anyone to read txns
drop policy if exists "Allow anyone to read txns" on public.txns;
create policy "Allow anyone to read txns" on public.txns for
select
using (true);
CREATE POLICY "Enable insert for authenticated users only" ON "public"."txns" AS PERMISSIVE FOR
INSERT
TO authenticated WITH CHECK (true);
--
--
--
---- Bids ----
-- Create an enum type for 'buy' vs 'sell' vs 'auction'
create type bid_type as enum ('buy', 'sell', 'donate', 'assurance buy', 'assurance sell');
create type bid_status as enum ('deleted', 'pending', 'accepted', 'declined');
create table if not exists public.bids (
id uuid not null default gen_random_uuid(),
created_at timestamptz not null default now(),
project uuid not null references public.projects(id) on delete cascade,
bidder uuid not null references auth.users(id) on delete cascade,
status bid_status not null default 'pending',
content jsonb,
primary key (id)
);
CREATE POLICY "Enable insert for authenticated users only" ON "public"."bids" AS PERMISSIVE FOR
INSERT
TO authenticated WITH CHECK (true);
CREATE POLICY "Enable update for users based on user_id" ON "public"."bids" AS PERMISSIVE FOR UPDATE TO public USING (auth.uid() = bidder);
CREATE POLICY "Enable read access for all users" ON "public"."bids" AS PERMISSIVE FOR
SELECT
TO public USING (true);
-- Comments
create table if not exists public.comments (
id int8 not null default (),
created_at timestamptz not null default now(),
project uuid not null references public.projects(id) on delete cascade,
commenter uuid not null references profiles.users(id) on delete cascade,
content jsonb,
primary key (id)
);
CREATE POLICY "Enable read access for all users" ON "public"."comments"
AS PERMISSIVE FOR SELECT
TO public
USING (true)
CREATE POLICY "Enable insert for authenticated users only" ON "public"."comments"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true)
-- causes
create table if not exists public.causes (
title text not null,
slug text not null unique,
subtitle text,
description jsonb,
header_image_url text not null,
open boolean not null default true,
prize boolean not null default false,
sort number not null,
project_description_outline text,
cert_params jsonb,
primary key (slug)
);
CREATE POLICY "Enable read access for all users" ON "public"."causes"
AS PERMISSIVE FOR SELECT
TO public
USING (true);
CREATE POLICY "Enable update for rachel based on email" ON "public"."causes"
AS PERMISSIVE FOR UPDATE
TO public
USING (auth.jwt() ->> 'email' = '[email protected]')
WITH CHECK (auth.jwt() ->> 'email' = '[email protected]');
CREATE POLICY "Enable update for austin based on email" ON "public"."causes"
AS PERMISSIVE FOR UPDATE
TO public
USING (auth.jwt() ->> 'email' = '[email protected]')
WITH CHECK (auth.jwt() ->> 'email' = '[email protected]');
CREATE POLICY "Enable update for saul based on email" ON "public"."causes" AS PERMISSIVE FOR
UPDATE
TO public USING (
auth.jwt() ->> 'email' = '[email protected]'
) WITH CHECK (
auth.jwt() ->> 'email' = '[email protected]'
);
-- Round header image RLS
CREATE POLICY "Give edit access to rachel 1w84ji1_0" ON storage.objects FOR INSERT TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to rachel 1w84ji1_0" ON storage.objects FOR UPDATE TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to rachel 1w84ji1_0" ON storage.objects FOR DELETE TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to austin 1w84ji1_0" ON storage.objects FOR INSERT TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to austin 1w84ji1_0" ON storage.objects FOR UPDATE TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to austin 1w84ji1_0" ON storage.objects FOR DELETE TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to saul 1w84ji1_0" ON storage.objects FOR INSERT TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to saul 1w84ji1_0" ON storage.objects FOR UPDATE TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
CREATE POLICY "Give edit access to saul 1w84ji1_0" ON storage.objects FOR DELETE TO public WITH CHECK (bucket_id = 'round-header-images' AND auth.jwt() ->> 'email'::text = '[email protected]');
-- Stripe txns
create table if not exists public.stripe_txns (
id uuid not null default gen_random_uuid(),
created_at timestamptz not null,
session_id string not null,
customer_id uuid not null references auth.users(id) on delete cascade,
txn_id uuid not null references public.txns(id),
amount float8 not null,
primary key (id)
);
-- Stripe txns RLS
CREATE POLICY "Enable insert for authenticated users only" ON "public"."stripe_txns"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true)
-- Project transfers
create table public.project_transfers (
id uuid not null,
recipient_email text not null,
recipient_name text not null,
created_at timestamptz not null default now(),
project_id uuid not null references public.projects(id) on delete cascade,
transferred boolean not null default false,
primary key (id)
);
-- project transfers RLS
CREATE POLICY "Enable read access for all users" ON "public"."project_transfers"
AS PERMISSIVE FOR SELECT
TO public
USING (true)
CREATE POLICY "Enable insert for authenticated users only" ON "public"."project_transfers"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true)
-- Project votes
CREATE TABLE public.project_votes (
id int8 NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
project_id uuid NOT NULL REFERENCES public.projects(id) ON DELETE CASCADE,
voter_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
vote int2 NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
-- Project votes RLS
CREATE POLICY "Enable read access for all users" ON "public"."project_votes"
AS PERMISSIVE FOR SELECT
TO public
USING (true);
CREATE POLICY "Enable insert for authenticated users only" ON "public"."project_votes"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true);
CREATE POLICY "Users can update their votes" ON "public"."project_votes"
AS PERMISSIVE FOR UPDATE
TO public
USING (auth.uid() = voter_id)
WITH CHECK (auth.uid() = voter_id);
-- Project causes join table
CREATE TABLE public.project_causes (
id int8 NOT NULL,
project_id uuid NOT NULL REFERENCES public.projects(id) ON DELETE CASCADE,
tag_slug text NOT NULL REFERENCES public.causes(slug) ON DELETE CASCADE,
PRIMARY KEY (id)
);
CREATE POLICY "Enable read access for all users" ON "public"."project_causes"
AS PERMISSIVE FOR SELECT
TO public
USING (true)
CREATE POLICY "Enable delete for users if they created the project" ON "public"."project_causes"
AS PERMISSIVE FOR DELETE
TO public
USING (EXISTS (SELECT 1 FROM public.projects WHERE id = project_id AND creator = auth.uid() ))
CREATE POLICY "Enable insert for authenticated users only" ON "public"."project_causes"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true);
-- Project evals
CREATE TABLE public.project_evals (
evaluator_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
project_id uuid NOT NULL REFERENCES public.projects(id) ON DELETE CASCADE,
score float8 NOT NULL DEFAULT 0,
confidence float8 NOT NULL DEFAULT 1,
created_at timestamptz NOT NULL DEFAULT now(),
-- This would be the ideal primary key, but it messes up Postgrest's foreign key relations
-- PRIMARY KEY (evaluator_id, project_id),
id uuid NOT NULL DEFAULT gen_random_uuid(),
PRIMARY KEY (id)
);
-- Project evals RLS
CREATE POLICY "Enable read access for all users" ON "public"."project_evals"
AS PERMISSIVE FOR SELECT
TO public
USING (true);
CREATE POLICY "Enable insert for authenticated users only" ON "public"."project_evals"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true);
CREATE POLICY "Users can update their votes" ON "public"."project_evals"
AS PERMISSIVE FOR UPDATE
TO public
USING (auth.uid() = evaluator_id)
WITH CHECK (auth.uid() = evaluator_id);
CREATE POLICY "Enable delete for users based on id" ON "public"."project_evals"
AS PERMISSIVE FOR DELETE
TO public
USING (auth.uid() = evaluator_id)
-- Profile trust scores
CREATE TABLE public.profile_trust (
truster_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
trusted_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
weight float8 NOT NULL DEFAULT 1,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (truster_id, trusted_id)
);
-- Profile trust RLS
CREATE POLICY "Enable read access for all users" ON "public"."profile_trust"
AS PERMISSIVE FOR SELECT
TO public
USING (true);
CREATE POLICY "Enable insert for authenticated users only" ON "public"."profile_trust"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true);
CREATE POLICY "Users can update their trust scores" ON "public"."profile_trust"
AS PERMISSIVE FOR UPDATE
TO public
USING (auth.uid() = truster_id)
WITH CHECK (auth.uid() = truster_id);
CREATE POLICY "Enable delete for users based on id" ON "public"."profile_trust"
AS PERMISSIVE FOR DELETE
TO public
USING (auth.uid() = truster_id)
-- Project follows
CREATE TABLE public.project_follows (
follower_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
project_id uuid NOT NULL REFERENCES public.projects(id) ON DELETE CASCADE,
PRIMARY KEY (follower_id, project_id)
);
-- Project follows RLS
CREATE POLICY "Enable read access for all users" ON "public"."project_follows"
AS PERMISSIVE FOR SELECT
TO public
USING (true)
CREATE POLICY "Enable delete for users based on user_id" ON "public"."project_follows"
AS PERMISSIVE FOR DELETE
TO public
USING (auth.uid() = follower_id)
CREATE POLICY "Enable insert for users based on user_id" ON "public"."project_follows"
AS PERMISSIVE FOR INSERT
TO public
WITH CHECK (auth.uid() = follower_id)
-- Grant agreements
CREATE TABLE public.grant_agreements (
project_id uuid NOT NULL REFERENCES public.projects(id),
lobbying_clause_excluded boolean NOT NULL DEFAULT false,
signed_off_site boolean NOT NULL DEFAULT false,
signed_at timestamptz,
signatory_name text,
recipient_name text,
project_title text,
project_description jsonb,
approved_at timestamptz,
approved_by uuid REFERENCES public.profiles(id),
completed_at timestamptz,
version int2 DEFAULT 1,
PRIMARY KEY (project_id)
);
-- Grant agreements RLS
CREATE POLICY "Enable read access for all users" ON "public"."grant_agreements"
AS PERMISSIVE FOR SELECT
TO public
USING (true)
CREATE POLICY "Enable insert for authenticated users only" ON "public"."grant_agreements"
AS PERMISSIVE FOR INSERT
TO authenticated
WITH CHECK (true)
create policy "Enable update for authenticated users only"
on "public"."grant_agreements"
as PERMISSIVE
for UPDATE
to authenticated
using (
true
);
-- Comment rxns
CREATE TABLE public.comment_rxns (
comment_id uuid NOT NULL REFERENCES public.comments(id),
reactor_id uuid NOT NULL REFERENCES public.profiles(id),
reaction text NOT NULL,
txn_id uuid REFERENCES public.txns(id),
PRIMARY KEY (comment_id, reactor_id, reaction)
);
-- Comment rxns RLS
create policy "Enable read access for all users" on "public"."comment_rxns"
as PERMISSIVE for SELECT to public
using (true);
create policy "Enable insert for authenticated users only" on "public"."comment_rxns"
as PERMISSIVE for INSERT
to authenticated
with check (
true
);
create policy "Enable delete for users based on user_id" on "public"."comment_rxns"
as PERMISSIVE for DELETE
to public
using (
auth.uid() = reactor_id
);
-- Profile roles
CREATE TABLE public.profile_roles (
id uuid PRIMARY KEY REFERENCES public.profiles(id),
donor boolean NOT NULL DEFAULT false,
organizer text,
scholar text,
volunteer text,
worker text,
senior text,
insider boolean NOT NULL DEFAULT false
);
-- Add RLS policies
ALTER TABLE public.profile_roles ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Anyone can read any profile role"
ON public.profile_roles FOR SELECT
USING (true);
CREATE POLICY "Users can insert their own profile roles"
ON public.profile_roles FOR INSERT
WITH CHECK (auth.uid() = id);
CREATE POLICY "Users can update their own profile roles"
ON public.profile_roles FOR UPDATE
USING (auth.uid() = id);