Files
f0bm/migration_comments.sql

38 lines
1.5 KiB
SQL

-- Create comments table
CREATE TABLE IF NOT EXISTS comments (
id SERIAL PRIMARY KEY,
item_id INTEGER NOT NULL REFERENCES items(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
parent_id INTEGER REFERENCES comments(id) ON DELETE SET NULL,
content TEXT NOT NULL,
is_deleted BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE,
vote_score INTEGER DEFAULT 0
);
-- Create notifications table
CREATE TABLE IF NOT EXISTS notifications (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
type VARCHAR(32) NOT NULL, -- 'reply', 'comment', 'mention'
reference_id INTEGER NOT NULL, -- ID of the comment
item_id INTEGER NOT NULL REFERENCES items(id) ON DELETE CASCADE,
is_read BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create comment_subscriptions table (for subscribing to posts)
CREATE TABLE IF NOT EXISTS comment_subscriptions (
user_id INTEGER NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
item_id INTEGER NOT NULL REFERENCES items(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
PRIMARY KEY (user_id, item_id)
);
-- Indexes for performance
CREATE INDEX idx_comments_item_id ON comments(item_id);
CREATE INDEX idx_comments_user_id ON comments(user_id);
CREATE INDEX idx_notifications_user_id ON notifications(user_id);
CREATE INDEX idx_notifications_unread ON notifications(user_id) WHERE is_read = FALSE;