feat: initial db persistence
This commit is contained in:
parent
e8abfb18eb
commit
2c4995f9d7
9 changed files with 342 additions and 46 deletions
|
|
@ -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;
|
||||
13
src/main/resources/db/migration/V002__add_message_table.sql
Normal file
13
src/main/resources/db/migration/V002__add_message_table.sql
Normal 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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue