Sha256: 9e5a359f037a98a23a9738e01eaa7401d956619a4ccdeba8a21da3a90f82a598
Contents?: true
Size: 1.64 KB
Versions: 1
Compression:
Stored size: 1.64 KB
Contents
# frozen_string_literal: true require "active_record_doctor/detectors/base" module ActiveRecordDoctor module Detectors class UnindexedForeignKeys < Base # :nodoc: @description = "detect unindexed foreign keys" @config = { ignore_tables: { description: "tables whose foreign keys should not be checked", global: true }, ignore_columns: { description: "columns, written as table.column, that should not be checked" } } private def message(table:, column:) # rubocop:disable Layout/LineLength "add an index on #{table}.#{column} - foreign keys are often used in database lookups and should be indexed for performance reasons" # rubocop:enable Layout/LineLength end def detect each_table(except: config(:ignore_tables)) do |table| each_column(table, except: config(:ignore_columns)) do |column| next unless foreign_key?(column) next if indexed?(table, column) next if indexed_as_polymorphic?(table, column) problem!(table: table, column: column.name) end end end def foreign_key?(column) column.name.end_with?("_id") end def indexed?(table, column) connection.indexes(table).any? do |index| index.columns.first == column.name end end def indexed_as_polymorphic?(table, column) type_column_name = column.name.sub(/_id\Z/, "_type") connection.indexes(table).any? do |index| index.columns == [type_column_name, column.name] end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_record_doctor-1.11.0 | lib/active_record_doctor/detectors/unindexed_foreign_keys.rb |