canape/migrations/0010_fix_rating_tables.up.sql
Grégoire Delattre 7832c407ee
All checks were successful
continuous-integration/drone/push Build is passing
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.
2021-08-12 15:43:22 -10:00

20 lines
732 B
SQL

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;