feat: initial db persistence

This commit is contained in:
Finn Linck Ryan 2025-12-13 17:09:08 +01:00
parent e8abfb18eb
commit 2c4995f9d7
9 changed files with 342 additions and 46 deletions

View file

@ -0,0 +1,18 @@
create or replace function handle_timestamps()
returns trigger as
$$
begin
if lower(tg_op) = 'insert' then
new.created_at = coalesce(new.created_at, current_timestamp);
new.updated_at = coalesce(new.updated_at, current_timestamp);
elsif lower(tg_op) = 'update' then
if new.created_at is distinct from old.created_at then
raise exception 'Direct modification of created_at is not allowed.';
end if;
new.updated_at = current_timestamp;
end if;
return new;
end;
$$ language plpgsql;

View file

@ -0,0 +1,13 @@
create table message (
id uuid not null primary key,
author varchar(31) not null,
content varchar(255) not null,
created_at timestamptz not null default current_timestamp,
updated_at timestamptz
);
create trigger handle_message_timestamps
before insert or update on message
for each row
execute function handle_timestamps();