Sha256: 55e18d8d168dfbde141f9989213d53493aa0c3c849f41234d7f2dc252a2d438f

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'addressable/uri'

module Koine
  module DbBkp
    module Mysql
      class Dump
        def initialize(config = {})
          config = normalize_config(config)

          @hostname = config.fetch(:hostname)
          @database = config.fetch(:database)
          @username = config[:username]
          @password = config[:password]
          @cli = Cli.new
        end

        def to_sql_file(file)
          parts = ['mysqldump']

          parts.push("-h #{escape(@hostname)}") if @hostname
          parts.push("-u #{escape(@username)}") if @username
          parts.push("-p#{escape(@password)}") if @password

          parts.push(@database)

          file = FileName.new(file)
          parts.push("> #{file}")

          @cli.execute(parts.join(' '))
        end

        private

        def escape(string)
          Shellwords.escape(string)
        end

        def normalize_config(config)
          config = config.reject { |_k, v| ['', nil].include?(v) }
          merge_url(symbolize_keys(config))
        end

        def merge_url(config)
          url = config.delete(:url)

          return config unless url

          url = Addressable::URI.parse(url)

          config.merge(
            adapter: url.scheme,
            hostname: url.host,
            database: url.path.split('/').join(''),
            username: url.user,
            password: url.password
          )
        end

        def symbolize_keys(hash)
          {}.tap do |new_hash|
            hash.each { |key, value| new_hash[key.to_sym] = value }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
koine-db_bkp-0.1.4 lib/koine/db_bkp/mysql/dump.rb
koine-db_bkp-0.1.3 lib/koine/db_bkp/mysql/dump.rb