Last active
December 14, 2015 04:48
-
-
Save marcusleemitchell/5030510 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REV[1] | |
require 'json/ext' | |
batch_size = 1000 | |
fields = ["id", "priority", "attempts", "handler", "last_error", "run_at", "locked_at", "failed_at", "locked_by", "created_at", "updated_at"] | |
ActiveRecord::Base.connection.execute("SELECT COUNT(*) FROM delayed_jobs;").each { |x| @total_rows = x[0] } | |
File.open("tmp/delayed_jobs.json","w") do |f| | |
((@total_rows / batch_size)+1).times do |i| | |
ActiveRecord::Base.connection.execute("SELECT * FROM delayed_jobs LIMIT #{batch_size} OFFSET #{(1000 * (i+1)}").each do |row| | |
hsh = {} | |
fields.each_with_index do |field, index| | |
hsh.merge!(field.to_sym => @row[index]) | |
end | |
f.write(JSON.generate(hsh)) | |
end | |
end | |
end | |
REV[2] | |
require 'json/ext' | |
batch_size = 1000 | |
fields = ["id", "priority", "attempts", "handler", "last_error", "run_at", "failed_at", "created_at", "updated_at"] | |
total_rows = ActiveRecord::Base.connection.select_value("SELECT COUNT(*) FROM delayed_jobs;") | |
File.open("tmp/delayed_jobs.json","w") do |f| | |
((total_rows / batch_size)+1).times do |i| | |
ActiveRecord::Base.connection.select_rows("SELECT #{fields.join(',')} FROM delayed_jobs LIMIT #{batch_size} OFFSET #{(1000 * (i+1)}").each do |row| | |
hsh = {} | |
fields.each_with_index do |field, index| | |
hsh.merge!(field.to_sym => @row[index]) | |
end | |
f.write(JSON.generate(hsh)) | |
end | |
end | |
end |
Also, you don't need to store locked_at
and locked_by
.
For the second execute
, I think you should connection.select_rows
instead.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do the count with
connection.select_value
instead.