Created
January 31, 2017 23:04
-
-
Save jakeonrails/cd5b13241f34b9aa4ecfc2f84d3c3196 to your computer and use it in GitHub Desktop.
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 count_sql_creates | |
counts = Hash.new(0) | |
logger = -> (event, start, finish, id, payload) do | |
if (table = payload[:sql][/INSERT INTO "(.+?)"/, 1]) | |
counts[table] += 1 | |
end | |
end | |
subscription = ActiveSupport::Notifications.subscribe('sql.active_record', logger) | |
yield | |
ActiveSupport::Notifications.unsubscribe(subscription) | |
counts | |
end | |
count_sql_creates { ... some code that creates records ... } | |
# => { companies: 2, orders: 3, ... } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment