sql_footprint

Check your footprint file into source control


Keywords
database, footprint, sql, usage
Install
gem install sql_footprint -v 2.0.1

Documentation

SqlFootprint Build Status

This gem allows you to keep a "footprint" of the sql queries that your application runs. It's like logging all the sql you're executing except that we remove all the value parameters and dedupe similar queries. This footprint should be valuable in determining if changes you've made will significantly change the way you're querying the database.

Installation

Add this line to your application's Gemfile:

gem 'sql_footprint', group: [:development, :test]

And then execute:

$ bundle

Usage

Typically, you would want to run this while you're running your specs. For example w/ RSpec:

RSpec.configure do |config|
  config.before(:suite) { SqlFootprint.start }
  config.after(:suite) { SqlFootprint.stop }
end

Minitest (in test_helper.rb) add the following:

SqlFootprint.start
Minitest.after_run { SqlFootprint.stop }

You can also add a Custom rule to SqlAnonymizer before running start:

RSpec.configure do |config|
  SqlFootprint::SqlAnonymizer.add_rule(/SELECT (.+) AS (.+)/, 'SELECT [redacted] AS [redacted]')
  config.before(:suite) { SqlFootprint.start }
  config.after(:suite) { SqlFootprint.stop }
end

Outputs

After running your specs you'll find a 'footprint.*.sql' file in your project. Footprints are per-database. For example, if you're using DB1 AND DB2 in your app, you would end up with two footprint files. (footprint.db1.sql, footprint.db2.sql)

If you're using an in-memory database, you'll end up with footprint.:memory:.sql.

Excluding Setup Code

If you want to exclude queries that your tests generate for fixture data, you can use the .exclude method. For example:

before do
  SqlFootprint.exclude do
    Model.create!(args*) # this query will not be included in your footprint
  end
end

Or if you're using FactoryGirl you could do something like this:

RSpec.configure do |config|
  module FactoryBoy
    def create(*args)
      SqlFootprint.exclude { FactoryGirl.create(*args) }
    end
  end
  config.include FactoryBoy
end

DO NOT run SqlFootprint in production!