25 lines
679 B
MySQL
25 lines
679 B
MySQL
|
-- SPDX-FileCopyrightText: 2023 Louis Hollingworth <louis@hollingworth.nl>
|
||
|
--
|
||
|
-- SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
|
||
|
-- Add up migration script here
|
||
|
|
||
|
CREATE OR REPLACE FUNCTION manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||
|
BEGIN
|
||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||
|
FOR EACH ROW EXECUTE PROCEDURE set_updated_at()', _tbl);
|
||
|
END;
|
||
|
$$ LANGUAGE plpgsql;
|
||
|
|
||
|
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS trigger AS $$
|
||
|
BEGIN
|
||
|
IF (
|
||
|
NEW IS DISTINCT FROM OLD AND
|
||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||
|
) THEN
|
||
|
NEW.updated_at := NOW();
|
||
|
END IF;
|
||
|
RETURN NEW;
|
||
|
END;
|
||
|
$$ LANGUAGE plpgsql
|