-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.sql
More file actions
162 lines (146 loc) · 5.06 KB
/
Copy pathdb.sql
File metadata and controls
162 lines (146 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
-- Creating the 'member' table
CREATE TABLE member (
member_id SERIAL PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(255),
phone_number VARCHAR(20),
profile_pic VARCHAR(255)
);
-- Creating the 'admin' table
CREATE TABLE admin (
admin_id SERIAL PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(255) NOT NULL,
phone_number VARCHAR(20),
admin_level INTEGER CHECK (admin_level BETWEEN 1 AND 3),
profile_pic VARCHAR(255)
);
CREATE TABLE posts (
post_id SERIAL PRIMARY KEY,
post_title VARCHAR(255) NOT NULL,
post_content TEXT NOT NULL,
post_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
like_count INTEGER DEFAULT 0,
view_count INTEGER DEFAULT 0,
author_type VARCHAR(100) NOT NULL,
author_id INTEGER NOT NULL,
CHECK (author_type IN ('member', 'admin')),
FOREIGN KEY (author_id) REFERENCES member(member_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED
);
-- Trigger function to enforce the author_type constraint
CREATE OR REPLACE FUNCTION enforce_author_type_posts()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.author_type = 'admin' THEN
IF NOT EXISTS (SELECT 1 FROM admin WHERE admin_id = NEW.author_id) THEN
RAISE EXCEPTION 'Invalid admin_id for author_type admin';
END IF;
ELSIF NEW.author_type = 'member' THEN
IF NOT EXISTS (SELECT 1 FROM member WHERE member_id = NEW.author_id) THEN
RAISE EXCEPTION 'Invalid member_id for author_type member';
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Trigger to enforce the author_type constraint
CREATE TRIGGER enforce_author_type_trigger_posts
BEFORE INSERT OR UPDATE ON posts
FOR EACH ROW EXECUTE FUNCTION enforce_author_type_posts();
CREATE TABLE comments (
comment_id SERIAL PRIMARY KEY,
comment_content VARCHAR(255) NOT NULL,
comment_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
author_type VARCHAR(100) NOT NULL,
author_id INTEGER NOT NULL,
post_id INTEGER NOT NULL,
CHECK (author_type IN ('member', 'admin')),
FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE
);
-- Trigger function to enforce the author_type constraint
CREATE OR REPLACE FUNCTION enforce_author_type_comments()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.author_type = 'admin' THEN
IF NOT EXISTS (SELECT 1 FROM admin WHERE admin_id = NEW.author_id) THEN
RAISE EXCEPTION 'Invalid admin_id for author_type admin';
END IF;
ELSIF NEW.author_type = 'member' THEN
IF NOT EXISTS (SELECT 1 FROM member WHERE member_id = NEW.author_id) THEN
RAISE EXCEPTION 'Invalid member_id for author_type member';
END IF;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Trigger to enforce the author_type constraint
CREATE TRIGGER enforce_author_type_trigger_comments
BEFORE INSERT OR UPDATE ON comments
FOR EACH ROW EXECUTE FUNCTION enforce_author_type_comments();
-- Creating the 'friends' relationship table
CREATE TABLE friends (
user_id1 INTEGER,
user_id2 INTEGER,
confirmed BOOLEAN DEFAULT FALSE, -- To handle friend requests
PRIMARY KEY (user_id1, user_id2),
FOREIGN KEY (user_id1) REFERENCES member(member_id) ON DELETE CASCADE,
FOREIGN KEY (user_id2) REFERENCES member(member_id) ON DELETE CASCADE,
CHECK (user_id1 <> user_id2) -- Ensure a user cannot befriend themselves
);
-- Creating the 'block' table
CREATE TABLE block (
blocker_id INTEGER,
blocked_id INTEGER,
PRIMARY KEY (blocker_id, blocked_id),
FOREIGN KEY (blocker_id) REFERENCES member(member_id) ON DELETE CASCADE,
FOREIGN KEY (blocked_id) REFERENCES member(member_id) ON DELETE CASCADE,
CHECK (blocker_id <> blocked_id) -- Ensure a user cannot block themselves
);
CREATE TABLE post_likes (
user_id INTEGER,
post_id INTEGER,
PRIMARY KEY (user_id, post_id),
FOREIGN KEY (user_id) REFERENCES member(member_id) ON DELETE CASCADE,
FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE
);
CREATE TABLE comment_likes (
user_id INTEGER,
comment_id INTEGER,
PRIMARY KEY (user_id, comment_id),
FOREIGN KEY (user_id) REFERENCES member(member_id) ON DELETE CASCADE,
FOREIGN KEY (comment_id) REFERENCES comments(comment_id) ON DELETE CASCADE
);
-- Create the password_reset_tokens table
CREATE TABLE password_reset_tokens (
token_id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
reset_token VARCHAR(255) NOT NULL,
reset_token_expires TIMESTAMP NOT NULL,
FOREIGN KEY (user_id) REFERENCES member(member_id) ON DELETE CASCADE
);
-- Creating the 'user_authentication' view
CREATE VIEW user_authentication AS
SELECT
member_id AS user_id,
username,
email,
password_hash,
full_name,
phone_number,
profile_pic,
'member' AS user_type
FROM member
UNION ALL
SELECT
admin_id AS user_id,
username,
NULL AS email,
password_hash,
full_name,
phone_number,
profile_pic,
'admin' AS user_type
FROM admin;