steventen/sql_tracker

Disable writing files to SqlTracker::Config.output_path

yoomlam opened this issue · 14 comments

The sql_tracker-*.json files are piling up in SqlTracker::Config.output_path. I'd like an option to disable writing these files as I use SqlTracker.track in RSpec tests.

Setting SqlTracker::Config.output_path = nil doesn't stop writing the files.

Hi @yoomlam
Sorry for the late reply. I think currently the code does not check whether it is nil or not, since it assumes that the user would always need this output file.

I think we can provide some option to disable it, but firstly, I'd like to know how do you use this in your case without the output file?

Hi @steventen,

Here's an example rspec where I'm using SqlTracker:

          query_data = SqlTracker.track do
            document_fetcher.find_or_create_documents!
          end

          # Uncomment the following to see a count of SQL queries
          # pp query_data.values.pluck(:sql, :count)
          doc_insert_queries = query_data.values.select { |o| o[:sql].start_with?("INSERT INTO \"documents\"") }
          expect(doc_insert_queries.pluck(:count).max).to eq 1

Note that I don't need the files -- I only need to examine the data in query_data.

Is your rspec part of Rails application testing? My current guess is that this test is part of rails test, therefore this part is probably triggered as well. So when the test stops and the rails instance exit, it triggers the code that saves the results into a file.

I think you can probably just add SqlTracker::Config.enabled = false in your test config file

A sql_tracker-81539-1614177391.json file is still created and query_data.values is empty.

Is the file empty as well or contains some data?

It contains 4.2KB of data. Are you able to reproduce?

Can you make sure to add the setting in config/environments/test.rb:

Rails.application.configure do
  ... ...
  SqlTracker::Config.enabled = false
end

Progress! Files are not created, but query_data is empty: {}.

Yeah, I see the problem now. The track block is using the options from the class attribute, therefore, when you set enabled = false, it would disable everything.

I think when people uses the track block approach, it should always make the enabled = true, regardless of the option at class level.

I'll create a PR to fix this shortly!

@yoomlam I have merged a fix into master. If you could try the master, that should fix the issue.

Thanks

@steventen Works great! Thanks! Please push out a new version of the gem.

The new version has just released. Thanks!