mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-11-18 22:44:37 +00:00
No longer Axum-based because filling out the request routes would have distracted from the purpose of the example.
31 lines
928 B
PL/PgSQL
31 lines
928 B
PL/PgSQL
-- We try to ensure every table has `created_at` and `updated_at` columns, which can help immensely with debugging
|
|
-- and auditing.
|
|
--
|
|
-- While `created_at` can just be `default now()`, setting `updated_at` on update requires a trigger which
|
|
-- is a lot of boilerplate. These two functions save us from writing that every time as instead we can just do
|
|
--
|
|
-- select payments.trigger_updated_at('<table name>');
|
|
--
|
|
-- after a `CREATE TABLE`.
|
|
create or replace function payments.set_updated_at()
|
|
returns trigger as
|
|
$$
|
|
begin
|
|
NEW.updated_at = now();
|
|
return NEW;
|
|
end;
|
|
$$ language plpgsql;
|
|
|
|
create or replace function payments.trigger_updated_at(tablename regclass)
|
|
returns void as
|
|
$$
|
|
begin
|
|
execute format('CREATE TRIGGER set_updated_at
|
|
BEFORE UPDATE
|
|
ON %s
|
|
FOR EACH ROW
|
|
WHEN (OLD is distinct from NEW)
|
|
EXECUTE FUNCTION payments.set_updated_at();', tablename);
|
|
end;
|
|
$$ language plpgsql;
|