From 203db370e0d15a7f4a4f2c30df031e4c61df2d5a Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Fri, 16 Aug 2024 16:26:04 +0500 Subject: [PATCH] add automatic created_at and updated_at fields to schema --- migrations/20240727123308_initial.sql | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/migrations/20240727123308_initial.sql b/migrations/20240727123308_initial.sql index 1c81572..13a3246 100644 --- a/migrations/20240727123308_initial.sql +++ b/migrations/20240727123308_initial.sql @@ -13,18 +13,6 @@ CREATE TABLE IF NOT EXISTS languages ); -CREATE FUNCTION lng_upd_trg_fn() RETURNS TRIGGER - LANGUAGE plpgsql AS -$$ -BEGIN - NEW.updated_at = NOW(); - RETURN NEW; -END; -$$; - -CREATE TRIGGER lng_upd_trg BEFORE UPDATE ON languages FOR EACH ROW EXECUTE FUNCTION lng_upd_trg_fn(); - - CREATE TABLE IF NOT EXISTS problems @@ -34,6 +22,8 @@ CREATE TABLE IF NOT EXISTS problems description TEXT NOT NULL, time_limit INT NOT NULL, memory_limit INT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), PRIMARY KEY(id) ); @@ -47,6 +37,8 @@ CREATE TABLE IF NOT EXISTS contests ( id serial NOT NULL, name VARCHAR(300) NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), PRIMARY KEY(id) ); @@ -226,11 +218,26 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER new_subtask_trg AFTER INSERT ON subtasks FOR EACH ROW EXECUTE FUNCTION on_new_subtask(); + + + +CREATE FUNCTION updated_at_update() RETURNS TRIGGER + LANGUAGE plpgsql AS +$$ +BEGIN + NEW.updated_at = NOW(); + RETURN NEW; +END; +$$; + +CREATE TRIGGER languages_upd_trg BEFORE UPDATE ON languages FOR EACH ROW EXECUTE FUNCTION updated_at_update(); +CREATE TRIGGER problems_upd_trg BEFORE UPDATE ON problems FOR EACH ROW EXECUTE FUNCTION updated_at_update(); +CREATE TRIGGER contests_upd_trg BEFORE UPDATE ON contests FOR EACH ROW EXECUTE FUNCTION updated_at_update(); -- +goose StatementEnd -- +goose Down -- +goose StatementBegin -DROP FUNCTION IF EXISTS lng_upd_trg_fn CASCADE; +DROP FUNCTION IF EXISTS updated_at_update CASCADE; DROP FUNCTION IF EXISTS on_new_participant CASCADE; DROP FUNCTION IF EXISTS on_new_task CASCADE; DROP FUNCTION IF EXISTS on_new_subtask CASCADE;