Repository navigation
Expand file tree
/
Copy pathinit.sql
More file actions
133 lines (123 loc) ยท 4.34 KB
/
Copy pathinit.sql
File metadata and controls
133 lines (123 loc) ยท 4.34 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
-- Connect to the default 'postgres' database
\c postgres;
-- Check if the database exists, if not, create it
DO
$do$
BEGIN
IF NOT EXISTS (SELECT FROM pg_database WHERE datname = 'twitsnap_users') THEN
CREATE DATABASE twitsnap_users;
END IF;
END
$do$;
-- Connect to the twitsnap_users database
\c twitsnap_users;
-- Create the users table if it doesn't exist
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
name VARCHAR(100) NOT NULL,
lastname VARCHAR(100) NOT NULL,
birthdate DATE NOT NULL,
profile_picture VARCHAR(1024) NULL,
background_picture VARCHAR(1024) NULL,
password VARCHAR(255) NULL, -- NULL for social login users
sso_uid VARCHAR(255) NULL,
provider_id VARCHAR(50) NULL,
is_private BOOLEAN NOT NULL DEFAULT false,
is_blocked BOOLEAN NOT NULL DEFAULT false,
expo_token VARCHAR(100) NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
latitude FLOAT,
longitude FLOAT,
phone_number VARCHAR(20) UNIQUE NOT NULL,
verified BOOLEAN NOT NULL DEFAULT false
);
-- Create a unique partial index on sso_uid if it does not exist
CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_sso_uid ON users (sso_uid) WHERE sso_uid IS NOT NULL;
-- Create the admins table if it doesn't exist
CREATE TABLE IF NOT EXISTS admins (
username VARCHAR(50) PRIMARY KEY,
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS follows (
userId INT NOT NULL,
followedId INT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (userId, followedId),
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (followedId) REFERENCES users(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS interests (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
parent_id INT NULL,
emoji VARCHAR(50) NULL,
FOREIGN KEY (parent_id) REFERENCES interests(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS user_interests (
user_id INT NOT NULL,
interest_id INT NOT NULL,
PRIMARY KEY (user_id, interest_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (interest_id) REFERENCES interests(id) ON DELETE CASCADE
);
-- Insert interests only if the table is empty
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM interests) THEN
INSERT INTO interests (name, parent_id, emoji) VALUES
('Sports', NULL, '๐
'),
('Soccer', 1, 'โฝ'),
('Swimming', 1, '๐'),
('Basketball', 1, '๐'),
('Tennis', 1, '๐พ'),
('Volleyball', 1, '๐'),
('Baseball', 1, 'โพ'),
('Athletics', 1, '๐'),
('Rugby', 1, '๐'),
('Cricket', 1, '๐'),
('Tech', NULL, '๐ป'),
('Programming', 11, '๐จโ๐ป'),
('Web Development', 11, '๐'),
('Mobile Development', 11, '๐ฑ'),
('AI & Machine Learning', 11, '๐ค'),
('Cybersecurity', 11, '๐'),
('Cloud Computing', 11, 'โ๏ธ'),
('Data Science', 11, '๐'),
('DevOps', 11, '๐ง'),
('Blockchain', 11, 'โ๏ธ'),
('Arts', NULL, '๐จ'),
('Painting', 21, '๐๏ธ'),
('Drawing', 21, 'โ๏ธ'),
('Photography', 21, '๐ธ'),
('Sculpture', 21, '๐ฟ'),
('Digital Art', 21, '๐ผ๏ธ'),
('Animation', 21, '๐๏ธ'),
('Graphic Design', 21, '๐๏ธ'),
('Film Making', 21, '๐ฌ'),
('Music', 21, '๐ต'),
('Science', NULL, '๐ฌ'),
('Physics', 31, 'โ๏ธ'),
('Chemistry', 31, '๐งช'),
('Biology', 31, '๐งฌ'),
('Astronomy', 31, '๐'),
('Mathematics', 31, 'โ'),
('Environmental Science', 31, '๐'),
('Geology', 31, '๐ชจ'),
('Robotics', 31, '๐ค'),
('Genetics', 31, '๐งฌ'),
('Languages', NULL, '๐'),
('English', 41, '๐ฌ๐ง'),
('Spanish', 41, '๐ช๐ธ'),
('French', 41, '๐ซ๐ท'),
('German', 41, '๐ฉ๐ช'),
('Chinese', 41, '๐จ๐ณ'),
('Japanese', 41, '๐ฏ๐ต'),
('Korean', 41, '๐ฐ๐ท'),
('Italian', 41, '๐ฎ๐น'),
('Portuguese', 41, '๐ง๐ท');
END IF;
END $$;