Skip to content

Instantly share code, notes, and snippets.

@marcusleemitchell
Last active December 14, 2015 04:48
Show Gist options
  • Save marcusleemitchell/5030510 to your computer and use it in GitHub Desktop.
Save marcusleemitchell/5030510 to your computer and use it in GitHub Desktop.
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
@mezis
Copy link

mezis commented Feb 25, 2013

You can do the count with connection.select_value instead.

@mezis
Copy link

mezis commented Feb 25, 2013

Also, you don't need to store locked_at and locked_by.

@mezis
Copy link

mezis commented Feb 25, 2013

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