Commit 2a687a29 authored by Timo Stolz's avatar Timo Stolz
Browse files

auto-increase updated_at columns

parents
Showing with 78 additions and 0 deletions
+78 -0
\if :{?utils_schema}
\else
\set utils_schema public
\endif
\ No newline at end of file
-- Deploy 0814-utils:increase-updated-at to pg
BEGIN;
\ir ../defaults.sql
create schema if not exists :"utils_schema";
create or replace function :"utils_schema".auto_increase_updated_at()
returns trigger
as $$
begin
if new is distinct from old and new.updated_at <= old.updated_at then
new.updated_at := current_timestamp;
end if;
return new;
end;
$$ language plpgsql volatile;
COMMIT;
-- Revert 0814-utils:increase-updated-at from pg
BEGIN;
\ir ../defaults.sql
drop function :"utils_schema".auto_increase_updated_at();
COMMIT;
[core]
engine = pg
plan_file = ./sqitch.plan
# top_dir = .
%syntax-version=1.0.0
%project=0814-utils
%uri=https://gitlab.nullachtvierzehn.de/0814/sqitch-utils
increase-updated-at 2022-03-08T14:05:05Z Timo Stolz <timo.stolz@nullachtvierzehn.de> # automatically increase updated at
begin;
\ir ../defaults.sql
create extension if not exists pgtap;
select plan(2);
create table test_10ad83aa_9eeb_11ec_8549_7bbf46bf41a7 (
id integer primary key,
updatable varchar,
updated_at timestamptz not null default current_timestamp
);
create trigger t900_auto_increase_updated_at
before update
on test_10ad83aa_9eeb_11ec_8549_7bbf46bf41a7
for each row
execute function :"utils_schema".auto_increase_updated_at();
insert into test_10ad83aa_9eeb_11ec_8549_7bbf46bf41a7 values (1, 'initial', '2000-01-01 00:00:00');
select is(updated_at, '2000-01-01 00:00:00') from test_10ad83aa_9eeb_11ec_8549_7bbf46bf41a7;
update test_10ad83aa_9eeb_11ec_8549_7bbf46bf41a7 set updatable = 'updated';
select is(updated_at, current_timestamp) from test_10ad83aa_9eeb_11ec_8549_7bbf46bf41a7;
rollback;
\ No newline at end of file
-- Verify 0814-utils:increase-updated-at on pg
BEGIN;
-- XXX Add verifications here.
ROLLBACK;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment