Created
February 1, 2022 14:52
-
-
Save floehopper/c9f9e771754761938e88da7c5f1bcfef to your computer and use it in GitHub Desktop.
Display changed row counts using ActiveRecord
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
def row_counts | |
Hash[*ApplicationRecord.connection.execute(%{ | |
ANALYZE; | |
SELECT | |
pgClass.relname AS tableName, | |
pgClass.reltuples AS rowCount | |
FROM | |
pg_class pgClass | |
INNER JOIN | |
pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace) | |
WHERE | |
pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND | |
pgClass.relkind='r' | |
}).values.flatten] | |
end | |
def display_changed_row_counts | |
original_counts = row_counts | |
yield | |
new_counts = row_counts | |
new_counts.keys.sort.each do |table_name| | |
original_count = original_counts[table_name].to_i | |
new_count = new_counts[table_name].to_i | |
unless new_count == original_count | |
puts "#{table_name}: #{original_count} -> #{new_count} (#{'%+d' % (new_count - original_count)})" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment