Skip to content

Commit d7cd43a

Browse files
committed
rewrite for clarity and AR 5.1 compat
In AR 5.1 find_or_create_by and new_record_before_save? interact differently, to break the original code. (I think maybe it was relying on a bug in new_record_before_save?, I don't really understand what new_record_before_save? is). I believe this is the same logic, rewritten more clearly and in a way that will work in Rails 5.1. Note that find_or_create_by is NOT atomic, in any Rails version. So we should not have introduced any race condition that wasn't there already, we're still creating by soon after find if not present, just a slight intervening check for token.last == 0, shoudn't be much racier than before.
1 parent 16cafe1 commit d7cd43a

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/oai/provider/model/activerecord_caching_wrapper.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,16 @@ def next_set(token_string)
9090
# select a subset of the result set, and return it with a
9191
# resumption token to get the next subset
9292
def select_partial(token)
93-
if 0 == token.last
94-
oaitoken = OaiToken.find_or_create_by(token: token.to_s)
95-
if oaitoken.new_record_before_save?
96-
OaiToken.connection.execute("insert into " +
97-
"#{OaiEntry.table_name} (oai_token_id, record_id) " +
98-
"select #{oaitoken.id}, id from #{model.table_name} where " +
99-
"#{OaiToken.sanitize_sql(token_conditions(token))}")
100-
end
93+
oaitoken = OaiToken.find_by(token: token.to_s)
94+
95+
if 0 == token.last && oaitoken.nil?
96+
oaitoken = OaiToken.create!(token: token.to_s)
97+
OaiToken.connection.execute("insert into " +
98+
"#{OaiEntry.table_name} (oai_token_id, record_id) " +
99+
"select #{oaitoken.id}, id from #{model.table_name} where " +
100+
"#{OaiToken.sanitize_sql(token_conditions(token))}")
101101
end
102102

103-
oaitoken = OaiToken.find_by_token(token.to_s)
104103
raise ResumptionTokenException.new unless oaitoken
105104

106105
PartialResult.new(

0 commit comments

Comments
 (0)