Class: Departure::CliGenerator

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

Overview

Generates the equivalent Percona’s pt-online-schema-change command to the given SQL statement

–no-check-alter is used to allow running CHANGE COLUMN statements. For more details, check: www.percona.com/doc/percona-toolkit/2.2/pt-online-schema-change.html#cmdoption-pt-online-schema-change–[no]check-alter # rubocop:disable Metrics/LineLength

Constant Summary collapse

COMMAND_NAME =
'pt-online-schema-change'.freeze
DEFAULT_OPTIONS =
Set.new(
  [
    Option.new('execute'),
    Option.new('statistics'),
    Option.new('alter-foreign-keys-method', 'auto'),
    Option.new('no-check-alter')
  ]
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(connection_details) ⇒ CliGenerator

TODO: Better doc.

Constructor. Specify any arguments to pass to pt-online-schema-change passing the PERCONA_ARGS env var when executing the migration

Parameters:

  • connection_data (Hash)


31
32
33
# File 'lib/departure/cli_generator.rb', line 31

def initialize(connection_details)
  @connection_details = connection_details
end

Instance Method Details

#generate(table_name, statement) ⇒ String

Generates the percona command. Fills all the connection credentials from the current AR connection, but that can be amended via ENV-vars: PERCONA_DB_HOST, PERCONA_DB_USER, PERCONA_DB_PASSWORD, PERCONA_DB_NAME Table name can’t not be amended, it populates automatically from the migration data

Parameters:

  • table_name (String)
  • statement (String)

    MySQL statement

Returns:

  • (String)


44
45
46
47
48
49
# File 'lib/departure/cli_generator.rb', line 44

def generate(table_name, statement)
  alter_argument = AlterArgument.new(statement)
  dsn = DSN.new(connection_details.database, table_name)

  "#{command} #{all_options} #{dsn} #{alter_argument}"
end

#parse_statement(statement) ⇒ String

Generates the percona command for a raw MySQL statement. Fills all the connection credentials from the current AR connection, but that can amended via ENV-vars: PERCONA_DB_HOST, PERCONA_DB_USER, PERCONA_DB_PASSWORD, PERCONA_DB_NAME Table name can’t not be amended, it populates automatically from the migration data

Parameters:

  • statement (String)

    MySQL statement

Returns:

  • (String)


59
60
61
62
63
64
# File 'lib/departure/cli_generator.rb', line 59

def parse_statement(statement)
  alter_argument = AlterArgument.new(statement)
  dsn = DSN.new(connection_details.database, alter_argument.table_name)

  "#{command} #{all_options} #{dsn} #{alter_argument}"
end