Class: Departure::Runner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/departure/runner.rb

Overview

It executes pt-online-schema-change commands in a new process and gets its output and status

Instance Method Summary collapse

Constructor Details

#initialize(logger, cli_generator, mysql_adapter, config = Departure.configuration) ⇒ Runner

Constructor

Parameters:

  • logger (#say, #write)
  • cli_generator (CliGenerator)
  • mysql_adapter (ActiveRecord::ConnectionAdapter)

    it must implement #execute and #raw_connection



17
18
19
20
21
22
23
# File 'lib/departure/runner.rb', line 17

def initialize(logger, cli_generator, mysql_adapter, config = Departure.configuration)
  @logger = logger
  @cli_generator = cli_generator
  @mysql_adapter = mysql_adapter
  @error_log_path = config&.error_log_path
  @redirect_stderr = config&.redirect_stderr
end

Instance Method Details

#abandon_results!Object



29
30
31
# File 'lib/departure/runner.rb', line 29

def abandon_results!
  raw_connection.abandon_results!
end

#affected_rowsInteger

Returns the number of rows affected by the last UPDATE, DELETE or INSERT statements

Returns:

  • (Integer)


58
59
60
# File 'lib/departure/runner.rb', line 58

def affected_rows
  raw_connection.affected_rows
end

#database_adapterObject



33
34
35
# File 'lib/departure/runner.rb', line 33

def database_adapter
  @mysql_adapter
end

#execute(command_line) ⇒ Boolean

TODO: rename it so we don’t confuse it with AR’s #execute Runs and logs the given command

Parameters:

  • command_line (String)

Returns:

  • (Boolean)


67
68
69
# File 'lib/departure/runner.rb', line 67

def execute(command_line)
  Command.new(command_line, error_log_path, logger, redirect_stderr).run
end

#query(sql) ⇒ Object

Executes the passed sql statement using pt-online-schema-change for ALTER TABLE statements, or the specified mysql adapter otherwise.

Parameters:

  • sql (String)


45
46
47
48
49
50
51
52
# File 'lib/departure/runner.rb', line 45

def query(sql)
  if alter_statement?(sql)
    command_line = cli_generator.parse_statement(sql)
    execute(command_line)
  else
    database_adapter.execute(sql)
  end
end

#query_optionsObject



25
26
27
# File 'lib/departure/runner.rb', line 25

def query_options
  raw_connection.query_options
end

#raw_connectionObject



37
38
39
# File 'lib/departure/runner.rb', line 37

def raw_connection
  database_adapter.raw_connection
end