Sha256: 7f94dc7c3f7599de43c40d1bcd29ddf88b84b4aee66345405a0b67ea869e0f4a

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Mongoid
  module Ids
    module Collisions
      def resolve_token_collisions(resolver)
        retries = resolver.retry_count
        begin
          yield
        rescue Moped::Errors::OperationFailure => e
          if is_duplicate_token_error?(e, self, resolver.field_name)
            if (retries -= 1) >= 0
              resolver.create_new_token_for(self)
              retry
            end
            raise_collision_retries_exceeded_error resolver.field_name, resolver.retry_count
          else
            raise e
          end
        end
      end

      def raise_collision_retries_exceeded_error(field_name, retry_count)
        Rails.logger.warn "[Mongoid::Ids] Warning: Maximum token generation retries (#{retry_count}) exceeded on `#{field_name}'." if defined?(Rails)
        raise Mongoid::Ids::CollisionRetriesExceeded.new(self, retry_count)
      end

      def is_duplicate_token_error?(err, document, field_name)
        [11000, 11001].include?(err.details['code']) &&
          err.details['err'] =~ /dup key/ &&
          err.details['err'] =~ /"#{document.send(field_name)}"/
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-ids-0.1.8 lib/mongoid/ids/collisions.rb
mongoid-ids-0.1.7 lib/mongoid/ids/collisions.rb
mongoid-ids-0.1.1 lib/mongoid/ids/collisions.rb