-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamodel.sql
63 lines (52 loc) · 2.2 KB
/
datamodel.sql
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
create schema spotify;
set search_path to spotify, public;
create table spotify.track (id varchar primary key, name varchar, popularity integer, url varchar, album_id varchar, artist_id varchar, user_id varchar);
create table spotify.track_features (id varchar primary key, duration numeric, danceability numeric, energy numeric, key varchar, loudness numeric, speechiness numeric, instrumentalness numeric, liveness numeric, tempo numeric);
create table spotify.spotify_user (id varchar primary key, url varchar);
create table spotify.artist (id varchar primary key, name varchar, url varchar );
create table spotify.album (id varchar primary key, name varchar, release_date date, total_tracks integer, image varchar, url varchar, artist_id varchar);
create table spotify.osm (id varchar primary key, geo_name varchar, population integer, wikipedia varchar, geom geometry);
-- add generated column to add centroid-geometrie to the osm-table
alter table osm
add column centroid geometry generated always as (
case
when st_geometrytype(geom) != 'ST_Point' then st_setsrid(st_centroid(geom), 3857)
end
) stored;
-- change srid of geometry osm
update osm
set geom = st_setsrid(geom, 3857);
-- View that merges most data of the tables
set search_path to spotify;
create materialized view mat_geo_spotify as
select
t.id track_id,
t.name track_name,
osm.geo_name geo_name,
osm.population population,
artist.name artist_name,
t.popularity popularity_song,
track_features.key key,
t.url track_url,
album.release_date album_release,
album.image,
album.total_tracks total_tracks,
su.id added_by,
osm.wikipedia,
osm.geom,
osm.centroid
from track t
join track_features on track_features.id = t.id
join album on album.id = t.album_id
join artist on artist.id = t.artist_id
join spotify_user su on su.id = t.user_id
join osm on osm.id = t.id;
create unique index on mat_geo_spotify (track_id);
alter schema spotify owner to geodata;
alter table album owner to geodata;
alter table artist owner to geodata;
alter table osm owner to geodata;
alter table spotify_user owner to geodata;
alter table track owner to geodata;
alter table track_features owner to geodata;
alter materialized view mat_geo_spotify owner to geodata;