Class: Departure::DbClient

Inherits:
Object
  • Object
show all
Defined in:
lib/departure/db_client.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, database_client) ⇒ DbClient

Returns a new instance of DbClient.



13
14
15
16
17
18
19
20
21
22
# File 'lib/departure/db_client.rb', line 13

def initialize(config, database_client)
  @config = config
  @database_client = database_client

  connection_details = Departure::ConnectionDetails.new(config)
  verbose = ActiveRecord::Migration.verbose
  sanitizers = [Departure::LogSanitizers::PasswordSanitizer.new(connection_details)]
  @logger = Departure::LoggerFactory.build(sanitizers: sanitizers, verbose: verbose)
  @cli_generator = Departure::CliGenerator.new(connection_details)
end

Instance Attribute Details

#database_clientObject (readonly)

Returns the value of attribute database_client.



11
12
13
# File 'lib/departure/db_client.rb', line 11

def database_client
  @database_client
end

Instance Method Details

#alter_statement?(raw_sql_string) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/departure/db_client.rb', line 48

def alter_statement?(raw_sql_string)
  raw_sql_string =~ /\Aalter table/i
end

#query(raw_sql_string) ⇒ Object

Intercepts raw query calls to pass ALTER TABLE statements to pt-online-schema-change otherwise sends to the database adapter eg: goes to pt-online-schema-change

query("ALTER TABLE `comments` ADD INDEX `index_comments_on_some_id_field` (`some_id_field`))

eg: sends to database adapter

query("COMMIT") - query("SELECT * from 'comments'")


30
31
32
33
34
35
36
# File 'lib/departure/db_client.rb', line 30

def query(raw_sql_string)
  if alter_statement?(raw_sql_string)
    send_to_pt_online_schema_change(raw_sql_string)
  else
    database_client.query(raw_sql_string)
  end
end

#send_to_pt_online_schema_change(raw_sql_string) ⇒ Object

Runs raw_sql_string through pt-online-schema-change command line tool



39
40
41
42
43
44
45
46
# File 'lib/departure/db_client.rb', line 39

def send_to_pt_online_schema_change(raw_sql_string)
  command_line = @cli_generator.parse_statement(raw_sql_string)

  Command.new(command_line,
              Departure.configuration.error_log_path,
              @logger,
              Departure.configuration.redirect_stderr).run
end