Add function to backfill missing data in scrobbled tracks

This commit is contained in:
Claudio Ortolina
2025-06-19 17:38:51 +03:00
parent 25634f5ee7
commit 35e060613b
2 changed files with 46 additions and 0 deletions
+10
View File
@@ -31,6 +31,16 @@ defmodule MusicLibrary.Artists do
end
end
def name_id_pairs(names) do
q =
from ar in ArtistRecord,
distinct: true,
where: fragment("artist ->> '$.name'") in ^names,
select: {fragment("artist ->> '$.name'"), ar.musicbrainz_id}
Repo.all(q)
end
def get_all_artist_ids do
q = from ar in ArtistRecord, distinct: true, select: ar.musicbrainz_id
@@ -0,0 +1,36 @@
defmodule MusicLibrary.ScrobbleActivity.Backfill do
alias MusicLibrary.Artists
alias MusicLibrary.Repo
@allowed_artists [
"IQ",
"Riverside",
"Airbag",
"Arena",
"Fish",
"Marillion",
"Porcupine Tree",
"Steven Wilson",
"Gazpacho",
"Sylvan",
"Dream Theater",
"Pink Floyd",
"Muse",
"Opeth"
]
def fill_missing_artist_ids do
name_id_pairs = Artists.name_id_pairs(@allowed_artists)
Enum.each(name_id_pairs, fn {name, id} ->
Repo.query(
"""
UPDATE scrobbled_tracks
SET artist = json_set(artist, '$.musicbrainz_id', ?)
WHERE artist ->> '$.name' == ?;
""",
[id, name]
)
end)
end
end