Skip to content

Instantly share code, notes, and snippets.

@kemenaran
Created September 16, 2022 12:18
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 kemenaran/cfc820487a62e592dfda196903b6e10c to your computer and use it in GitHub Desktop.
Save kemenaran/cfc820487a62e592dfda196903b6e10c to your computer and use it in GitHub Desktop.
Silencing ActiveRecord SQL logs when loading Rails fixtures
# 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