Sha256: 952700f9cc3eb5b605397a630d6bac91b477f5eab851b3d501a08ebbb272e4c1

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 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 #{@hostname}") if @hostname
          parts.push("-u #{@username}") if @username
          parts.push("-p#{@password}") if @password

          parts.push(@database)

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

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

        private

        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.2 lib/koine/db_bkp/mysql/dump.rb
koine-db_bkp-0.1.1 lib/koine/db_bkp/mysql/dump.rb