Skip to content

Skip re-fetching tracks, albums, and artists that are already fresh in the DB - #194

Closed
jbrown1618 with Copilot wants to merge 2 commits into
mainfrom
copilot/optimize-data-fetching
Closed

Skip re-fetching tracks, albums, and artists that are already fresh in the DB#194
jbrown1618 with Copilot wants to merge 2 commits into
mainfrom
copilot/optimize-data-fetching

Conversation

Copilot AI commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Every Spotify sync re-fetched full details for all tracks, albums, and artists regardless of whether that data already existed. This adds a last_updated timestamp to those three tables and skips Spotify API calls for records updated within the last 7 days.

Schema

  • Added last_updated TIMESTAMP DEFAULT current_timestamp to track, album, and artist tables
  • New migration v016_add_last_updated adds the columns to existing DBs; existing rows get current_timestamp as their initial value, so they're treated as fresh for the first week post-migration

Fetch logic (save_spotify_data.py)

  • load_fresh_uris() queries the DB at sync start and populates fresh_tracks, fresh_albums, fresh_artists sets
  • process_track() skips adding stale track data to tracks_data/track_artist for fresh tracks, but still queues their albums and artists for independent freshness evaluation
  • queue_album() / queue_artist() short-circuit if the URI is in the fresh set — no API call is made for them
  • track_data(), album_data(), artist_data() now include last_updated = datetime.now(timezone.utc) so the timestamp is refreshed on every upsert
def queue_album(album):
    if album["uri"] in queued_albums:
        return
    if album["uri"] in fresh_albums:   # skip if updated within 7 days
        return
    queued_albums.add(album["uri"])

The staleness window is controlled by staleness_days = 7.

Original prompt

This section details on the original issue you should resolve

<issue_title>Only fetch data we don't already have</issue_title>
<issue_description>Right now, when we pull fresh data from Spotify, we fetch all data for tracks, albums, and artists in the set of liked and playlisted tracks. We could make this more efficient by not fetching details for items that are already in the db.

The only reason to update a record that already exists would be to get a fresh profile picture for an artist. We also could get fresh popularity metrics, but we barely use those in the UI and should maybe just remove them.

Ideally we should keep a last-updated timestamp for tracks, artists, and albums, and update if the data is more than a week stale.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…resh data

Co-authored-by: jbrown1618 <9657751+jbrown1618@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize data fetching for tracks, albums, and artists Skip re-fetching tracks, albums, and artists that are already fresh in the DB Feb 25, 2026
@jbrown1618 jbrown1618 closed this Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Only fetch data we don't already have

2 participants