Created
September 16, 2022 12:18
-
-
Save kemenaran/cfc820487a62e592dfda196903b6e10c to your computer and use it in GitHub Desktop.
Silencing ActiveRecord SQL logs when loading Rails fixtures
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
# By default, ActiveRecord logs SQL statements during tests – including when | |
# creating fixtures. | |
# | |
# When a Rails app has many fixtures, those logs can clobber test logs with | |
# a lot of logged SQL statements. | |
# | |
# We could disable ActiveRecord SQL logs entirely during tests, but having | |
# SQL queries displayed during tests is useful. Instead this snippet disables | |
# the ActiveRecord SQL logs just when loading fixtures. | |
# | |
# Usage: add this to the bottom of your tests/test_helper.rb file. | |
module FixturesLogSilencer | |
extend ActiveSupport::Concern | |
class_methods do | |
# Remove fixtures creation queries from test logs, even when config.log_level = :debug | |
def create_fixtures(fixtures_directory, fixture_set_names, *args) | |
ActiveRecord::Base.logger.silence(:info) do | |
Rails.benchmark 'Loading fixtures', level: :info do | |
super | |
end | |
end | |
end | |
end | |
end | |
class ActiveRecord::FixtureSet | |
prepend FixturesLogSilencer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment