Created
September 2, 2021 14:01
-
-
Save walidvb/1af865aa4e41ab9bf9b9b297130d7926 to your computer and use it in GitHub Desktop.
Count all rows from all tables on rails
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
class DatabaseReport | |
def entry_counts | |
tables = table_model_names.map do |model_name| | |
entity = model_name.constantize rescue nil | |
next if entity.nil? | |
[entity.to_s, entity.count] | |
end.compact | |
total = tables.reduce(0) do |acc, curr| | |
puts "#{curr[0]}: #{curr[1]}" | |
acc + curr[1] | |
end | |
puts "Total: #{total}" | |
end | |
private | |
def table_model_names | |
ActiveRecord::Base.connection.tables.map(&:singularize).map(&:camelize) | |
end | |
end | |
DatabaseReport.new.entry_counts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment