From c3ed147004bca0391f2a173bee25e88d416dbdf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Thu, 12 Aug 2021 15:37:16 -1000 Subject: [PATCH] Fix queries on the views with ratings The old behaviour was a simple join, if there's no rating for the movie, there's no result for movie. The backend can not return the movie and does a new GetDetails from online detailers. --- migrations/0010_fix_rating_tables.down.sql | 20 ++++++++++++++++++++ migrations/0010_fix_rating_tables.up.sql | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 migrations/0010_fix_rating_tables.down.sql create mode 100644 migrations/0010_fix_rating_tables.up.sql diff --git a/migrations/0010_fix_rating_tables.down.sql b/migrations/0010_fix_rating_tables.down.sql new file mode 100644 index 0000000..d2838f3 --- /dev/null +++ b/migrations/0010_fix_rating_tables.down.sql @@ -0,0 +1,20 @@ +CREATE OR REPLACE VIEW movies_with_rating AS +SELECT + m.id, m.imdb_id, m.title, m.plot, m.tmdb_id, m.year, m.original_title, m.runtime, m.sort_title, m.tagline, m.genres, + r.rating, + r.votes, + m.updated_at, m.created_at +FROM movies m +JOIN imdb_ratings r +ON m.imdb_id = r.imdb_id; + +CREATE OR REPLACE VIEW shows_with_rating AS +SELECT + s.id, s.imdb_id, s.title, s.plot, s.tvdb_id, s.year, s.first_aired, + r.rating, + r.votes, + s.updated_at, s.created_at +FROM shows s +JOIN imdb_ratings r +ON s.imdb_id = r.imdb_id; + diff --git a/migrations/0010_fix_rating_tables.up.sql b/migrations/0010_fix_rating_tables.up.sql new file mode 100644 index 0000000..18de066 --- /dev/null +++ b/migrations/0010_fix_rating_tables.up.sql @@ -0,0 +1,20 @@ +CREATE OR REPLACE VIEW movies_with_rating AS +SELECT + m.id, m.imdb_id, m.title, m.plot, m.tmdb_id, m.year, m.original_title, m.runtime, m.sort_title, m.tagline, m.genres, + CASE WHEN r.rating IS NULL THEN m.rating ELSE r.rating END, + CASE WHEN r.votes IS NULL THEN m.votes ELSE r.votes END, + m.updated_at, m.created_at +FROM movies m +LEFT JOIN imdb_ratings r +ON m.imdb_id = r.imdb_id; + +CREATE OR REPLACE VIEW shows_with_rating AS +SELECT + s.id, s.imdb_id, s.title, s.plot, s.tvdb_id, s.year, s.first_aired, + CASE WHEN r.rating IS NULL THEN s.rating ELSE r.rating END, + CASE WHEN r.votes IS NULL THEN 0 ELSE r.votes END, + s.updated_at, s.created_at +FROM shows s +LEFT JOIN imdb_ratings r +ON s.imdb_id = r.imdb_id; +