From 81b1490cce6cd2c05fe7f3459e76a288f0a4ad38 Mon Sep 17 00:00:00 2001 From: Carter Bertolini Date: Fri, 1 Nov 2024 16:37:22 -0400 Subject: [PATCH] Create playlist database migration --- .../down.sql | 5 +++++ .../up.sql | 17 ++++++++++++++ src/schema.rs | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 migrations/2024-10-22-212759_create_playlist_tables/down.sql create mode 100644 migrations/2024-10-22-212759_create_playlist_tables/up.sql diff --git a/migrations/2024-10-22-212759_create_playlist_tables/down.sql b/migrations/2024-10-22-212759_create_playlist_tables/down.sql new file mode 100644 index 0000000..defde9d --- /dev/null +++ b/migrations/2024-10-22-212759_create_playlist_tables/down.sql @@ -0,0 +1,5 @@ +DROP INDEX playlists_owner_idx; +DROP TABLE playlists; + +DROP INDEX playlist_songs_playlist_idx; +DROP TABLE playlist_songs; diff --git a/migrations/2024-10-22-212759_create_playlist_tables/up.sql b/migrations/2024-10-22-212759_create_playlist_tables/up.sql new file mode 100644 index 0000000..cf35cb8 --- /dev/null +++ b/migrations/2024-10-22-212759_create_playlist_tables/up.sql @@ -0,0 +1,17 @@ +CREATE TABLE playlists ( + id SERIAL PRIMARY KEY, + created_at TIMESTAMP NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP NOT NULL DEFAULT NOW(), + owner_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL, + name TEXT NOT NULL +); + +CREATE INDEX playlists_owner_idx ON playlists(owner_id); + +CREATE TABLE playlist_songs ( + playlist_id INTEGER REFERENCES playlists(id) ON DELETE CASCADE NOT NULL, + song_id INTEGER REFERENCES songs(id) ON DELETE CASCADE NOT NULL, + PRIMARY KEY (playlist_id, song_id) +); + +CREATE INDEX playlist_songs_playlist_idx ON playlist_songs(playlist_id); diff --git a/src/schema.rs b/src/schema.rs index 29401e7..31aebd6 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -39,6 +39,23 @@ diesel::table! { } } +diesel::table! { + playlist_songs (playlist_id, song_id) { + playlist_id -> Int4, + song_id -> Int4, + } +} + +diesel::table! { + playlists (id) { + id -> Int4, + created_at -> Timestamp, + updated_at -> Timestamp, + owner_id -> Int4, + name -> Text, + } +} + diesel::table! { song_artists (song_id, artist_id) { song_id -> Int4, @@ -95,6 +112,9 @@ diesel::table! { diesel::joinable!(album_artists -> albums (album_id)); diesel::joinable!(album_artists -> artists (artist_id)); +diesel::joinable!(playlist_songs -> playlists (playlist_id)); +diesel::joinable!(playlist_songs -> songs (song_id)); +diesel::joinable!(playlists -> users (owner_id)); diesel::joinable!(song_artists -> artists (artist_id)); diesel::joinable!(song_artists -> songs (song_id)); diesel::joinable!(song_dislikes -> songs (song_id)); @@ -111,6 +131,8 @@ diesel::allow_tables_to_appear_in_same_query!( artists, friend_requests, friendships, + playlist_songs, + playlists, song_artists, song_dislikes, song_history,