nordabiz/database/migrations/012_add_read_tracking_tables.sql
Maciej Pienczyn e87ba8ee09 feat: Add read tracking for Forum topics/replies and B2B classifieds
- Add ForumTopicRead, ForumReplyRead, ClassifiedRead models
- Add SQL migration for new tables
- Record reads when user views forum topic (topic + all visible replies)
- Record reads when user views B2B classified
- Display "Seen by" avatars in forum topic and B2B detail pages

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 20:50:27 +01:00

48 lines
2.1 KiB
SQL

-- Migration: Add read tracking tables for Forum and B2B
-- Date: 2026-01-31
-- Description: Adds tables to track who has read forum topics, replies, and B2B classifieds
-- Forum Topic Reads
CREATE TABLE IF NOT EXISTS forum_topic_reads (
id SERIAL PRIMARY KEY,
topic_id INTEGER NOT NULL REFERENCES forum_topics(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
read_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT uq_forum_topic_user_read UNIQUE (topic_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_forum_topic_reads_topic ON forum_topic_reads(topic_id);
CREATE INDEX IF NOT EXISTS idx_forum_topic_reads_user ON forum_topic_reads(user_id);
-- Forum Reply Reads
CREATE TABLE IF NOT EXISTS forum_reply_reads (
id SERIAL PRIMARY KEY,
reply_id INTEGER NOT NULL REFERENCES forum_replies(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
read_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT uq_forum_reply_user_read UNIQUE (reply_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_forum_reply_reads_reply ON forum_reply_reads(reply_id);
CREATE INDEX IF NOT EXISTS idx_forum_reply_reads_user ON forum_reply_reads(user_id);
-- Classified (B2B) Reads
CREATE TABLE IF NOT EXISTS classified_reads (
id SERIAL PRIMARY KEY,
classified_id INTEGER NOT NULL REFERENCES classifieds(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
read_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT uq_classified_user_read UNIQUE (classified_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_classified_reads_classified ON classified_reads(classified_id);
CREATE INDEX IF NOT EXISTS idx_classified_reads_user ON classified_reads(user_id);
-- Grant permissions
GRANT ALL ON TABLE forum_topic_reads TO nordabiz_app;
GRANT ALL ON TABLE forum_reply_reads TO nordabiz_app;
GRANT ALL ON TABLE classified_reads TO nordabiz_app;
GRANT USAGE, SELECT ON SEQUENCE forum_topic_reads_id_seq TO nordabiz_app;
GRANT USAGE, SELECT ON SEQUENCE forum_reply_reads_id_seq TO nordabiz_app;
GRANT USAGE, SELECT ON SEQUENCE classified_reads_id_seq TO nordabiz_app;