Spaces:
Runtime error

File size: 9,292 Bytes
22a3ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aa90f1
 
 
 
 
 
 
 
22a3ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aa90f1
 
 
 
 
22a3ded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
CREATE EXTENSION IF NOT EXISTS pgcrypto;

CREATE TABLE IF NOT EXISTS users (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  email text NOT NULL UNIQUE,
  nickname text NOT NULL UNIQUE,
  password_hash text NOT NULL,
  avatar_url text DEFAULT '',
  email_verified boolean NOT NULL DEFAULT false,
  is_verified boolean NOT NULL DEFAULT false,
  role text NOT NULL DEFAULT 'user',
  blocked_at timestamptz,
  verification_token text,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);
ALTER TABLE users ADD COLUMN IF NOT EXISTS is_verified boolean NOT NULL DEFAULT false;
ALTER TABLE users ADD COLUMN IF NOT EXISTS role text NOT NULL DEFAULT 'user';
ALTER TABLE users ADD COLUMN IF NOT EXISTS blocked_at timestamptz;
UPDATE users SET is_verified = email_verified WHERE is_verified IS DISTINCT FROM email_verified;
CREATE INDEX IF NOT EXISTS users_role_idx ON users(role);

CREATE TABLE IF NOT EXISTS user_sessions (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  token text NOT NULL UNIQUE,
  created_at timestamptz NOT NULL DEFAULT now(),
  expires_at timestamptz NOT NULL
);

CREATE TABLE IF NOT EXISTS friendships (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  friend_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  created_at timestamptz NOT NULL DEFAULT now(),
  UNIQUE (user_id, friend_id)
);

CREATE TABLE IF NOT EXISTS friend_requests (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  sender_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  receiver_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  status text NOT NULL DEFAULT 'pending',
  seen_at timestamptz,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now(),
  UNIQUE (sender_id, receiver_id)
);
ALTER TABLE friend_requests ADD COLUMN IF NOT EXISTS seen_at timestamptz;

CREATE TABLE IF NOT EXISTS songs (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid REFERENCES users(id) ON DELETE CASCADE,
  title text NOT NULL,
  artist text DEFAULT '',
  cover_url text DEFAULT '',
  cover_position text DEFAULT '50% 50%',
  cover_scale double precision NOT NULL DEFAULT 1,
  file_url text NOT NULL,
  duration double precision,
  trim_start double precision,
  trim_end double precision,
  is_favorite boolean NOT NULL DEFAULT false,
  lyrics text,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);

ALTER TABLE songs ADD COLUMN IF NOT EXISTS trim_start double precision;
ALTER TABLE songs ADD COLUMN IF NOT EXISTS trim_end double precision;
ALTER TABLE songs ADD COLUMN IF NOT EXISTS cover_position text DEFAULT '50% 50%';
ALTER TABLE songs ADD COLUMN IF NOT EXISTS cover_scale double precision NOT NULL DEFAULT 1;
ALTER TABLE songs ADD COLUMN IF NOT EXISTS user_id uuid REFERENCES users(id) ON DELETE CASCADE;
CREATE INDEX IF NOT EXISTS songs_user_id_created_at_idx ON songs(user_id, created_at DESC);

CREATE TABLE IF NOT EXISTS youtube_cache (
  video_id text PRIMARY KEY,
  file_url text NOT NULL,
  cover_url text DEFAULT '',
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS playlists (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid REFERENCES users(id) ON DELETE CASCADE,
  name text NOT NULL,
  song_ids uuid[] NOT NULL DEFAULT '{}',
  cover_url text DEFAULT '',
  cover_position text DEFAULT '50% 50%',
  cover_scale double precision NOT NULL DEFAULT 1,
  is_public boolean NOT NULL DEFAULT false,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);

ALTER TABLE playlists ADD COLUMN IF NOT EXISTS is_public boolean NOT NULL DEFAULT false;
ALTER TABLE playlists ADD COLUMN IF NOT EXISTS cover_position text DEFAULT '50% 50%';
ALTER TABLE playlists ADD COLUMN IF NOT EXISTS cover_scale double precision NOT NULL DEFAULT 1;
ALTER TABLE playlists ADD COLUMN IF NOT EXISTS user_id uuid REFERENCES users(id) ON DELETE CASCADE;
CREATE INDEX IF NOT EXISTS playlists_user_id_created_at_idx ON playlists(user_id, created_at DESC);

CREATE TABLE IF NOT EXISTS listen_history (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id uuid REFERENCES users(id) ON DELETE CASCADE,
  song_id uuid,
  song_title text DEFAULT '',
  song_artist text DEFAULT '',
  listened_at bigint,
  mood text,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);
ALTER TABLE listen_history ADD COLUMN IF NOT EXISTS user_id uuid REFERENCES users(id) ON DELETE CASCADE;
CREATE INDEX IF NOT EXISTS listen_history_user_id_created_at_idx ON listen_history(user_id, created_at DESC);

CREATE TABLE IF NOT EXISTS collab_playlists (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  owner_id uuid REFERENCES users(id) ON DELETE CASCADE,
  name text NOT NULL,
  song_ids uuid[] NOT NULL DEFAULT '{}',
  cover_url text DEFAULT '',
  cover_position text DEFAULT '50% 50%',
  cover_scale double precision NOT NULL DEFAULT 1,
  access_level text NOT NULL DEFAULT 'collaborative',
  owner_email text DEFAULT '',
  collaborator_ids uuid[] NOT NULL DEFAULT '{}',
  collaborator_emails text[] NOT NULL DEFAULT '{}',
  last_edited_by text,
  last_edited_at bigint,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);

ALTER TABLE collab_playlists ADD COLUMN IF NOT EXISTS cover_position text DEFAULT '50% 50%';
ALTER TABLE collab_playlists ADD COLUMN IF NOT EXISTS cover_scale double precision NOT NULL DEFAULT 1;
ALTER TABLE collab_playlists ADD COLUMN IF NOT EXISTS owner_id uuid REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE collab_playlists ADD COLUMN IF NOT EXISTS collaborator_ids uuid[] NOT NULL DEFAULT '{}';
ALTER TABLE collab_playlists ADD COLUMN IF NOT EXISTS access_level text NOT NULL DEFAULT 'collaborative';
UPDATE collab_playlists
SET owner_id = users.id
FROM users
WHERE collab_playlists.owner_id IS NULL
  AND lower(collab_playlists.owner_email) = lower(users.email);
CREATE INDEX IF NOT EXISTS collab_playlists_owner_id_created_at_idx ON collab_playlists(owner_id, created_at DESC);
CREATE INDEX IF NOT EXISTS collab_playlists_collaborator_ids_gin_idx ON collab_playlists USING gin(collaborator_ids);

CREATE TABLE IF NOT EXISTS collab_playlist_invites (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  playlist_id uuid NOT NULL REFERENCES collab_playlists(id) ON DELETE CASCADE,
  sender_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  receiver_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  status text NOT NULL DEFAULT 'pending',
  seen_at timestamptz,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now(),
  UNIQUE (playlist_id, receiver_id)
);
CREATE INDEX IF NOT EXISTS collab_playlist_invites_receiver_status_idx ON collab_playlist_invites(receiver_id, status, created_at DESC);

CREATE TABLE IF NOT EXISTS song_shares (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  sender_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  receiver_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  song_id uuid REFERENCES songs(id) ON DELETE CASCADE,
  message text DEFAULT '',
  read_at timestamptz,
  created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS song_shares_receiver_id_created_at_idx ON song_shares(receiver_id, created_at DESC);

CREATE OR REPLACE FUNCTION touch_updated_at()
RETURNS trigger AS $$
BEGIN
  NEW.updated_at = now();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS songs_touch_updated_at ON songs;
CREATE TRIGGER songs_touch_updated_at
BEFORE UPDATE ON songs
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS youtube_cache_touch_updated_at ON youtube_cache;
CREATE TRIGGER youtube_cache_touch_updated_at
BEFORE UPDATE ON youtube_cache
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS users_touch_updated_at ON users;
CREATE TRIGGER users_touch_updated_at
BEFORE UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS playlists_touch_updated_at ON playlists;
CREATE TRIGGER playlists_touch_updated_at
BEFORE UPDATE ON playlists
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS listen_history_touch_updated_at ON listen_history;
CREATE TRIGGER listen_history_touch_updated_at
BEFORE UPDATE ON listen_history
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS collab_playlists_touch_updated_at ON collab_playlists;
CREATE TRIGGER collab_playlists_touch_updated_at
BEFORE UPDATE ON collab_playlists
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS friend_requests_touch_updated_at ON friend_requests;
CREATE TRIGGER friend_requests_touch_updated_at
BEFORE UPDATE ON friend_requests
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();

DROP TRIGGER IF EXISTS collab_playlist_invites_touch_updated_at ON collab_playlist_invites;
CREATE TRIGGER collab_playlist_invites_touch_updated_at
BEFORE UPDATE ON collab_playlist_invites
FOR EACH ROW EXECUTE FUNCTION touch_updated_at();