Sha256: 32881db04d2c744ab3a3b24e4f450aedfac63357867eed49060a3adfdb414ca2

Contents?: true

Size: 1.18 KB

Versions: 11

Compression:

Stored size: 1.18 KB

Contents

require 'English'

module GhostAdapter
  class Command
    def initialize(alter:, table:, database: nil, dry_run: false)
      @alter = alter
      @table = table
      @database = GhostAdapter.config.database || database
      @dry_run = dry_run
      validate_args_and_config!
    end

    def to_a
      [
        EXECUTABLE,
        *base_args,
        *config_args,
        *execute_arg
      ]
    end

    private

    EXECUTABLE = 'gh-ost'.freeze

    attr_reader :alter, :database, :table, :dry_run

    def validate_args_and_config!
      raise ArgumentError, 'alter cannot be nil' if alter.nil?
      raise ArgumentError, 'table cannot be nil' if table.nil?
      raise ArgumentError, 'database cannot be nil' if database.nil?
    end

    def base_args
      [
        "--alter=#{alter}",
        "--table=#{table}",
        "--database=#{database}"
      ]
    end

    def config_args
      context = {
        pid: $PID,
        table: table,
        database: database,
        timestamp: Time.now.utc.to_i,
        unique_id: SecureRandom.uuid
      }

      GhostAdapter.config.as_args(context: context)
    end

    def execute_arg
      dry_run ? [] : ['--execute']
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ghost_adapter-0.7.0 lib/ghost_adapter/command.rb
ghost_adapter-0.6.0 lib/ghost_adapter/command.rb
ghost_adapter-0.5.0 lib/ghost_adapter/command.rb
ghost_adapter-0.4.2 lib/ghost_adapter/command.rb
ghost_adapter-0.4.1 lib/ghost_adapter/command.rb
ghost_adapter-0.4.0 lib/ghost_adapter/command.rb
ghost_adapter-0.3.0 lib/ghost_adapter/command.rb
ghost_adapter-0.2.3 lib/ghost_adapter/command.rb
ghost_adapter-0.2.2 lib/ghost_adapter/command.rb
ghost_adapter-0.2.1 lib/ghost_adapter/command.rb
ghost_adapter-0.2.0 lib/ghost_adapter/command.rb