-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-acquire-missing-artists-fields.exs
35 lines (29 loc) · 1.19 KB
/
2-acquire-missing-artists-fields.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use CouchNormalizer.Scenario
# Sets/Updates missing `artist_uri` and `artist` fields for `album` and `track` documents
# through special 'queued' approach.
CouchNormalizer.Registry.acquire "2-acquire-missing-artists-fields", fn(_db, doc_id, _rev, body) ->
if (body["type"] == "album" || body["type"] == "track") do
# contains 'ids' and 'fields' which will be updated
# we should use it as a `:cached!` because this is a fat one.
name_and_id_doc = doc("changes-queue", "tracks_albums_artist_name_and_id", :cached!)
# checks if `name_and_id_doc` has records for current `doc_id`
name_and_id_field = name_and_id_doc[doc_id]
if name_and_id_field != nil do
[artist_name, artist_id] = name_and_id_field
artist_uri = "medianet:artist:#{artist_id}"
# creates or updates a missing `artist_uri` field
if body["artist_uri"] == nil do
create_field :artist_uri, artist_uri
else
update_field :artist_uri, artist_uri
end
# creates or updates a missing `artist` field
if body["artist"] == nil do
create_field :artist, artist_name
else
update_field :artist, artist_name
end
{:update, body}
end
end
end