Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request mbleigh#809 from fearenales/master
Browse files Browse the repository at this point in the history
Due to database collisions, retry when finding or creating a tag
  • Loading branch information
seuros authored May 3, 2017
2 parents d9efdc6 + 44d807f commit f1f33a9
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,20 @@ def self.find_or_create_all_with_like_by_name(*list)

return [] if list.empty?

existing_tags = named_any(list)

list.map do |tag_name|
comparable_tag_name = comparable_name(tag_name)
existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
begin
tries ||= 3

existing_tags = named_any(list)
comparable_tag_name = comparable_name(tag_name)
existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
existing_tag || create(name: tag_name)
rescue ActiveRecord::RecordNotUnique
# Postgres aborts the current transaction with
# PG::InFailedSqlTransaction: ERROR: current transaction is aborted, commands ignored until end of transaction block
# so we have to rollback this transaction
if (tries -= 1).positive?
ActiveRecord::Base.connection.execute 'ROLLBACK'
retry
end

raise DuplicateTagError.new("'#{tag_name}' has already been taken")
end
end
Expand Down

0 comments on commit f1f33a9

Please sign in to comment.