feat: user, audit, hashed_text, email
This commit is contained in:
parent
cd5b593d73
commit
737137439a
1
schema/000_email.sql
Normal file
1
schema/000_email.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
create type public.email as (email text);
|
1
schema/000_hashed_text.sql
Normal file
1
schema/000_hashed_text.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
create type hashed_text as (hashed text);
|
@ -1,5 +0,0 @@
|
|||||||
create type public.thread_kind as enum
|
|
||||||
( 'post'
|
|
||||||
, 'short'
|
|
||||||
, 'message'
|
|
||||||
);
|
|
10
schema/010_audit.sql
Normal file
10
schema/010_audit.sql
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
create type public.audit_kind as enum
|
||||||
|
( 'modify'
|
||||||
|
, 'delete'
|
||||||
|
, 'create'
|
||||||
|
);
|
||||||
|
|
||||||
|
create table public.audit
|
||||||
|
( id int not null primary key generated always as identity
|
||||||
|
, kind public.audit_kind not null
|
||||||
|
);
|
@ -1,5 +1,11 @@
|
|||||||
create table public.thread
|
create type public.thread_kind as enum
|
||||||
( id int not null generated always as identity
|
( 'post'
|
||||||
, uid uuid not null default gen_random_uuid()
|
, 'short'
|
||||||
, kind thread_kind not null
|
, 'message'
|
||||||
|
);
|
||||||
|
|
||||||
|
create table public.thread
|
||||||
|
( id int not null primary key generated always as identity
|
||||||
|
, uid uuid not null default gen_random_uuid()
|
||||||
|
, kind public.thread_kind not null
|
||||||
);
|
);
|
||||||
|
103
schema/010_user.sql
Normal file
103
schema/010_user.sql
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
create type public.usr_username as (username text);
|
||||||
|
|
||||||
|
create table public.usr_audit
|
||||||
|
( usr int not null
|
||||||
|
, kind public.audit_kind not null
|
||||||
|
, actor int not null
|
||||||
|
, prev_username public.usr_username null
|
||||||
|
, prev_password public.hashed_text null
|
||||||
|
, prev_email public.email null
|
||||||
|
);
|
||||||
|
|
||||||
|
create table public.usr
|
||||||
|
( id int not null primary key generated always as identity
|
||||||
|
, uid uuid not null default gen_random_uuid()
|
||||||
|
, deleted boolean not null default false
|
||||||
|
, username public.usr_username not null unique
|
||||||
|
, password public.hashed_text not null
|
||||||
|
, email public.email not null unique
|
||||||
|
);
|
||||||
|
|
||||||
|
alter table public.usr_audit
|
||||||
|
add constraint fk_usr_audit_usr
|
||||||
|
foreign key (usr)
|
||||||
|
references public.usr (id)
|
||||||
|
, add constraint fk_usr_audit_actor
|
||||||
|
foreign key (actor)
|
||||||
|
references public.usr (id)
|
||||||
|
;
|
||||||
|
|
||||||
|
create function public.do_usr_audit()
|
||||||
|
returns trigger
|
||||||
|
language plpgsql as $$
|
||||||
|
declare
|
||||||
|
audit_kind public.audit_kind;
|
||||||
|
usr_id int;
|
||||||
|
prev_username public.usr_username := null;
|
||||||
|
prev_password public.hashed_text := null;
|
||||||
|
prev_email public.email := null;
|
||||||
|
begin
|
||||||
|
if (TG_OP = 'UPDATE') then
|
||||||
|
usr_id := OLD.id;
|
||||||
|
audit_kind := 'modify';
|
||||||
|
prev_username := OLD.username;
|
||||||
|
prev_password := OLD.password;
|
||||||
|
prev_email := OLD.email;
|
||||||
|
elsif (TG_OP = 'INSERT') then
|
||||||
|
usr_id := NEW.id;
|
||||||
|
audit_kind := 'create';
|
||||||
|
end if;
|
||||||
|
|
||||||
|
insert into public.usr_audit
|
||||||
|
( kind
|
||||||
|
, usr
|
||||||
|
, actor
|
||||||
|
, prev_username
|
||||||
|
, prev_email
|
||||||
|
, prev_password
|
||||||
|
)
|
||||||
|
values
|
||||||
|
( audit_kind
|
||||||
|
, usr_id
|
||||||
|
, usr_id -- TODO
|
||||||
|
, prev_username
|
||||||
|
, prev_email
|
||||||
|
, prev_password
|
||||||
|
);
|
||||||
|
|
||||||
|
return NEW;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
create function public.do_usr_soft_delete()
|
||||||
|
returns trigger
|
||||||
|
language plpgsql as $$
|
||||||
|
declare
|
||||||
|
audit_id int;
|
||||||
|
begin
|
||||||
|
insert into public.usr_audit
|
||||||
|
( kind
|
||||||
|
, usr
|
||||||
|
, actor
|
||||||
|
)
|
||||||
|
values
|
||||||
|
( 'delete' :: public.audit_kind
|
||||||
|
, OLD.id
|
||||||
|
, OLD.id -- TODO
|
||||||
|
);
|
||||||
|
|
||||||
|
update public.usr
|
||||||
|
set deleted = true
|
||||||
|
where id = OLD.id;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
create trigger trigger_usr_audit
|
||||||
|
after insert or update on public.usr
|
||||||
|
for each row execute function public.do_usr_audit();
|
||||||
|
|
||||||
|
create trigger trigger_usr_soft_delete
|
||||||
|
before delete on public.usr
|
||||||
|
for each row execute function public.do_usr_soft_delete();
|
3
scripts/migrate.sh
Normal file
3
scripts/migrate.sh
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#! /usr/bin/bash
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user