Skip to content

Commit

Permalink
avoid unique validation errors switch to upserting subjects
Browse files Browse the repository at this point in the history
  • Loading branch information
camallen committed May 20, 2022
1 parent 72c89cc commit bcb057c
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/services/import/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@ def initialize(zooniverse_subject_id, context)
end

def run
subject = ::Subject.find_or_create_by!(context_id: context.id, zooniverse_subject_id: zooniverse_subject_id)
subject = upsert_subject
SubjectBackfillerJob.perform_async(subject.id)
subject
end

private

def upsert_subject
# use upsert to avoid collisions for inserting rows
upsert_results = ::Subject.upsert_all(
[{
context_id: context.id,
zooniverse_subject_id: zooniverse_subject_id
}],
unique_by: %i[zooniverse_subject_id context_id],
update_only: [:context_id] # must have an update clause to get a result set
)
upserted_subject_id = upsert_results.to_a.first['id']
::Subject.find(upserted_subject_id)
end
end
end

0 comments on commit bcb057c

Please sign in to comment.