Last active
August 29, 2015 14:17
-
-
Save austinfromboston/4b727c13d4d6f1a2754c to your computer and use it in GitHub Desktop.
this is a simple way to count the rows in your database, in case you are trying to track down where rows are getting created
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 RowCounter | |
def self.checkpoint | |
@counter_collection ||= [] | |
@counter_collection << RowCounter.new | |
@counter_collection[-1] - @counter_collection[-2] | |
end | |
def initialize | |
census | |
end | |
def tables | |
@tables ||= ActiveRecord::Base.connection.tables | |
end | |
def census | |
@census ||= tables.reduce({}) do |counts, table_name| | |
counts[table_name] = ActiveRecord::Base.connection.execute("select count(*) from #{table_name};").first.values.first.to_i | |
counts | |
end | |
end | |
def -(other_rowcounter) | |
return census.select {|_,v| v>0} unless other_rowcounter | |
other_census = other_rowcounter.census | |
census.reduce({}) do |result, (key, value)| | |
next result if value == other_census[key] | |
result[key] = value.to_i - other_census[key].to_i | |
result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment