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';

CREATE OR REPLACE FUNCTION update_last_signined_at_column()
RETURNS TRIGGER AS $$
BEGIN
  IF OLD.last_signined_ip != NEW.last_signined_ip THEN
    NEW.last_signined_at = (extract(epoch from now()) * 1000);
  ELSE
    NEW.last_signined_at = NEW.last_signined_at;
  END IF;
  RETURN NEW;
END;
$$ language 'plpgsql';

