Class: Exwiw::AfterInsertHook

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

Defined Under Namespace

Classes: Context

Class Method Summary collapse

Class Method Details

.run(path:, cli_options:, output_dir:, next_idx:, output_extension:, logger:) ⇒ Object



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

def self.run(path:, cli_options:, output_dir:, next_idx:, output_extension:, logger:)
  ext = File.extname(path)

  if ext == '.rb'
    run_ruby(
      path: path,
      cli_options: cli_options,
      output_dir: output_dir,
      next_idx: next_idx,
      output_extension: output_extension,
      logger: logger,
    )
  else
    run_shell(path: path, cli_options: cli_options, output_dir: output_dir, logger: logger)
  end
end

.run_ruby(path:, cli_options:, output_dir:, next_idx:, output_extension:, logger:) ⇒ Object



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
# File 'lib/exwiw/after_insert_hook.rb', line 24

def self.run_ruby(path:, cli_options:, output_dir:, next_idx:, output_extension:, logger:)
  ctx = Context.new(cli_options, output_extension: output_extension)
  ctx.instance_eval(File.read(path), path)

  # One output file per buffer, numbered sequentially from next_idx: first
  # the shared collection-less buffer (kept at next_idx so existing hooks
  # produce the same filename as before), then one file per targeted
  # collection in first-targeted order. Collection files reuse the
  # insert-NNN-<collection>.{ext} naming of the per-table dump files, so
  # filename-driven import tooling handles them unchanged.
  outputs = []
  content = ctx.collected.join("\n")
  outputs << ['after_insert', content] unless content.empty?
  ctx.collected_by_collection.each do |collection, chunks|
    body = chunks.join("\n")
    next if body.empty?
    outputs << [collection, body]
  end

  if outputs.empty?
    logger.info("After-insert hook produced no output; skipping file write.")
    return
  end

  outputs.each_with_index do |(name, body), offset|
    idx_str = (next_idx + offset).to_s.rjust(3, '0')
    output_path = File.join(output_dir, "insert-#{idx_str}-#{name}.#{output_extension}")
    File.write(output_path, body)
    logger.info("Wrote after-insert hook output to #{output_path}")
  end
end

.run_shell(path:, cli_options:, output_dir:, logger:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/exwiw/after_insert_hook.rb', line 56

def self.run_shell(path:, cli_options:, output_dir:, logger:)
  env = {
    'EXWIW_OUTPUT_DIR'       => output_dir,
    'EXWIW_SCHEMA_DIR'       => cli_options[:schema_dir].to_s,
    'EXWIW_DATABASE_ADAPTER' => cli_options[:database_adapter].to_s,
    'EXWIW_DATABASE_HOST'    => cli_options[:database_host].to_s,
    'EXWIW_DATABASE_PORT'    => cli_options[:database_port].to_s,
    'EXWIW_DATABASE_USER'    => cli_options[:database_user].to_s,
    'EXWIW_DATABASE_NAME'    => cli_options[:database_name].to_s,
    'EXWIW_TARGET_TABLE'     => cli_options[:target_table].to_s,
    'EXWIW_SCOPE_COLUMN'     => cli_options[:scope_column].to_s,
    'EXWIW_IDS'              => Array(cli_options[:ids]).join(','),
    'EXWIW_OUTPUT_FORMAT'    => cli_options[:output_format].to_s,
  }
  logger.info("Running after-insert shell hook: #{path}")
  ok = system(env, path)
  raise "after-insert shell hook failed: #{path}" unless ok
end