Commit 09d3e48
fix(tracks): match ISRC ignoring dashes and case, add functional index (#832)
## Summary
ISRC lookup via `GET /v1/tracks?isrc=…` failed when the stored value and
the query disagreed on dash placement. Partner report (Thomas):
Fixed purely on the search/query side (no normalization at write time):
- Normalize the incoming `isrc` query param in Go: uppercase + strip
non-alphanumeric characters before passing to the DB.
- In the SQL, compare against `regexp_replace(upper(isrc), '[^A-Z0-9]',
'', 'g')` so the stored value is normalized at compare time.
Net result: any combination of dashes/spaces/casing in either the
request or the stored row matches. `US-ANG-21-03742`, `USANG2103742`,
`usang2103742`, and `US-ANG2103742` all resolve to the same row.
## Index
Added `tracks_isrc_normalized_idx`, a **partial functional index**
matching the comparison expression so the new query can index-scan
instead of seq-scanning all of `tracks`:
```sql
create index concurrently if not exists tracks_isrc_normalized_idx
on public.tracks ((regexp_replace(upper(isrc), '[^A-Z0-9]'::text, ''::text, 'g')))
where isrc is not null;
```
Verified planner picks it on a 50k-row scratch table:
```
Bitmap Heap Scan on scratch_tracks (cost=16.43..248.18 rows=500 width=4)
Recheck Cond: (regexp_replace(upper(isrc), '[^A-Z0-9]'::text, ''::text, 'g'::text) = ANY (...))
-> Bitmap Index Scan on scratch_tracks_isrc_norm_idx (cost=0.00..16.30 rows=500 width=0)
Index Cond: (regexp_replace(upper(isrc), '[^A-Z0-9]'::text, ''::text, 'g'::text) = ANY (...))
```
Index DDL uses `CREATE INDEX CONCURRENTLY` (no `BEGIN/COMMIT` wrapper)
so the build does not take an `ACCESS EXCLUSIVE` lock on `tracks`. `IF
NOT EXISTS` keeps the migration idempotent. Partial on `isrc IS NOT
NULL` since most rows have no ISRC.
Heads-up given #830: that PR moved `0201` away from `CONCURRENTLY`
because the legacy Python Celery task on `challenge_disbursements` was
keeping ~3-minute transactions open continuously, blocking the build's
`virtualxid` wait indefinitely. `tracks` is written by the Go indexer
and isn't subject to that long-transaction pattern, so `CONCURRENTLY`
should complete normally here. If a stuck build needs to be aborted,
drop the invalid index with `DROP INDEX IF EXISTS
tracks_isrc_normalized_idx;` and re-run.
## ISWC
There is no `iswc` query-param lookup today (column exists and is
exposed in API responses, but no handler or sqlc query reads it).
Nothing to mirror — left as-is.
## Test plan
- [x] `TestGetTracksByISRC` covers: stored-with-dashes queried undashed,
queried with same dashes, queried lowercased; stored-without-dashes
queried as-is and queried with inserted dashes.
- [x] Existing tracks tests (`TestTracksEndpoint`,
`TestGetTracksByPermalink`, `TestGetTracksExcludesAccessAuthorities`,
`Test200UnAuthed`) still pass.
- [x] Migration applies cleanly and is idempotent (re-running prints
`relation \"tracks_isrc_normalized_idx\" already exists, skipping`).
- [x] `EXPLAIN` on a seeded scratch table shows `Bitmap Index Scan on
tracks_isrc_normalized_idx`.
- [ ] After deploy: verify `SELECT indexrelid::regclass, indisvalid FROM
pg_index WHERE indexrelid::regclass::text =
'tracks_isrc_normalized_idx';` returns `indisvalid = true`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 6190f64 commit 09d3e48
5 files changed
Lines changed: 84 additions & 3 deletions
File tree
- api
- dbv1
- queries
- ddl/migrations
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | | - | |
| 7 | + | |
5 | 8 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
11 | 20 | | |
12 | 21 | | |
13 | 22 | | |
| |||
41 | 50 | | |
42 | 51 | | |
43 | 52 | | |
44 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
45 | 58 | | |
46 | 59 | | |
47 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
39 | 81 | | |
40 | 82 | | |
41 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
0 commit comments