Sha256: f0dfbfdfc8837fabafa595614f9c7374a521b9f0a1123d765eb8ab3b80faca8b

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 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 [^\s]*[\n]* /

    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
      # Separates the ALTER TABLE from the table_name
      #
      # Removes the grave marks, if they are there, so we can get the table_name
      @table_name = String(match)
                      .split(' ')[2]
                      .delete('`')
    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('`', '\\\`')
        .gsub(/\\n/, '')
        .gsub('"', '\\\"')
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
departure-6.7.0 lib/departure/alter_argument.rb
departure-6.6.0 lib/departure/alter_argument.rb
departure-6.5.0 lib/departure/alter_argument.rb
departure-6.4.0 lib/departure/alter_argument.rb
departure-6.3.0 lib/departure/alter_argument.rb
departure-76c9880-6.2.0 lib/departure/alter_argument.rb
departure-6.2.0 lib/departure/alter_argument.rb