Skip to content

Instantly share code, notes, and snippets.

@rmcauley
Created July 29, 2013 18:03
Show Gist options
  • Save rmcauley/6106285 to your computer and use it in GitHub Desktop.
Save rmcauley/6106285 to your computer and use it in GitHub Desktop.
only_faved = db.c.fetch_list("SELECT user_id FROM r4_album_ratings WHERE album_id = %s, album_rating_user IS NULL AND album_fave = TRUE", (self.id,))
# TODO: Carry over old_faved using INSERTs
db.c.update("DELETE FROM r4_album_ratings WHERE album_id = %s album_rating_user IS NULL AND album_fave = TRUE", (self.id,))
db.c.update(
"WITH old_fave AS ( "
"DELETE FROM r4_album_ratings "
"WHERE album_id = %s AND album_fave = TRUE AND album_rating_user IS NOT NULL "
"RETURNING * "
") "
"INSERT INTO r4_album_ratings (album_id, user_id, album_fave, album_rating_complete, album_rating_user) "
"SELECT MIN(%s) AS album_id, r4_album_ratings.user_id AS user_id, BOOL_OR(old_fave.album_fave) AS album_fave, "
"FALSE AS album_rating_complete, ROUND(CAST(AVG(song_rating_user) AS NUMERIC), 1) AS album_rating_user "
"FROM r4_song_album "
" LEFT JOIN r4_song_ratings USING (song_id) "
" LEFT JOIN old_fave ON (r4_song_ratings.user_id = old_fave.user_id) "
"WHERE r4_song_album.album_id = %s "
"GROUP BY r4_album_ratings.user_id ",
(self.id,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment