Last active
August 29, 2015 14:19
-
-
Save IanVaughan/3e8868a498b5b92c1f14 to your computer and use it in GitHub Desktop.
Finds invalid rails records
This file contains 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
#!/usr/bin/env ruby | |
puts "Loading Rails..." | |
require 'ruby-progressbar' | |
require './config/environment' | |
require "csv" | |
puts "Booting Rails..." | |
Rails.application.eager_load! | |
puts "Getting Models..." | |
models = ActiveRecord::Base.descendants | |
puts "Analysing..." | |
CSV.open("results.csv", "w") do |csv| | |
models_with_index.each do |model, index| | |
bar = ProgressBar.create(title: "#{model} (#{index}/#{models.count})", total: model.count) | |
model.find_in_batches do |batch| | |
batch.each_with_index do |record, index| | |
bar.increment | |
if !record.valid? && !record.valid? | |
record.errors.messages.each do |message| | |
csv << [model.to_s, message, record.id, record.created_at.to_s(:basic), record.updated_at.to_s(:basic)] | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment