|
| 1 | +-- drop everything |
| 2 | +alter table if exists public.gist drop constraint if exists gist_userid_fkey; |
| 3 | +alter table if exists public.session drop constraint if exists session_userid_fkey; |
| 4 | +drop index if exists gist_owner_idx; |
| 5 | + |
| 6 | +drop table if exists public.user; |
| 7 | +drop table if exists public.session; |
| 8 | +drop table if exists public.gist; |
| 9 | +drop table if exists public.todo; |
| 10 | + |
| 11 | +drop function if exists public.get_user; |
| 12 | +drop function if exists public.gist_create; |
| 13 | +drop function if exists public.gist_update; |
| 14 | +drop function if exists public.gist_destroy; |
| 15 | +drop function if exists public.login; |
| 16 | +drop function if exists public.logout; |
| 17 | + |
| 18 | +create table public.user ( |
| 19 | + id bigserial primary key, |
| 20 | + created_at timestamptz default now(), |
| 21 | + updated_at timestamptz, |
| 22 | + github_id int8 unique, |
| 23 | + github_name text, |
| 24 | + github_login text, |
| 25 | + github_avatar_url text |
| 26 | +); |
| 27 | + |
| 28 | +create table public.session ( |
| 29 | + id uuid default extensions.uuid_generate_v4() not null primary key, |
| 30 | + created_at timestamptz default now(), |
| 31 | + userid int8, |
| 32 | + expires timestamptz default now() + '1 year' |
| 33 | +); |
| 34 | + |
| 35 | +create table public.gist ( |
| 36 | + id uuid default extensions.uuid_generate_v4() not null primary key, |
| 37 | + created_at timestamptz default now(), |
| 38 | + name text, |
| 39 | + files json, |
| 40 | + userid int8, |
| 41 | + updated_at timestamptz, |
| 42 | + deleted_at timestamptz |
| 43 | +); |
| 44 | + |
| 45 | +create table public.todo ( |
| 46 | + uid uuid default extensions.uuid_generate_v4() not null primary key, |
| 47 | + created_at timestamptz default now(), |
| 48 | + guestid uuid not null, |
| 49 | + text text, |
| 50 | + done boolean |
| 51 | +); |
| 52 | + |
| 53 | +-- foreign key relations |
| 54 | +alter table public.gist add constraint gist_userid_fkey foreign key (userid) references public.user (id); |
| 55 | +alter table public.session add constraint session_userid_fkey foreign key (userid) references public.user (id); |
| 56 | + |
| 57 | +-- indexes |
| 58 | +create index gist_owner_idx on public.gist using btree (userid); |
| 59 | +create index todo_owner_idx on public.todo using btree (guestid); |
| 60 | + |
| 61 | +-- functions |
| 62 | +create or replace function public.get_user (sessionid uuid) |
| 63 | +returns record |
| 64 | +language plpgsql volatile |
| 65 | +as $$ |
| 66 | + declare |
| 67 | + ret record; |
| 68 | + _ record; |
| 69 | + begin |
| 70 | + select userid from session where session.id = sessionid into _; |
| 71 | + |
| 72 | + select id, github_name, github_login, github_avatar_url from public.user into ret where public.user.id = _.userid; |
| 73 | + |
| 74 | + return ret; |
| 75 | + end; |
| 76 | +$$; |
| 77 | + |
| 78 | +create or replace function public.gist_list ( |
| 79 | + list_search text, |
| 80 | + list_userid int8, |
| 81 | + list_count int4, |
| 82 | + list_start int4 |
| 83 | +) |
| 84 | +returns table ( |
| 85 | + id uuid, |
| 86 | + name text, |
| 87 | + created_at timestamptz, |
| 88 | + updated_at timestamptz |
| 89 | +) |
| 90 | +language plpgsql volatile |
| 91 | +as $$ |
| 92 | + begin |
| 93 | + return query |
| 94 | + select gist.id, gist.name, gist.created_at, gist.updated_at |
| 95 | + from gist |
| 96 | + where gist.userid = list_userid and gist.deleted_at is null and gist.name ilike ('%' || list_search || '%') |
| 97 | + order by coalesce(gist.updated_at, gist.created_at) desc |
| 98 | + limit list_count + 1 |
| 99 | + offset list_start; |
| 100 | + end; |
| 101 | +$$; |
| 102 | + |
| 103 | +create or replace function public.gist_create (name text, files json, userid int8) |
| 104 | +returns record |
| 105 | +language plpgsql volatile |
| 106 | +as $$ |
| 107 | + declare |
| 108 | + ret record; |
| 109 | + begin |
| 110 | + insert into gist (name, files, userid) |
| 111 | + values (name, files, userid) returning gist.id, gist.name, gist.files, gist.userid into ret; |
| 112 | + |
| 113 | + return ret; |
| 114 | + end; |
| 115 | +$$; |
| 116 | + |
| 117 | +create or replace function public.gist_destroy ( |
| 118 | + gist_ids uuid[], |
| 119 | + gist_userid int8 |
| 120 | +) |
| 121 | +returns void |
| 122 | +language plpgsql volatile |
| 123 | +as $$ |
| 124 | + begin |
| 125 | + update gist set deleted_at = now() where id = any(gist_ids) and userid = gist_userid; |
| 126 | + end; |
| 127 | +$$; |
| 128 | + |
| 129 | +create or replace function public.gist_update ( |
| 130 | + gist_id uuid, |
| 131 | + gist_name text, |
| 132 | + gist_files json, |
| 133 | + gist_userid int8 |
| 134 | +) |
| 135 | +returns record |
| 136 | +language plpgsql volatile |
| 137 | +as $$ |
| 138 | + declare |
| 139 | + ret record; |
| 140 | + begin |
| 141 | + update gist |
| 142 | + set name = gist_name, files = gist_files, updated_at = now() |
| 143 | + where id = gist_id and userid = gist_userid and deleted_at is null |
| 144 | + returning id, name, files, userid into ret; |
| 145 | + |
| 146 | + return ret; |
| 147 | + end; |
| 148 | +$$; |
| 149 | + |
| 150 | +create or replace function public.login ( |
| 151 | + user_github_id int8, |
| 152 | + user_github_name text, |
| 153 | + user_github_login text, |
| 154 | + user_github_avatar_url text |
| 155 | +) |
| 156 | +returns record |
| 157 | +language plpgsql volatile |
| 158 | +as $$ |
| 159 | + declare |
| 160 | + _ record; |
| 161 | + ret record; |
| 162 | + begin |
| 163 | + insert into "user" (github_id, github_name, github_login, github_avatar_url, updated_at) |
| 164 | + values (user_github_id, user_github_name, user_github_login, user_github_avatar_url, now()) |
| 165 | + on conflict (github_id) do update set github_name = user_github_name, github_login = user_github_login, github_avatar_url = user_github_avatar_url, updated_at = now() where public."user".github_id = user_github_id |
| 166 | + returning id into _; |
| 167 | + |
| 168 | + insert into "session" (userid) values (_.id) returning session.id as sessionid, session.userid, session.expires into ret; |
| 169 | + |
| 170 | + return ret; |
| 171 | + end; |
| 172 | +$$; |
| 173 | + |
| 174 | +create or replace function public.logout ( |
| 175 | + sessionid uuid |
| 176 | +) |
| 177 | +returns void |
| 178 | +language plpgsql volatile |
| 179 | +as $$ |
| 180 | + begin |
| 181 | + delete from session where id = sessionid; |
| 182 | + end; |
| 183 | +$$; |
0 commit comments