kovyrin/db-charmer — Issue #37: find_each issue
Author: mpabst
Created: 2012-02-21 21:16:04+00:00
State: open

We wrote the following database update script:

Petition.on_db(:slave0).find_each do |petition|
puts "#{Time.now} Fixing total_signature_count for petition ID ##{petition.id}"
petition.update_attribute(:total_signature_count, petition.on_db(:slave0).restricted_signatures.count)
sleep 1
end

When we actually ran it, though, it tried to send the update_attribute calls to the slave database and we got an error that the database connection was read-only.

We were able to work around this issue in few ways.

First the problem went away if we changed the first line to:
Petition.on_db(:slave0).limit(5).find_each do |petition|

This also led to the "limit(5)" being ignored.

Second, the problem went away if we changed the first line to:
Petition.find_each do |petition|

This was what we ended up using, but it does seem to increase load on master.