diff --git a/migrations/202206171200_initialize/down.sql b/migrations/202206171200_initialize/down.sql index 4aba7c6..8071988 100644 --- a/migrations/202206171200_initialize/down.sql +++ b/migrations/202206171200_initialize/down.sql @@ -2,3 +2,4 @@ DROP EXTENSION "uuid-ossp"; DROP FUNCTION update_updated_at_column; DROP FUNCTION update_state_changed_at_column; DROP FUNCTION update_active_changed_at_column; +DROP FUNCTION update_last_signined_at_column; diff --git a/migrations/202206171200_initialize/up.sql b/migrations/202206171200_initialize/up.sql index 6efc7c4..6850b4f 100644 --- a/migrations/202206171200_initialize/up.sql +++ b/migrations/202206171200_initialize/up.sql @@ -34,3 +34,15 @@ BEGIN 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'; + diff --git a/migrations/202206201240_member/down.sql b/migrations/202206201240_member/down.sql index e2694fb..2e1b056 100644 --- a/migrations/202206201240_member/down.sql +++ b/migrations/202206201240_member/down.sql @@ -1,4 +1,5 @@ DROP INDEX idx_members_username; +DROP TRIGGER tg_members_last_signined_at; DROP TRIGGER tg_members_state_changed_at; DROP TRIGGER tg_members_updated_at; DROP TABLE members; diff --git a/migrations/202206201240_member/up.sql b/migrations/202206201240_member/up.sql index c1dddf0..b9ec044 100644 --- a/migrations/202206201240_member/up.sql +++ b/migrations/202206201240_member/up.sql @@ -51,3 +51,10 @@ CREATE TRIGGER tg_members_state_changed_at ON members FOR EACH ROW EXECUTE PROCEDURE update_state_changed_at_column(); + +-- trigger (state_changed_at) +CREATE TRIGGER tg_members_last_signined_at + BEFORE UPDATE + ON members + FOR EACH ROW + EXECUTE PROCEDURE update_last_signined_at_column();