chore: babe wake up new migrations just dropped

This commit is contained in:
🤖 2023-07-21 16:36:39 +00:00
parent cecef81fff
commit 9eb73bd1b2
7 changed files with 222 additions and 0 deletions

1
01ee432_to_658ad01.sql Normal file
View File

@ -0,0 +1 @@

35
3712154_to_38893b5.sql Normal file
View File

@ -0,0 +1,35 @@
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION public.do_grp_add_admins()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
declare
admins int[];
begin
admins := ( select array_agg(usr.id)
from public.grp_members_admins() as usr
);
perform public.grp_add_members( to_grp => NEW.id
, add_usrs => admins
);
return null;
end;
$function$
;
CREATE OR REPLACE FUNCTION public.grp_members(of_grp integer)
RETURNS SETOF usr
LANGUAGE plpgsql
STABLE
AS $function$
begin
return query select u.*
from public.grp_usr gu
inner join public.usr u on gu.usr = u.id
where gu.grp = of_grp;
end;
$function$
;

1
38893b5_to_fe4288d.sql Normal file
View File

@ -0,0 +1 @@

1
658ad01_to_3712154.sql Normal file
View File

@ -0,0 +1 @@

1
c7091e9_to_a28cbba.sql Normal file
View File

@ -0,0 +1 @@

168
d26aa99_to_c7091e9.sql Normal file
View File

@ -0,0 +1,168 @@
drop function if exists "public"."usr_session_touch"(session usr_session_key);
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION public.usr_groups(of_usr human_uuid.huid)
RETURNS SETOF grp
LANGUAGE plpgsql
STABLE
AS $function$
begin
return query select g.*
from public.usr u
inner join public.grp_usr gu on gu.usr = u.id
inner join public.grp g on g.id = gu.grp
where u.uid = of_usr;
end;
$function$
;
CREATE OR REPLACE FUNCTION public.usr_session_touch(touch_key usr_session_key)
RETURNS usr
LANGUAGE plpgsql
AS $function$
declare
session public.usr_session;
session_usr public.usr;
new_exp timestamp;
begin
select s.*
from public.usr_session s
where s.key = touch_key
into session;
if session is null then
raise exception 'usr_session_invalid';
end if;
if session.expires_at <= now() then
raise exception 'usr_session_expired';
end if;
if session.remembered then
new_exp := now() + interval '1 week';
else
new_exp := now() + interval '1 hour';
end if;
update public.usr_session as s
set expires_at = new_exp
where s.id = session.id;
select u.*
from public.usr u
where u.id = session.usr
into session_usr;
return session_usr;
end;
$function$
;
CREATE OR REPLACE FUNCTION public.do_insert_grp_perm()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
begin
insert into public.perm
(path, owner_user, owner_group, owner_user_mode, owner_group_mode, everyone_mode)
values
('/groups/' || human_uuid.huid_to_string(NEW.uid) || '/members', (public.get_acting_usr()).id, NEW.id, 'w', 'w', '-')
, ('/groups/' || human_uuid.huid_to_string(NEW.uid) || '/name', (public.get_acting_usr()).id, NEW.id, 'w', 'w', 'r')
;
return new;
end;
$function$
;
CREATE OR REPLACE FUNCTION public.do_usr_create_default_grp()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
declare
name public.grp_tag := grp_tag_of_string('usr_' || new.uid);
new_grp public.grp;
begin
insert into public.grp
(tag)
values
(name)
returning * into new_grp;
perform public.grp_add_member(to_grp => new_grp.id, add_usr => new.id);
update public.perm
set owner_user = NEW.id
, owner_group = (public.grp_admins()).id
, owner_user_mode = 'r' :: public.perm_mode
where path = '/groups/' || (new_grp.uid :: text) || '/members'
or path = '/groups/' || (new_grp.uid :: text) || '/name';
return null;
end;
$function$
;
CREATE OR REPLACE FUNCTION public.do_usr_session_immutable_columns()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
begin
if OLD.id <> NEW.id then
raise exception 'immutable_field'
using detail = 'public.usr_session.id is immutable',
errcode = 'restrict_violation';
elsif OLD.key <> NEW.key then
raise exception 'immutable_field'
using detail = 'public.usr_session.key is immutable',
errcode = 'restrict_violation';
elsif OLD.usr <> NEW.usr then
raise exception 'immutable_field'
using detail = 'public.usr_session.usr is immutable',
errcode = 'restrict_violation';
elsif OLD.location <> NEW.location then
raise exception 'immutable_field'
using detail = 'public.usr_session.location is immutable',
errcode = 'restrict_violation';
elsif OLD.device <> NEW.device then
raise exception 'immutable_field'
using detail = 'public.usr_session.device is immutable',
errcode = 'restrict_violation';
elsif OLD.ip <> NEW.ip then
raise exception 'immutable_field'
using detail = 'public.usr_session.ip is immutable',
errcode = 'restrict_violation';
end if;
return NEW;
end;
$function$
;
CREATE OR REPLACE FUNCTION public.usr_session_login(tag_or_email usr_tag_or_email, password text, remember boolean DEFAULT false, location text DEFAULT NULL::text, device usr_session_device DEFAULT NULL::usr_session_device, ip inet DEFAULT NULL::inet)
RETURNS usr_session_key
LANGUAGE plpgsql
AS $function$
declare
usr public.usr;
key public.usr_session_key := usr_session_key_gen();
expires_at timestamp;
begin
usr := public.usr_session_login_validate(tag_or_email, password);
if remember then
expires_at := now() + interval '1 week';
else
expires_at := now() + interval '1 hour';
end if;
insert into public.usr_session
(key, expires_at, usr, remembered, location, device, ip)
values
(key, expires_at, usr.id, coalesce(remember, false), location, device, ip);
return key;
end;
$function$
;

15
fe4288d_to_d26aa99.sql Normal file
View File

@ -0,0 +1,15 @@
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION public.grp_add_members(to_grp integer, add_usrs integer[])
RETURNS void
LANGUAGE plpgsql
AS $function$
begin
insert into public.grp_usr (grp, usr)
select to_grp, usr_id
from unnest(add_usrs) usr_id
left join public.grp_usr gu on gu.usr = usr_id and gu.grp = to_grp
where gu is null;
end;
$function$
;