Sha256: b53bfdfe05cc2cdc9162befdecee5c60149ee55603a1abd8325efe015c6d123b

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

module PerconaMigrator
  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

6 entries across 6 versions & 1 rubygems

Version Path
percona_migrator-3.0.0 lib/percona_migrator/alter_argument.rb
percona_migrator-1.1.0 lib/percona_migrator/alter_argument.rb
percona_migrator-0.1.1 lib/percona_migrator/alter_argument.rb
percona_migrator-1.0.0 lib/percona_migrator/alter_argument.rb
percona_migrator-0.1.0.rc.7 lib/percona_migrator/alter_argument.rb
percona_migrator-0.1.0.rc.6 lib/percona_migrator/alter_argument.rb