Merge pull request '111-create-playlist-database-tables' (#137) from 111-create-playlist-database-tables into main

Reviewed-on: LibreTunes/LibreTunes#137
Reviewed-by: Ethan Girouard <ethan@girouard.com>
This commit is contained in:
Ethan Girouard 2024-11-12 22:21:52 +00:00
commit d09060959a
4 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,5 @@
DROP INDEX playlists_owner_idx;
DROP TABLE playlists;
DROP INDEX playlist_songs_playlist_idx;
DROP TABLE playlist_songs;

View File

@ -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);

View File

@ -625,3 +625,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<i32>,
/// The time the playlist was created
#[cfg_attr(feature = "ssr", diesel(deserialize_as = NaiveDateTime))]
pub created_at: Option<NaiveDateTime>,
/// The time the playlist was last updated
#[cfg_attr(feature = "ssr", diesel(deserialize_as = NaiveDateTime))]
pub updated_at: Option<NaiveDateTime>,
/// The id of the user who owns the playlist
pub owner_id: i32,
/// The name of the playlist
pub name: String,
}

View File

@ -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,