Class: PgCanary::Rules::FunctionOnColumn

Inherits:
Base
  • Object
show all
Defined in:
lib/pg_canary/rules/definitions/function_on_column.rb

Overview

A column wrapped in a function inside WHERE (lower(email) = ?, date(created_at) = ?) can never use a plain index on that column. Silent when a matching expression index (same function, same column) exists.

Constant Summary collapse

CHECKED_KINDS =
%i[AEXPR_OP AEXPR_LIKE AEXPR_ILIKE AEXPR_IN AEXPR_BETWEEN].freeze

Instance Method Summary collapse

Methods inherited from Base

all, check, default_enabled, enabled?, #initialize, option, options, rule_name

Constructor Details

This class inherits a constructor from PgCanary::Rules::Base

Instance Method Details

#checkObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pg_canary/rules/definitions/function_on_column.rb', line 16

def check
  detections = []
  each_scope do |scope|
    next unless scope.where_clause

    scope.where_clause.walk_scope do |node|
      next unless node.is_a?(PgQuery::A_Expr) && CHECKED_KINDS.include?(node.kind)

      [node.lexpr, node.rexpr].each do |side|
        detections << inspect_side(scope, side)
      end
    end
  end
  detections.compact
end