rails-template icon indicating copy to clipboard operation
rails-template copied to clipboard

Add rake task to return a count of each model in the database

Open eoinkelly opened this issue 2 years ago • 2 comments

I created this for debugging a client project. It seems useful but I'm unsure whether it is useful enough to warrant being in this template. Thoughts?

namespace :info do
    desc "List stats from DB"
    task db_stats: :environment do
        # Ensure all classes are loaded if we are in an environment which lazy
        # loads them (e.g. development)
        Rails.application.eager_load!

        ApplicationRecord
        .descendants
        .map(&:to_s)
        .sort
        .each do |model_name|
            puts "#{model_name}: #{model_name.constantize.count} records"
        end
    end
end

eoinkelly avatar Aug 09 '22 21:08 eoinkelly

We discussed

  • Task is useful enough to put in template
  • We should put it in the app namespace
  • We should run rake -T |grep app and put this in the rake section in the README.
  • Add explanation to README that tasks in app namespace are for tasks which could be run locally or on deployed envs. Task in dev namespace are for running locally for dev happiness.

eoinkelly avatar Oct 20 '22 21:10 eoinkelly

It turns out that Rails already uses the app: namespace for two tasks

# App tasks (useful in local development and deployed environments)
rails app:template                       # Applies the template supplied by LOCATION=(/path/to/template) or URL
rails app:update                         # Update configs and some other initially generated files (or use just update:configs or update:bin)

There is a possibility of overlap if we also use it but it doesn't seem to me like it would be enough of a problem to make us bikeshed another name. Sing out if you disagree.

eoinkelly avatar Oct 21 '22 22:10 eoinkelly