Sha256: 2f0019c9490ec139e9b8ce85cf0669c7d984f61833eae7dd7549256ea2397556

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module Departure
  class InvalidAlterStatement < StandardError; end

  # Represents the '--alter' argument of Percona's pt-online-schema-change
  # See https://www.percona.com/doc/percona-toolkit/2.0/pt-online-schema-change.html
  class AlterArgument
    ALTER_TABLE_REGEX = /\AALTER TABLE `(\w+)` /

    attr_reader :table_name

    # Constructor
    #
    # @param statement [String]
    # @raise [InvalidAlterStatement] if the statement is not an ALTER TABLE
    def initialize(statement)
      @statement = statement

      match = statement.match(ALTER_TABLE_REGEX)
      raise InvalidAlterStatement unless match

      @table_name = match.captures[0]
    end

    # Returns the '--alter' pt-online-schema-change argument as a string. See
    # https://www.percona.com/doc/percona-toolkit/2.0/pt-online-schema-change.html
    def to_s
      "--alter \"#{parsed_statement}\""
    end

    private

    attr_reader :statement

    # Removes the 'ALTER TABLE' portion of the SQL statement
    #
    # @return [String]
    def parsed_statement
      @parsed_statement ||= statement
        .gsub(ALTER_TABLE_REGEX, '')
        .gsub('`', '\\\`')
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
departure-6.1.0 lib/departure/alter_argument.rb
departure-6.0.0 lib/departure/alter_argument.rb
departure-5.0.0 lib/departure/alter_argument.rb