Class: NoBrainer::QueryRunner::MissingIndex

Inherits:
Middleware
  • Object
show all
Defined in:
lib/no_brainer/query_runner/missing_index.rb

Instance Method Summary collapse

Methods inherited from Middleware

#initialize

Constructor Details

This class inherits a constructor from NoBrainer::QueryRunner::Middleware

Instance Method Details

#call(env) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/no_brainer/query_runner/missing_index.rb', line 2

def call(env)
  @runner.call(env)
rescue RuntimeError => e
  if match_data = /^Index `(.+)` was not found on table `(.+)\.(.+)`\.$/.match(e.message)
    _, index_name, db_name, table_name = *match_data

    model = NoBrainer::Document.all.select { |m| m.table_name == table_name }.first
    index = model.indexes.values.select { |i| i.aliased_name == index_name.to_sym }.first if model
    index_name = index.name if index

    if model.try(:pk_name).try(:to_s) == index_name.to_s
      err_msg  = "Please update the primary key `#{index_name}` in the table `#{db_name}.#{table_name}`."
    else
      err_msg  = "Please run `NoBrainer.sync_indexes' or `rake nobrainer:sync_indexes' to create the index `#{index_name}`"
      err_msg += " in the table `#{db_name}.#{table_name}`."
      err_msg += " Read http://nobrainer.io/docs/indexes for more information."
    end

    raise NoBrainer::Error::MissingIndex.new(err_msg)
  end
  raise
end