Use addedAt for playlist song ordering instead of id#105
Conversation
…ctness Co-authored-by: Krosebrook <214532761+Krosebrook@users.noreply.github.com>
addedAt for playlist song ordering instead of id
ⓘ You are approaching your monthly quota for Qodo. Upgrade your plan Review Summary by QodoOrder playlist songs by addedAt timestamp for semantic correctness
WalkthroughsDescription• Order playlist songs by addedAt timestamp instead of auto-increment id • Ensures semantic correctness and consistency with getLikedSongs ordering • Fixes fragile ordering that relied on insertion order rather than explicit timestamp Diagramflowchart LR
A["getPlaylistWithSongs<br/>method"] -->|"previously ordered by"| B["playlistSongs.id<br/>auto-increment PK"]
A -->|"now ordered by"| C["playlistSongs.addedAt<br/>semantic timestamp"]
C -->|"consistent with"| D["getLikedSongs<br/>ordering pattern"]
File Changes1. server/storage.ts
|
Code Review by Qodo
1. Non-deterministic song ordering
|
| .innerJoin(playlistSongs, eq(songs.id, playlistSongs.songId)) | ||
| .where(eq(playlistSongs.playlistId, id)) | ||
| .orderBy(playlistSongs.id); | ||
| .orderBy(playlistSongs.addedAt); |
There was a problem hiding this comment.
1. Non-deterministic song ordering 🐞 Bug ✓ Correctness
Ordering only by playlistSongs.addedAt can produce inconsistent song order when multiple rows share the same timestamp (common with limited timestamp precision) or when addedAt is NULL. This can cause playlists to appear to “shuffle” between requests even when data hasn’t changed.
Agent Prompt
### Issue description
`getPlaylistWithSongs` orders songs only by `playlistSongs.addedAt`. Since `addedAt` is nullable and not guaranteed unique, playlists can return songs in a non-deterministic order (ties/NULLs), causing inconsistent UI ordering.
### Issue Context
- `playlistSongs.addedAt` is defined without `.notNull()`, so NULLs are possible.
- A similar pattern (`getLikedSongs`) explicitly uses `.nullsLast()` when ordering by a timestamp.
### Fix Focus Areas
- server/storage.ts[159-169]
- shared/schema.ts[70-75]
### Suggested change
- Import `asc` from `drizzle-orm`.
- Update the query to use deterministic ordering, e.g.:
- `.orderBy(asc(playlistSongs.addedAt).nullsLast(), asc(playlistSongs.id))`
- (Optionally) if you want to preserve older behavior for NULL timestamps, consider ordering by `id` when `addedAt` is NULL via `nullsLast()` plus the `id` tiebreaker.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
There was a problem hiding this comment.
Pull request overview
Updates playlist song ordering to use the semantic addedAt timestamp (instead of the auto-increment id) so playlist tracks return in the order they were added.
Changes:
- Change
getPlaylistWithSongsordering fromplaylistSongs.idtoplaylistSongs.addedAt.
| .innerJoin(playlistSongs, eq(songs.id, playlistSongs.songId)) | ||
| .where(eq(playlistSongs.playlistId, id)) | ||
| .orderBy(playlistSongs.id); | ||
| .orderBy(playlistSongs.addedAt); |
There was a problem hiding this comment.
playlistSongs.addedAt is nullable in the schema (timestamp().defaultNow() without .notNull()), so ordering by it can put legacy/null rows first and yield unexpected playlist ordering. Consider ordering with null handling (e.g., nulls last) and adding a deterministic tiebreaker (e.g., then by playlistSongs.id), or making addedAt non-nullable + backfilling via migration.
getPlaylistWithSongswas ordering byplaylistSongs.id(auto-increment PK) rather than the semantic timestamp field. While typically equivalent,idordering is semantically incorrect and fragile.Changes
server/storage.ts: SwitchorderBy(playlistSongs.id)→orderBy(playlistSongs.addedAt), consistent withgetLikedSongswhich orders bysongLikes.createdAt🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.
Summary by cubic
Refactored getPlaylistWithSongs to use innerJoin and sort songs by addedAt for correct playlist order. The method now returns only playlists with valid songs, reducing null checks and improving query semantics.
Refactors
Migration
Written for commit 53b8e78. Summary will update on new commits.