Skip to content

Instantly share code, notes, and snippets.

@jakeonrails
Created January 31, 2017 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakeonrails/cd5b13241f34b9aa4ecfc2f84d3c3196 to your computer and use it in GitHub Desktop.
Save jakeonrails/cd5b13241f34b9aa4ecfc2f84d3c3196 to your computer and use it in GitHub Desktop.
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