fix(db): enforce root media kinds
This commit is contained in:
@@ -14,6 +14,34 @@ CREATE TABLE series (
|
||||
|
||||
CREATE INDEX series_root ON series (root_id);
|
||||
|
||||
CREATE TRIGGER movies_require_movie_root
|
||||
BEFORE INSERT ON movies
|
||||
WHEN NOT EXISTS (SELECT 1 FROM roots WHERE id = NEW.root_id AND kind = 'movie')
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'movies require a movie root');
|
||||
END;
|
||||
|
||||
CREATE TRIGGER movies_require_movie_root_on_update
|
||||
BEFORE UPDATE OF root_id ON movies
|
||||
WHEN NOT EXISTS (SELECT 1 FROM roots WHERE id = NEW.root_id AND kind = 'movie')
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'movies require a movie root');
|
||||
END;
|
||||
|
||||
CREATE TRIGGER series_require_tv_root
|
||||
BEFORE INSERT ON series
|
||||
WHEN NOT EXISTS (SELECT 1 FROM roots WHERE id = NEW.root_id AND kind = 'tv')
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'series require a TV root');
|
||||
END;
|
||||
|
||||
CREATE TRIGGER series_require_tv_root_on_update
|
||||
BEFORE UPDATE OF root_id ON series
|
||||
WHEN NOT EXISTS (SELECT 1 FROM roots WHERE id = NEW.root_id AND kind = 'tv')
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'series require a TV root');
|
||||
END;
|
||||
|
||||
CREATE TABLE seasons (
|
||||
id INTEGER PRIMARY KEY,
|
||||
series_id INTEGER NOT NULL REFERENCES series (id) ON DELETE CASCADE,
|
||||
|
||||
@@ -304,4 +304,29 @@ mod tests {
|
||||
.expect("movie schema remains available");
|
||||
assert_eq!(movie_count, 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn aggregates_require_roots_of_their_own_kind() {
|
||||
let (_dir, db) = fresh().await;
|
||||
let movie_root: i64 =
|
||||
sqlx::query_scalar("SELECT id FROM roots WHERE kind = 'movie' LIMIT 1")
|
||||
.fetch_one(db.pool())
|
||||
.await
|
||||
.expect("movie root");
|
||||
let tv_root: i64 = sqlx::query_scalar("SELECT id FROM roots WHERE kind = 'tv' LIMIT 1")
|
||||
.fetch_one(db.pool())
|
||||
.await
|
||||
.expect("TV root");
|
||||
|
||||
sqlx::query("INSERT INTO movies (tmdb_id, title, root_id) VALUES (1, 'x', ?)")
|
||||
.bind(tv_root)
|
||||
.execute(db.pool())
|
||||
.await
|
||||
.expect_err("movie cannot use TV root");
|
||||
sqlx::query("INSERT INTO series (tmdb_id, title, root_id) VALUES (2, 'x', ?)")
|
||||
.bind(movie_root)
|
||||
.execute(db.pool())
|
||||
.await
|
||||
.expect_err("series cannot use movie root");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user