beteran-server-service/migrations/202206171200_initialize/up.sql

36 lines
818 B
PL/PgSQL

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = (extract(epoch from now()) * 1000);
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE OR REPLACE FUNCTION update_state_changed_at_column()
RETURNS TRIGGER AS $$
BEGIN
IF OLD.state != NEW.state THEN
NEW.state_changed_at = (extract(epoch from now()) * 1000);
ELSE
NEW.state_changed_at = NEW.state_changed_at;
END IF;
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE OR REPLACE FUNCTION update_active_changed_at_column()
RETURNS TRIGGER AS $$
BEGIN
IF OLD.active != NEW.active THEN
NEW.active_changed_at = (extract(epoch from now()) * 1000);
ELSE
NEW.active_changed_at = NEW.active_changed_at;
END IF;
RETURN NEW;
END;
$$ language 'plpgsql';