-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
189 lines (176 loc) · 6.01 KB
/
init.sql
File metadata and controls
189 lines (176 loc) · 6.01 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
182
183
184
185
186
187
188
189
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE OR REPLACE FUNCTION update_timestamp_modified_column()
RETURNS TRIGGER AS $$
BEGIN
IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN
NEW.timestamp_modified = now();
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;
$$ language 'plpgsql';
CREATE TABLE IF NOT EXISTS Djs (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
name varchar(255) NOT NULL,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_dj_timestamp_modified') THEN
CREATE TRIGGER update_dj_timestamp_modified BEFORE UPDATE ON Djs FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Artists (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
name varchar(255) NOT NULL,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_artists_timestamp_modified') THEN
CREATE TRIGGER update_artists_timestamp_modified BEFORE UPDATE ON Artists FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Songs (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
title varchar(255) NOT NULL,
artist_id uuid NOT NULL,
release_date date,
duration int,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp,
FOREIGN KEY (artist_id) REFERENCES Artists(id)
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_songs_timestamp_modified') THEN
CREATE TRIGGER update_songs_timestamp_modified BEFORE UPDATE ON Songs FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Venues (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
name varchar(255) NOT NULL,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_venues_timestamp_modified') THEN
CREATE TRIGGER update_venues_timestamp_modified BEFORE UPDATE ON Venues FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Occasions (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
name varchar(255) NOT NULL,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_occasions_timestamp_modified') THEN
CREATE TRIGGER update_occasions_timestamp_modified BEFORE UPDATE ON Occasions FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Sets (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
dj_id uuid NOT NULL,
occasion_id uuid,
venue_id uuid,
recording_date date,
source text UNIQUE,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp,
FOREIGN KEY (dj_id) REFERENCES Djs(id),
FOREIGN KEY (occasion_id) REFERENCES Occasions(id),
FOREIGN KEY (venue_id) REFERENCES Venues(id)
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_sets_timestamp_modified') THEN
CREATE TRIGGER update_sets_timestamp_modified BEFORE UPDATE ON Sets FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Transitions (
song_from uuid NOT NULL,
song_to uuid NOT NULL,
set_id uuid NOT NULL,
FOREIGN KEY (song_from) REFERENCES Songs(id),
FOREIGN KEY (song_to) REFERENCES Songs(id),
FOREIGN KEY (set_id) REFERENCES Sets(id),
CONSTRAINT Transitions_UQ UNIQUE(song_from, song_to, set_id)
);
CREATE TABLE IF NOT EXISTS Tags (
id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
name varchar(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS Song_Tags (
song_id uuid,
tag_id uuid,
source text,
FOREIGN KEY (song_id) REFERENCES Songs(id),
FOREIGN KEY (tag_id) REFERENCES Tags(id),
CONSTRAINT Song_Tags_UQ UNIQUE(song_id, tag_id, source)
);
CREATE TABLE IF NOT EXISTS Artist_Tags (
artist_id uuid,
tag_id uuid,
source text,
FOREIGN KEY (artist_id) REFERENCES Artists(id),
FOREIGN KEY (tag_id) REFERENCES Tags(id),
CONSTRAINT Artist_Id_UQ UNIQUE(artist_id, tag_id, source)
);
CREATE TABLE IF NOT EXISTS Spotify_Songs (
spotify_uri varchar(255) PRIMARY KEY,
song_id uuid NOT NULL UNIQUE,
acousticness float,
danceability float,
duration_ms int,
energy float,
instrumentalness float,
key int,
mode int,
liveness float,
loudness float,
speechiness float,
tempo float,
time_signature int,
valence float,
preview_url varchar,
image_url_large varchar,
image_url_small varchar,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp,
FOREIGN KEY (song_id) REFERENCES Songs(id)
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_spotify_songs_timestamp_modified') THEN
CREATE TRIGGER update_spotify_songs_timestamp_modified BEFORE UPDATE ON Spotify_Songs FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;
CREATE TABLE IF NOT EXISTS Spotify_Artists (
spotify_uri varchar(255) PRIMARY KEY,
artist_id uuid NOT NULL,
followers int,
popularity int,
timestamp_added timestamp default current_timestamp,
timestamp_modified timestamp,
FOREIGN KEY (artist_id) REFERENCES Artists(id)
);
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_spotify_artists_timestamp_modified') THEN
CREATE TRIGGER update_spotify_artists_timestamp_modified BEFORE UPDATE ON Spotify_Artists FOR EACH ROW EXECUTE PROCEDURE update_timestamp_modified_column();
END IF;
END
$$;