Skip to content

Commit 054b038

Browse files
committed
[community-service] (feat): Implement discussion replies
1 parent 46d64d3 commit 054b038

3 files changed

Lines changed: 42 additions & 20 deletions

File tree

community/migrations/000001_create_comments_table.up.sql

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
-- Migration: Create comments table
1+
-- Migration: Create comments table (shared DB: tenants, users, posts from Cortex/Postal)
22
CREATE TABLE IF NOT EXISTS comments (
33
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
44
uuid UUID UNIQUE NOT NULL DEFAULT gen_random_uuid(),
55
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
66
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
7-
deleted_at TIMESTAMP,
8-
9-
tenant_id INT,
10-
post_id INT,
11-
user_id INT NOT NULL,
12-
parent_id INT,
7+
tenant_id INT REFERENCES tenants(id),
8+
post_id INT REFERENCES posts(id),
9+
user_id INT NOT NULL REFERENCES users(id),
10+
parent_id INT REFERENCES comments(id),
1311

1412
content TEXT NOT NULL,
1513

1614
status VARCHAR(20) NOT NULL DEFAULT 'pending',
1715
toxicity_score DECIMAL(3,2),
1816

19-
like_count INT DEFAULT 0,
20-
reply_count INT DEFAULT 0
21-
);
17+
like_count INT DEFAULT 0);
2218

23-
CREATE INDEX IF NOT EXISTS idx_comments_deleted_at ON comments(deleted_at);
2419
CREATE INDEX IF NOT EXISTS idx_comments_post_id ON comments(post_id);
2520
CREATE INDEX IF NOT EXISTS idx_comments_user_id ON comments(user_id);
2621
CREATE INDEX IF NOT EXISTS idx_comments_status ON comments(status);
@@ -35,6 +30,7 @@ END;
3530
$$ language 'plpgsql';
3631

3732
DROP TRIGGER IF EXISTS update_comments_updated_at ON comments;
33+
3834
CREATE TRIGGER update_comments_updated_at
3935
BEFORE UPDATE ON comments
4036
FOR EACH ROW

community/migrations/000002_create_discussions_table.up.sql

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,31 @@ CREATE TABLE IF NOT EXISTS discussions (
44
uuid UUID UNIQUE NOT NULL DEFAULT gen_random_uuid(),
55
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
66
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
7-
deleted_at TIMESTAMP,
7+
88

9-
tenant_id INT,
10-
user_id INT NOT NULL,
11-
category_id INT NOT NULL,
9+
tenant_id INT REFERENCES tenants(id),
10+
user_id INT NOT NULL REFERENCES users(id),
11+
category_id INT NOT NULL REFERENCES categories(id),
1212

1313
title VARCHAR(500) NOT NULL,
1414
slug VARCHAR(500) UNIQUE NOT NULL,
1515
content TEXT NOT NULL,
1616

1717
status VARCHAR(20) NOT NULL DEFAULT 'open',
18-
is_pinned BOOLEAN DEFAULT false,
1918

2019
upvote_count INT DEFAULT 0,
2120
view_count INT DEFAULT 0,
22-
reply_count INT DEFAULT 0,
23-
last_activity_at TIMESTAMP
21+
reply_count INT DEFAULT 0
2422
);
2523

26-
CREATE INDEX IF NOT EXISTS idx_discussions_deleted_at ON discussions(deleted_at);
27-
CREATE INDEX IF NOT EXISTS idx_discussions_slug ON discussions(slug);
2824
CREATE INDEX IF NOT EXISTS idx_discussions_user_id ON discussions(user_id);
2925
CREATE INDEX IF NOT EXISTS idx_discussions_category_id ON discussions(category_id);
3026
CREATE INDEX IF NOT EXISTS idx_discussions_status ON discussions(status);
27+
CREATE INDEX IF NOT EXISTS idx_discussions_tenant_id ON discussions(tenant_id);
3128

3229
DROP TRIGGER IF EXISTS update_discussions_updated_at ON discussions;
3330
CREATE TRIGGER update_discussions_updated_at
3431
BEFORE UPDATE ON discussions
3532
FOR EACH ROW
3633
EXECUTE FUNCTION update_updated_at_column();
34+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Migration: Create discussion_replies table
2+
CREATE TABLE IF NOT EXISTS discussion_replies (
3+
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
4+
uuid UUID UNIQUE NOT NULL DEFAULT gen_random_uuid(),
5+
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
6+
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
7+
8+
tenant_id INT REFERENCES tenants(id),
9+
discussion_id INT NOT NULL REFERENCES discussions(id),
10+
user_id INT NOT NULL REFERENCES users(id),
11+
parent_id INT REFERENCES discussion_replies(id),
12+
13+
content TEXT NOT NULL,
14+
15+
upvote_count INT DEFAULT 0,
16+
is_solution BOOLEAN DEFAULT false
17+
);
18+
19+
CREATE INDEX IF NOT EXISTS idx_discussion_replies_discussion_id ON discussion_replies(discussion_id);
20+
CREATE INDEX IF NOT EXISTS idx_discussion_replies_user_id ON discussion_replies(user_id);
21+
CREATE INDEX IF NOT EXISTS idx_discussion_replies_parent_id ON discussion_replies(parent_id);
22+
CREATE INDEX IF NOT EXISTS idx_discussion_replies_is_solution ON discussion_replies(is_solution);
23+
24+
DROP TRIGGER IF EXISTS update_discussion_replies_updated_at ON discussion_replies;
25+
CREATE TRIGGER update_discussion_replies_updated_at
26+
BEFORE UPDATE ON discussion_replies
27+
FOR EACH ROW
28+
EXECUTE FUNCTION update_updated_at_column();

0 commit comments

Comments
 (0)