Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/sql_tracker/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module SqlTracker
class Config
include ActiveSupport::Configurable
config_accessor :tracked_paths, :tracked_sql_command, :output_path, :enabled
config_accessor :tracked_paths, :tracked_sql_command, :output_path, :enabled, :retain_sql_query_ids

class << self
def apply_defaults
Expand All @@ -17,6 +17,7 @@ def apply_defaults
'tmp'
end
end
self.retain_sql_query_ids ||= false
self
end
end
Expand Down
14 changes: 8 additions & 6 deletions lib/sql_tracker/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ def trace_path_matcher

def clean_sql_query(query)
query.squish!
query.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|[\$\+\-\w\.]+)/, '\1xxx')
query.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)')
query.gsub!(/(\sBETWEEN\s)('[^']+'|[\+\-\w\.]+)(\sAND\s)('[^']+'|[\+\-\w\.]+)/i, '\1xxx\3xxx')
query.gsub!(/(\sVALUES\s)\(.+\)/i, '\1(xxx)')
query.gsub!(/(\s(LIKE|ILIKE|SIMILAR TO|NOT SIMILAR TO)\s)('[^']+')/i, '\1xxx')
query.gsub!(/(\s(LIMIT|OFFSET)\s)(\d+)/i, '\1xxx')
unless @config&.retain_sql_query_ids
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think It's better to return the query directly instead of wrapping everything in the conditional block.

So something like:

Suggested change
unless @config&.retain_sql_query_ids
return query if @config&.retain_sql_query_ids

query.gsub!(/(\s(=|>|<|>=|<=|<>|!=)\s)('[^']+'|[\$\+\-\w\.]+)/, '\1xxx')
query.gsub!(/(\sIN\s)\([^\(\)]+\)/i, '\1(xxx)')
query.gsub!(/(\sBETWEEN\s)('[^']+'|[\+\-\w\.]+)(\sAND\s)('[^']+'|[\+\-\w\.]+)/i, '\1xxx\3xxx')
query.gsub!(/(\sVALUES\s)\(.+\)/i, '\1(xxx)')
query.gsub!(/(\s(LIKE|ILIKE|SIMILAR TO|NOT SIMILAR TO)\s)('[^']+')/i, '\1xxx')
query.gsub!(/(\s(LIMIT|OFFSET)\s)(\d+)/i, '\1xxx')
end
query
end

Expand Down
2 changes: 2 additions & 0 deletions test/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def test_defaults_should_not_overwrite_user_configs
SqlTracker::Config.tracked_paths = %w(app/model)
SqlTracker::Config.tracked_sql_command = %w(SELECT)
SqlTracker::Config.output_path = '/usr/local/test'
SqlTracker::Config.retain_sql_query_ids = true

config = SqlTracker::Config.apply_defaults

assert_equal(false, config.enabled)
assert_equal(%w(app/model), config.tracked_paths)
assert_equal(%w(SELECT), config.tracked_sql_command)
assert_equal('/usr/local/test', config.output_path)
assert_equal(true, config.retain_sql_query_ids)
end
end
end
17 changes: 17 additions & 0 deletions test/handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ def test_clean_values
assert_equal(expected, cleaned_query)
end

def test_not_clean_values_due_to_config
query = %{
INSERT INTO users VALUES
(nextval('id_seq'), 'a', 105, DEFAULT),
(nextval('id_seq'), 'b', 9100, DEFAULT);
}
expected = %{
INSERT INTO users VALUES (nextval('id_seq'), 'a', 105, DEFAULT), (nextval('id_seq'), 'b', 9100, DEFAULT);
}.squish

config = sample_config
config.retain_sql_query_ids = true
handler = SqlTracker::Handler.new(config)
returned_query = handler.clean_sql_query(query)
assert_equal(expected, returned_query)
end

def test_clean_pattern_matching
query = %(
SELECT users.* FROM users
Expand Down