added timestamps to messages

This commit is contained in:
2025-12-15 17:33:13 +01:00
parent 68e093f388
commit 5c3cfdafe4
6 changed files with 62 additions and 14 deletions

View File

@@ -23,6 +23,22 @@ CREATE TABLE IF NOT EXISTS message_ (
id BIGSERIAL PRIMARY KEY,
sender INT REFERENCES user_(id) NOT NULL,
room INT REFERENCES room_(id) NOT NULL,
type VARCHAR(32) NOT NULL,
content TEXT NOT NULL
message_type VARCHAR(32) NOT NULL,
content TEXT NOT NULL,
sent_at TIMESTAMP
);
-- Message timestamp creation
CREATE OR REPLACE FUNCTION create_message_timestamp()
RETURNS trigger
AS $$
BEGIN
NEW.sent_at := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE TRIGGER insert_message
BEFORE INSERT ON message_
FOR EACH ROW
EXECUTE FUNCTION create_message_timestamp();

View File

@@ -15,7 +15,7 @@ INSERT INTO membership_ (user_id, room) VALUES
(3, 1), -- Carol in General Discussion
(1, 3); -- Alice in Random Memes
INSERT INTO message_ (sender, room, type, content) VALUES
INSERT INTO message_ (sender, room, message_type, content) VALUES
(1, 1, 'text', 'Hey everyone, hows it going?'),
(2, 1, 'text', 'All good! Just trying to get through some work.'),
(3, 1, 'text', 'Hello! How are you guys?'),