-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.sql
More file actions
181 lines (166 loc) · 7.1 KB
/
database.sql
File metadata and controls
181 lines (166 loc) · 7.1 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Users table
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
last_login_at TIMESTAMP WITH TIME ZONE
);
-- Profiles table
CREATE TABLE IF NOT EXISTS profiles (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
username VARCHAR(30) UNIQUE NOT NULL,
display_name VARCHAR(50),
avatar_url TEXT,
bio TEXT,
is_public BOOLEAN DEFAULT true,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- User stats table
CREATE TABLE IF NOT EXISTS user_stats (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
total_pomodoros INTEGER DEFAULT 0,
total_focus_time INTEGER DEFAULT 0,
current_streak INTEGER DEFAULT 0,
longest_streak INTEGER DEFAULT 0,
weekly_pomodoros INTEGER DEFAULT 0,
monthly_pomodoros INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- User settings table
CREATE TABLE IF NOT EXISTS user_settings (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
durations JSONB DEFAULT '{"FOCUS": 25, "SHORT": 5, "LONG": 15}',
goals JSONB DEFAULT '{"daily": 8, "weekly": 40}',
settings JSONB DEFAULT '{}',
sound_presets JSONB DEFAULT '[]',
theme VARCHAR(50) DEFAULT 'default',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Tasks table
CREATE TABLE IF NOT EXISTS tasks (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
text TEXT NOT NULL,
completed BOOLEAN DEFAULT false,
project_id UUID,
priority VARCHAR(20) DEFAULT 'medium',
estimated_pomodoros INTEGER DEFAULT 1,
completed_pomodoros INTEGER DEFAULT 0,
subtasks JSONB DEFAULT '[]',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Projects table
CREATE TABLE IF NOT EXISTS projects (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name VARCHAR(100) NOT NULL,
color VARCHAR(7) DEFAULT '#6366f1',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Pomodoro sessions table
CREATE TABLE IF NOT EXISTS pomodoro_sessions (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
project_id UUID,
task_id UUID,
duration INTEGER NOT NULL,
session_type VARCHAR(20) DEFAULT 'focus',
completed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Sync state table
CREATE TABLE IF NOT EXISTS sync_state (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID UNIQUE NOT NULL REFERENCES users(id) ON DELETE CASCADE,
last_synced_at TIMESTAMP WITH TIME ZONE,
device_id VARCHAR(255),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Friendships table
CREATE TABLE IF NOT EXISTS friendships (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
friend_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
status VARCHAR(20) DEFAULT 'pending',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, friend_id)
);
-- Rooms table
CREATE TABLE IF NOT EXISTS rooms (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(100) NOT NULL,
description TEXT,
is_private BOOLEAN DEFAULT false,
password_hash VARCHAR(255),
max_participants INTEGER DEFAULT 10,
status VARCHAR(20) DEFAULT 'active',
created_by UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Room participants table
CREATE TABLE IF NOT EXISTS room_participants (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
room_id UUID NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
joined_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
left_at TIMESTAMP WITH TIME ZONE,
is_active BOOLEAN DEFAULT true,
is_host BOOLEAN DEFAULT false,
current_task TEXT,
UNIQUE(room_id, user_id)
);
-- Achievements table
CREATE TABLE IF NOT EXISTS achievements (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(100) NOT NULL,
description TEXT,
icon VARCHAR(100),
requirement_type VARCHAR(50),
requirement_value INTEGER,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- User achievements table
CREATE TABLE IF NOT EXISTS user_achievements (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
achievement_id UUID NOT NULL REFERENCES achievements(id) ON DELETE CASCADE,
earned_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, achievement_id)
);
-- Indexes
CREATE INDEX IF NOT EXISTS idx_tasks_user_id ON tasks(user_id);
CREATE INDEX IF NOT EXISTS idx_tasks_project_id ON tasks(project_id);
CREATE INDEX IF NOT EXISTS idx_projects_user_id ON projects(user_id);
CREATE INDEX IF NOT EXISTS idx_pomodoro_sessions_user_id ON pomodoro_sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_pomodoro_sessions_completed_at ON pomodoro_sessions(completed_at);
CREATE INDEX IF NOT EXISTS idx_friendships_user_id ON friendships(user_id);
CREATE INDEX IF NOT EXISTS idx_friendships_friend_id ON friendships(friend_id);
CREATE INDEX IF NOT EXISTS idx_rooms_status ON rooms(status);
CREATE INDEX IF NOT EXISTS idx_room_participants_room_id ON room_participants(room_id);
CREATE INDEX IF NOT EXISTS idx_profiles_username ON profiles(username);
CREATE INDEX IF NOT EXISTS idx_user_stats_total_pomodoros ON user_stats(total_pomodoros DESC);
CREATE INDEX IF NOT EXISTS idx_user_stats_weekly_pomodoros ON user_stats(weekly_pomodoros DESC);
-- Insert default achievements
INSERT INTO achievements (name, description, icon, requirement_type, requirement_value) VALUES
('First Steps', 'Complete your first pomodoro', 'trophy', 'pomodoros', 1),
('Getting Started', 'Complete 10 pomodoros', 'award', 'pomodoros', 10),
('Focus Master', 'Complete 100 pomodoros', 'medal', 'pomodoros', 100),
('Productivity Pro', 'Complete 500 pomodoros', 'crown', 'pomodoros', 500),
('Week Warrior', 'Complete 40 pomodoros in a week', 'flame', 'weekly_pomodoros', 40),
('Streak Starter', 'Maintain a 3-day streak', 'zap', 'streak', 3),
('Streak Keeper', 'Maintain a 7-day streak', 'fire', 'streak', 7),
('Streak Master', 'Maintain a 30-day streak', 'star', 'streak', 30)
ON CONFLICT DO NOTHING;