-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
65 lines (55 loc) · 1.95 KB
/
Copy pathinit.sql
File metadata and controls
65 lines (55 loc) · 1.95 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
CREATE DATABASE library;
\c library;
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
login VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
version INTEGER DEFAULT 1,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS artists (
id UUID PRIMARY KEY,
name VARCHAR(255) NOT NULL,
grammy BOOLEAN NOT NULL,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS albums (
id UUID PRIMARY KEY,
name VARCHAR(255) NOT NULL,
year INTEGER NOT NULL,
artistId UUID,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (artistId) REFERENCES artists(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS tracks (
id UUID PRIMARY KEY,
name VARCHAR(255) NOT NULL,
duration INTEGER NOT NULL,
artistId UUID,
albumId UUID,
createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (artistId) REFERENCES artists(id) ON DELETE SET NULL,
FOREIGN KEY (albumId) REFERENCES albums(id) ON DELETE SET NULL
);
CREATE TABLE IF NOT EXISTS favorites (
id UUID PRIMARY KEY DEFAULT gen_random_uuid()
);
CREATE TABLE IF NOT EXISTS favorites_artists (
favorites_id UUID REFERENCES favorites(id) ON DELETE CASCADE,
artist_id UUID REFERENCES artists(id) ON DELETE CASCADE,
PRIMARY KEY (favorites_id, artist_id)
);
CREATE TABLE IF NOT EXISTS favorites_albums (
favorites_id UUID REFERENCES favorites(id) ON DELETE CASCADE,
album_id UUID REFERENCES albums(id) ON DELETE CASCADE,
PRIMARY KEY (favorites_id, album_id)
);
CREATE TABLE IF NOT EXISTS favorites_tracks (
favorites_id UUID REFERENCES favorites(id) ON DELETE CASCADE,
track_id UUID REFERENCES tracks(id) ON DELETE CASCADE,
PRIMARY KEY (favorites_id, track_id)
);