From 81b1490cce6cd2c05fe7f3459e76a288f0a4ad38 Mon Sep 17 00:00:00 2001 From: Carter Bertolini Date: Fri, 1 Nov 2024 16:37:22 -0400 Subject: [PATCH 1/3] 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, From 0335e3d255cb2c573d1be5364507ac95ba07956c Mon Sep 17 00:00:00 2001 From: Carter Bertolini Date: Fri, 1 Nov 2024 16:48:56 -0400 Subject: [PATCH 2/3] Created model --- src/models.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/models.rs b/src/models.rs index b96c8d2..fb96465 100644 --- a/src/models.rs +++ b/src/models.rs @@ -626,3 +626,24 @@ pub struct HistoryEntry { /// The id of the song that was listened to pub song_id: i32, } + +/// Model for a playlist +#[cfg_attr(feature = "ssr", derive(Queryable, Selectable, Insertable))] +#[cfg_attr(feature = "ssr", diesel(table_name = crate::schema::playlists))] +#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))] +#[derive(Serialize, Deserialize)] +pub struct Playlist { + /// A unique id for the playlist + #[cfg_attr(feature = "ssr", diesel(deserialize_as = i32))] + pub id: Option, + /// The time the playlist was created + #[cfg_attr(feature = "ssr", diesel(deserialize_as = SystemTime))] + pub created_at: Option, + /// The time the playlist was last updated + #[cfg_attr(feature = "ssr", diesel(deserialize_as = SystemTime))] + pub updated_at: Option, + /// The id of the user who owns the playlist + pub owner_id: i32, + /// The name of the playlist + pub name: String, +} From 7f298f75ce908d20c628c62305a0fb55b3045cb0 Mon Sep 17 00:00:00 2001 From: Carter Bertolini Date: Tue, 12 Nov 2024 17:09:08 -0500 Subject: [PATCH 3/3] Change Playlist Model to use chronos time --- src/models.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models.rs b/src/models.rs index 3119eec..fd3e217 100644 --- a/src/models.rs +++ b/src/models.rs @@ -636,11 +636,11 @@ pub struct Playlist { #[cfg_attr(feature = "ssr", diesel(deserialize_as = i32))] pub id: Option, /// The time the playlist was created - #[cfg_attr(feature = "ssr", diesel(deserialize_as = SystemTime))] - pub created_at: Option, + #[cfg_attr(feature = "ssr", diesel(deserialize_as = NaiveDateTime))] + pub created_at: Option, /// The time the playlist was last updated - #[cfg_attr(feature = "ssr", diesel(deserialize_as = SystemTime))] - pub updated_at: Option, + #[cfg_attr(feature = "ssr", diesel(deserialize_as = NaiveDateTime))] + pub updated_at: Option, /// The id of the user who owns the playlist pub owner_id: i32, /// The name of the playlist