Skip to content

Instantly share code, notes, and snippets.

@walidvb
Created September 2, 2021 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walidvb/1af865aa4e41ab9bf9b9b297130d7926 to your computer and use it in GitHub Desktop.
Save walidvb/1af865aa4e41ab9bf9b9b297130d7926 to your computer and use it in GitHub Desktop.
Count all rows from all tables on rails
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