-- name: InsertContextualLike :exec INSERT INTO contextual_likes (user_id, track_id, session_vector, session_id) VALUES ($1, $2, $3, $4); -- name: SoftDeleteContextualLikesForUserTrack :exec -- Marks the user's active rows for the song as deleted: every copy sharing -- the track's song_key, as UnlikeTrack removes the like from every copy. -- The context was captured on whichever copy was playing, which needn't be -- the one the unlike names. Idempotent — already-deleted rows aren't -- re-touched. UPDATE contextual_likes SET deleted_at = now() WHERE user_id = sqlc.arg(user_id)::uuid AND deleted_at IS NULL AND (track_id = sqlc.arg(track_id)::uuid OR track_id IN (SELECT t.id FROM tracks t WHERE t.song_key = (SELECT s.song_key FROM tracks s WHERE s.id = sqlc.arg(track_id)::uuid))); -- name: ListActiveContextualLikesForUser :many -- Returns all the user's active (non-soft-deleted) contextual_likes with -- non-null vectors. Cardinality is bounded by the user's actual like-while- -- playing history — typically tens to low hundreds. Used by the engine to -- compute contextual_match_score for the candidate pool. -- -- Only rows whose song is still liked count, so a context left active by -- an unlike that missed it can't keep boosting a song the user let go of. SELECT cl.track_id, cl.session_vector FROM contextual_likes cl WHERE cl.user_id = $1 AND cl.deleted_at IS NULL AND cl.session_vector IS NOT NULL AND EXISTS (SELECT 1 FROM general_likes g WHERE g.user_id = cl.user_id AND g.track_id = cl.track_id);