diff --git a/notifier/query_exclusion.rb b/notifier/query_exclusion.rb index 97c355cd476d7cd62ad0eb5fea85a5d67aa524b5..7b5a39c49da4b21e43cbe62efd49be17704c0d9a 100644 --- a/notifier/query_exclusion.rb +++ b/notifier/query_exclusion.rb @@ -5,27 +5,29 @@ require 'pg_query' class QueryExclusion # rubocop:disable Layout/LineLength EXCLUSIONS = [ - "select pg_database_size(current_database()) /*application:test*/", - "SELECT \"schema_migrations\".\"version\" FROM \"schema_migrations\" ORDER BY \"schema_migrations\".\"version\" ASC /*application:test*/", - "select pg_stat_statements_reset() /*application:test*/", + "select pg_database_size(current_database())", + "SELECT \"schema_migrations\".\"version\" FROM \"schema_migrations\" ORDER BY \"schema_migrations\".\"version\" ASC", + "select pg_stat_statements_reset()", "SELECT c.relname FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = ANY (current_schemas($1)) AND c.relname = $2 AND c.relkind IN ($3,$4)", - "INSERT INTO \"schema_migrations\" (\"version\") VALUES ($1) RETURNING \"version\" /*application:test*/", - "SELECT \"ar_internal_metadata\".* FROM \"ar_internal_metadata\" WHERE \"ar_internal_metadata\".\"key\" = $1 LIMIT $2 /*application:test*/", + "INSERT INTO \"schema_migrations\" (\"version\") VALUES ($1) RETURNING \"version\"", + "SELECT \"ar_internal_metadata\".* FROM \"ar_internal_metadata\" WHERE \"ar_internal_metadata\".\"key\" = $1 LIMIT $2", "SELECT pg_try_advisory_lock($1)", "SELECT pg_advisory_unlock($1)", "SELECT current_database()", "SELECT $1", - "SELECT current_schema" - ].map(&:downcase).freeze + "SELECT current_schema" + ].map { |q| PgQuery.parse(q).deparse.downcase }.freeze # rubocop:enable Layout/LineLength def self.exclude?(sql) normalized_query = PgQuery.normalize(sql).downcase - return true if normalized_query.start_with?(/commit|show|reset|begin|release savepoint|savepoint|set|rollback/) return true if normalized_query.include?('/* pgssignore */') - return true if EXCLUSIONS.include?(normalized_query) - tables = PgQuery.parse(normalized_query).tables + query_without_comments = PgQuery.parse(normalized_query).deparse.downcase + return true if query_without_comments.start_with?(/commit|show|reset|begin|release|savepoint|set|rollback/) + return true if EXCLUSIONS.include?(query_without_comments) + + tables = PgQuery.parse(query_without_comments).tables return false if tables.empty? diff --git a/notifier/spec/query_exclusion_spec.rb b/notifier/spec/query_exclusion_spec.rb index 71fc1c6bc4663516dc91ab0f70bcb9295032c249..62e82f07c0beb12d7afd43542e28b4b5f2fdb3e5 100644 --- a/notifier/spec/query_exclusion_spec.rb +++ b/notifier/spec/query_exclusion_spec.rb @@ -8,6 +8,14 @@ RSpec.describe QueryExclusion do expect(described_class.exclude?(query)).to be true end + it 'is true if the query differs from the excluded list only by a comment' do + query = described_class::EXCLUSIONS.first + + query_with_comment = "#{query} /* some comment */" + + expect(described_class.exclude?(query_with_comment)).to be true + end + it 'is true if query includes /* pgssignore /*' do query = "SELECT 1 /* pgssignore */"