Skip to content

Commit

Permalink
feat(sql): junction tables & better sql.
Browse files Browse the repository at this point in the history
IDK I did a lot... meh
  • Loading branch information
5-pebbles committed Mar 16, 2024
1 parent 5345175 commit 29d37e8
Show file tree
Hide file tree
Showing 37 changed files with 351 additions and 247 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions migrations/U10__tokens.sql

This file was deleted.

1 change: 0 additions & 1 deletion migrations/U5__users.sql → migrations/U10__users.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY
, permissions TEXT NOT NULL DEFAULT ''
, hash TEXT NOT NULL
)
7 changes: 7 additions & 0 deletions migrations/U11__user_permissions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS user_permissions (id TEXT NOT NULL
, username TEXT NOT NULL
, PRIMARY KEY (id, username)
, FOREIGN KEY (id) REFERENCES permissions(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE ON UPDATE CASCADE
);

4 changes: 4 additions & 0 deletions migrations/U15__tokens.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS tokens (id TEXT PRIMARY KEY
, username TEXT NOT NULL
, FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE ON UPDATE CASCADE
)
1 change: 1 addition & 0 deletions migrations/U1__pragma.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PRAGMA foreign_keys = ON
File renamed without changes.
7 changes: 7 additions & 0 deletions migrations/U21__invite_permissions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS invite_permissions (id TEXT NOT NULL
, code TEXT NOT NULL
, PRIMARY KEY (id, code)
, FOREIGN KEY (id) REFERENCES permissions(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (code) REFERENCES invites(code) ON DELETE CASCADE ON UPDATE CASCADE
);

File renamed without changes.
6 changes: 0 additions & 6 deletions migrations/U26__artist_genres.sql

This file was deleted.

File renamed without changes.
6 changes: 0 additions & 6 deletions migrations/U31__artist_albums.sql

This file was deleted.

6 changes: 6 additions & 0 deletions migrations/U31__artist_genres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS artist_genres (artist_id TEXT NOT NULL
, genre_id TEXT NOT NULL
, PRIMARY KEY (artist_id, genre_id)
, FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (genre_id) REFERENCES genres(id) ON DELETE CASCADE ON UPDATE CASCADE
);
6 changes: 0 additions & 6 deletions migrations/U32__album_genres.sql

This file was deleted.

File renamed without changes.
6 changes: 0 additions & 6 deletions migrations/U36__album_tracks.sql

This file was deleted.

6 changes: 6 additions & 0 deletions migrations/U36__artist_albums.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS artist_albums (album_id TEXT NOT NULL
, artist_id TEXT NOT NULL
, PRIMARY KEY (album_id, artist_id)
, FOREIGN KEY (album_id) REFERENCES albums(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE ON UPDATE CASCADE
);
6 changes: 6 additions & 0 deletions migrations/U37__album_genres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS album_genres (album_id TEXT NOT NULL
, genre_id TEXT NOT NULL
, PRIMARY KEY (album_id, genre_id)
, FOREIGN KEY (album_id) REFERENCES albums(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (genre_id) REFERENCES genres(id) ON DELETE CASCADE ON UPDATE CASCADE
);
6 changes: 0 additions & 6 deletions migrations/U37__track_genres.sql

This file was deleted.

File renamed without changes.
6 changes: 6 additions & 0 deletions migrations/U41__album_tracks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS album_tracks (track_id TEXT NOT NULL
, album_id TEXT NOT NULL
, PRIMARY KEY (track_id, album_id)
, FOREIGN KEY (track_id) REFERENCES tracks(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (album_id) REFERENCES albums(id) ON DELETE CASCADE ON UPDATE CASCADE
);
6 changes: 6 additions & 0 deletions migrations/U42__track_genres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS track_genres (track_id TEXT NOT NULL
, genre_id TEXT NOT NULL
, PRIMARY KEY (track_id, genre_id)
, FOREIGN KEY (track_id) REFERENCES tracks(id) ON DELETE CASCADE ON UPDATE CASCADE
, FOREIGN KEY (genre_id) REFERENCES genres(id) ON DELETE CASCADE ON UPDATE CASCADE
);
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use strum::IntoEnumIterator;

pub fn migration() -> String {
format!(
"INSERT OR IGNORE INTO permissions (id) VALUES {};",
"INSERT OR IGNORE INTO permissions (id) VALUES ('{}');",
Permission::iter()
.map(|p| format!("\n ('{}')", p.to_string()))
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.join("'), ('")
)
}
5 changes: 2 additions & 3 deletions src/api/music/albums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ async fn album_delete(db: Database, user: User, id: String) -> Result<()> {
}

db.run(move |conn| -> Result<()> {
conn.execute_batch("PRAGMA foreign_keys = ON;")?;

let tx = conn.transaction()?;

if let Err(QueryReturnedNoRows) =
Expand All @@ -169,9 +171,6 @@ async fn album_delete(db: Database, user: User, id: String) -> Result<()> {
}

tx.execute("DELETE FROM albums WHERE id = ?", params![id])?;
tx.execute("DELETE FROM album_genres WHERE album_id = ?", params![id])?;
tx.execute("DELETE FROM artist_albums WHERE album_id = ?", params![id])?;
tx.execute("DELETE FROM album_tracks WHERE album_id = ?", params![id])?;

tx.commit()?;

Expand Down
14 changes: 4 additions & 10 deletions src/api/music/artists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ use rocket_sync_db_pools::rusqlite::{params, Error::QueryReturnedNoRows, ToSql};

use crate::{
api::errors::ApiError,
database::{
artists::Artist, database::Database, permissions::Permission, users::User,
},
database::{artists::Artist, database::Database, permissions::Permission, users::User},
};

type Result<T> = std::result::Result<T, ApiError>;

#[post("/artist", data = "<artist>")]
async fn artist_write(
db: Database,
user: User,
artist: Json<Artist>,
) -> Result<Json<Artist>> {
async fn artist_write(db: Database, user: User, artist: Json<Artist>) -> Result<Json<Artist>> {
if !user.permissions.contains(&Permission::ArtistWrite) {
Err(Status::Forbidden)?
}
Expand Down Expand Up @@ -122,6 +116,8 @@ async fn artist_delete(db: Database, user: User, id: String) -> Result<()> {
}

db.run(move |conn| -> Result<()> {
conn.execute_batch("PRAGMA foreign_keys = ON;")?;

let tx = conn.transaction()?;

if let Err(QueryReturnedNoRows) = tx.query_row(
Expand All @@ -133,8 +129,6 @@ async fn artist_delete(db: Database, user: User, id: String) -> Result<()> {
}

tx.execute("DELETE FROM artists WHERE id = ?", params![id])?;
tx.execute("DELETE FROM artist_genres WHERE artist_id = ?", params![id])?;
tx.execute("DELETE FROM artist_albums WHERE artist_id = ?", params![id])?;

tx.commit()?;

Expand Down
14 changes: 2 additions & 12 deletions src/api/music/genres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ async fn genre_delete(db: Database, user: User, genre: String) -> Result<()> {
}

db.run(move |conn| -> Result<()> {
conn.execute_batch("PRAGMA foreign_keys = ON;")?;

let tx = conn.transaction()?;

if let Err(QueryReturnedNoRows) =
Expand All @@ -73,18 +75,6 @@ async fn genre_delete(db: Database, user: User, genre: String) -> Result<()> {
}

tx.execute("DELETE FROM genres WHERE id = ?", params![genre])?;
tx.execute(
"DELETE FROM artist_genres WHERE genre_id = ?",
params![genre],
)?;
tx.execute(
"DELETE FROM album_genres WHERE genre_id = ?",
params![genre],
)?;
tx.execute(
"DELETE FROM track_genres WHERE genre_id = ?",
params![genre],
)?;

tx.commit()?;

Expand Down
4 changes: 2 additions & 2 deletions src/api/music/tracks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ async fn track_delete(db: Database, user: User, id: String) -> Result<()> {
}

db.run(move |conn| -> Result<()> {
conn.execute_batch("PRAGMA foreign_keys = ON;")?;

let tx = conn.transaction()?;

if let Err(QueryReturnedNoRows) =
Expand All @@ -179,8 +181,6 @@ async fn track_delete(db: Database, user: User, id: String) -> Result<()> {
}

tx.execute("DELETE FROM tracks WHERE id = ?", params![id])?;
tx.execute("DELETE FROM track_genres WHERE track_id = ?", params![id])?;
tx.execute("DELETE FROM album_tracks WHERE track_id = ?", params![id])?;

tx.commit()?;

Expand Down
Loading

0 comments on commit 29d37e8

Please sign in to comment.