Class: Exwiw::ExplainRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/exwiw/explain_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_config:, schema_dir:, dump_target:, logger:, io: $stdout, explain_verbosity: nil) ⇒ ExplainRunner

Returns a new instance of ExplainRunner.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/exwiw/explain_runner.rb', line 5

def initialize(
  connection_config:,
  schema_dir:,
  dump_target:,
  logger:,
  io: $stdout,
  explain_verbosity: nil
)
  @connection_config = connection_config
  @schema_dir = schema_dir
  @dump_target = dump_target
  @logger = logger
  @io = io
  # mongodb-only; nil lets the adapter pick its safe default (queryPlanner).
  @explain_verbosity = explain_verbosity
end

Instance Method Details

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/exwiw/explain_runner.rb', line 22

def run
  adapter = Adapter.build(@connection_config, @logger)
  # explain executes nothing, so an adapter whose non-target scope is captured
  # at runtime (mongodb) has no parent ids to filter children by; ask it to
  # build placeholder scope filters so each query reflects the real dump.
  adapter.explain_scope_with_placeholders!
  configs = load_table_config(adapter.class.table_config_class)
  validate_ignored(configs)

  table_by_name = configs.each_with_object({}) { |config, hash| hash[config.name] = config }

  target = table_by_name[@dump_target.table_name]
  validate_target_exists!(target)
  adapter.validate_as_dump_target!(target) if target

  dumpable_configs = configs.select { |c| adapter.dumpable?(c) }
  QueryAstBuilder.validate_scope!(dumpable_configs, table_by_name, @dump_target, @logger)

  @logger.debug("Determining table processing order...")
  ordered_table_names = DetermineTableProcessingOrder.run(dumpable_configs, logger: @logger)

  total_size = ordered_table_names.size
  ordered_table_names.each_with_index do |table_name, idx|
    table = table_by_name.fetch(table_name)
    if table.ignore
      @logger.debug("Skipping explain for '#{table_name}' (ignore:true)")
      next
    end

    @logger.debug("Explaining '#{table_name}'... (#{idx + 1}/#{total_size})")

    query_ast = adapter.build_query(table, @dump_target, table_by_name)
    # describe_query is the adapter-agnostic "what query is this": the
    # commented SELECT for SQL adapters, the find description for mongodb.
    query_text = adapter.describe_query(query_ast)
    explain_text = adapter.explain(query_ast, verbosity: @explain_verbosity)

    @io.puts "-- [#{idx + 1}/#{total_size}] #{table_name}"
    @io.puts query_text
    @io.puts
    @io.puts "-- EXPLAIN:"
    @io.puts explain_text
    @io.puts
  end
end