lib/materialist/materializer.rb in materialist-0.0.2 vs lib/materialist/materializer.rb in materialist-0.0.3

- old
+ new

@@ -74,13 +74,22 @@ @url = url @instance = klass.new @options = klass.materialist_options end - def upsert + def upsert(retry_on_race_condition: true) upsert_record.tap do |entity| instance.send(after_upsert, entity) if after_upsert end + rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid + # when there is a race condition and uniqueness of :source_url + # is enforced by database index, this error is raised + # so we simply try upsert again + # if error is due to another type of uniqueness constraint + # second call will also fail and error would bubble up + retry_on_race_condition ? + upsert(retry_on_race_condition: false) : + raise end def destroy model_class.find_by(source_url: url).tap do |entity| entity.destroy!.tap do |entity|