Skip to content

Commit

Permalink
Update SQL files according to new YIM tables
Browse files Browse the repository at this point in the history
  • Loading branch information
amCap1712 committed Jan 5, 2024
1 parent 7c8515f commit 2cba086
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 105 deletions.
12 changes: 0 additions & 12 deletions admin/sql/create_foreign_keys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ ALTER TABLE api_compat.session
REFERENCES "user" (id)
ON DELETE CASCADE;

ALTER TABLE statistics.user
ADD CONSTRAINT user_stats_user_id_foreign_key
FOREIGN KEY (user_id)
REFERENCES "user" (id)
ON DELETE CASCADE;

ALTER TABLE statistics.year_in_music
ADD CONSTRAINT user_stats_year_in_music_user_id_foreign_key
FOREIGN KEY (user_id)
REFERENCES "user" (id)
ON DELETE CASCADE;

ALTER TABLE reported_users
ADD CONSTRAINT reporter_user_id_foreign_key
FOREIGN KEY (reporter_user_id)
Expand Down
9 changes: 0 additions & 9 deletions admin/sql/create_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ CREATE UNIQUE INDEX token_api_key_ndx_token ON api_compat.token (token, api_key)
CREATE UNIQUE INDEX sid_ndx_session ON api_compat.session (sid);
CREATE UNIQUE INDEX sid_api_key_ndx_session ON api_compat.session (sid, api_key);

CREATE UNIQUE INDEX msid_ndx_artist_stats ON statistics.artist (msid);
CREATE UNIQUE INDEX msid_ndx_release_stats ON statistics.release (msid);
CREATE UNIQUE INDEX msid_ndx_recording_stats ON statistics.recording (msid);

CREATE UNIQUE INDEX user_type_range_ndx_stats ON statistics.user (user_id, stats_type, stats_range);
CREATE INDEX user_id_ndx__user_stats ON statistics.user (user_id);

CREATE INDEX latest_listened_at_spotify_auth ON spotify_auth (latest_listened_at DESC NULLS LAST);

CREATE INDEX user_id_ndx_external_service_oauth ON external_service_oauth (user_id);
Expand Down Expand Up @@ -62,6 +55,4 @@ CREATE UNIQUE INDEX caa_id_ndx_release_color ON release_color (caa_id);

CREATE UNIQUE INDEX user_id_ndx_user_setting ON user_setting (user_id);

CREATE UNIQUE INDEX user_id_year_in_music_uniq_idx ON statistics.year_in_music (user_id, year);

COMMIT;
6 changes: 0 additions & 6 deletions admin/sql/create_primary_keys.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ ALTER TABLE spotify_auth ADD CONSTRAINT spotify_auth_pkey PRIMARY KEY (user_id);
ALTER TABLE external_service_oauth ADD CONSTRAINT external_service_oauth_pkey PRIMARY KEY (id);
ALTER TABLE listens_importer ADD CONSTRAINT listens_importer_pkey PRIMARY KEY (id);

ALTER TABLE statistics.artist ADD CONSTRAINT stats_artist_pkey PRIMARY KEY (id);
ALTER TABLE statistics.release ADD CONSTRAINT stats_release_pkey PRIMARY KEY (id);
ALTER TABLE statistics.recording ADD CONSTRAINT stats_recording_pkey PRIMARY KEY (id);
ALTER TABLE statistics.user ADD CONSTRAINT stats_user_pkey PRIMARY KEY (id);
ALTER TABLE statistics.year_in_music ADD CONSTRAINT stats_year_in_music_pkey PRIMARY KEY (id);

ALTER TABLE recommendation.cf_recording ADD CONSTRAINT rec_cf_recording_pkey PRIMARY KEY (id);
ALTER TABLE recommendation.recommender ADD CONSTRAINT rec_recommender_pkey PRIMARY KEY (id);
ALTER TABLE recommendation.recommender_session ADD CONSTRAINT rec_recommender_session_pkey PRIMARY KEY (id);
Expand Down
1 change: 0 additions & 1 deletion admin/sql/create_schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
CREATE SCHEMA api_compat;
CREATE SCHEMA statistics;
CREATE SCHEMA recommendation;
70 changes: 0 additions & 70 deletions admin/sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -143,76 +143,6 @@ CREATE TABLE listens_importer (
error_message TEXT
);

CREATE TABLE statistics.artist (
id SERIAL, -- PK
msid UUID NOT NULL,
name VARCHAR,
release JSONB,
recording JSONB,
listener JSONB,
listen_count JSONB,
last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);
ALTER TABLE statistics.artist ADD CONSTRAINT artist_stats_msid_uniq UNIQUE (msid);

CREATE TABLE statistics.release (
id SERIAL, -- PK
msid UUID NOT NULL,
name VARCHAR,
recording JSONB,
listener JSONB,
listen_count JSONB,
last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);
ALTER TABLE statistics.release ADD CONSTRAINT release_stats_msid_uniq UNIQUE (msid);

CREATE TABLE statistics.recording (
id SERIAL, -- PK
msid UUID NOT NULL,
name VARCHAR,
listener JSONB,
listen_count JSONB,
last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()

);
ALTER TABLE statistics.recording ADD CONSTRAINT recording_stats_msid_uniq UNIQUE (msid);

CREATE TABLE statistics.user (
id SERIAL, -- PK
user_id INTEGER NOT NULL, -- FK to "user".id
stats_type user_stats_type,
stats_range stats_range_type,
data JSONB,
count INTEGER,
-- we use int timestamps when serializing data in spark, we return the same from the api
-- using timestamp with time zone here just complicates stuff. we'll need to add
-- datetime/timestamp conversions in code at multiple places and we never seem to use this
-- value anyways in LB backend atm.
from_ts BIGINT,
to_ts BIGINT,
last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE TABLE statistics.sitewide (
id SERIAL, --pk
stats_range TEXT,
artist JSONB,
release JSONB,
recording JSONB,
listening_activity JSONB,
last_updated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

ALTER TABLE statistics.sitewide ADD CONSTRAINT stats_range_uniq UNIQUE (stats_range);

CREATE TABLE statistics.year_in_music (
id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
user_id INTEGER NOT NULL, -- FK to "user".id
year SMALLINT NOT NULL,
data JSONB
);


CREATE TABLE recommendation_feedback (
id SERIAL, -- PK
user_id INTEGER NOT NULL, -- FK to "user".id
Expand Down
7 changes: 0 additions & 7 deletions admin/sql/reset_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ DELETE FROM hide_user_timeline_event CASCADE;
DELETE FROM external_service_oauth CASCADE;
DELETE FROM listens_importer CASCADE;

-- DELETE FROM statistics.release CASCADE;
-- DELETE FROM statistics.artist CASCADE;
-- DELETE FROM statistics.recording CASCADE;
DELETE FROM statistics.user CASCADE;
-- DELETE FROM statistics.sitewide CASCADE;
-- DELETE FROM statistics.year_in_music CASCADE;

DELETE FROM recording_feedback CASCADE;
DELETE FROM recommendation_feedback CASCADE;
DELETE FROM user_relationship CASCADE;
Expand Down
1 change: 1 addition & 0 deletions admin/timescale/create_schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ CREATE SCHEMA apple_cache;
CREATE SCHEMA similarity;
CREATE SCHEMA tags;
CREATE SCHEMA popularity;
CREATE SCHEMA statistics; -- this schema is used to store the YIM tables, tables are created dynamically in listenbrainz/db/year_in_music.py

0 comments on commit 2cba086

Please sign in to comment.