Перейти к содержанию

tasks_autotimestamps

Сигнатура: tasks_autotimestamps() RETURNS "trigger"
Язык: sql
Security: INVOKER

Тело функции

begin
  -- INSERT: created_at по умолчанию
  if tg_op = 'INSERT' then
    if new.created_at is null then
      new.created_at := now();
    end if;
  end if;

  -- updated_at всегда обновляем
  new.updated_at := now();

  -- Если статус "терминальный" (success / fail / refunded)
  -- и finished_at ещё не выставлен — ставим сейчас.
  if lower(coalesce(new.status, '')) = any (array['success','fail','refunded']) then
    if new.finished_at is null then
      new.finished_at := now();
    end if;
  end if;

  return new;
end;