Sha256: 63d8030678d614f9593278f88aab3d84e52df559bd00c8ed957eb90a578f4c8f

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module Wordmove
  class Doctor
    class Mysql
      attr_reader :config, :logger

      def initialize(movefile_name = nil, movefile_dir = '.')
        @logger = Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
        begin
          @config = Wordmove::Movefile.new(movefile_name, movefile_dir).fetch[:local][:database]
        rescue Psych::SyntaxError
          return
        end
      end

      def check!
        logger.task "Checking local database commands and connection"

        return logger.error "Can't connect to mysql using your movefile.yml" if config.nil?

        mysql_client_doctor
        mysqldump_doctor
        mysql_server_doctor
      end

      private

      def mysql_client_doctor
        if system("which mysql", out: File::NULL)
          logger.success "`mysql` command is in $PATH"
        else
          logger.error "`mysql` command is not in $PATH"
        end
      end

      def mysqldump_doctor
        if system("which mysqldump", out: File::NULL)
          logger.success "`mysqldump` command is in $PATH"
        else
          logger.error "`mysqldump` command is not in $PATH"
        end
      end

      def mysql_server_doctor
        command = ["mysql"]
        command << "-u #{config['user']}"
        command << "-p#{config['password']}" unless config['password'].blank?
        command << "-h #{config['host']}"
        command << "-e'QUIT'"
        command = command.join(" ")

        if system(command, out: File::NULL, err: File::NULL)
          logger.success "Successfully connected to the database"
        else
          logger.error <<~LONG
            We can't connect to the database using credentials
            specified in the Movefile. Double check them or try
            to debug your system configuration.

            The command used to test was:

            #{command}
          LONG
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wordmove-2.4.2 lib/wordmove/doctor/mysql.rb
wordmove-2.4.1 lib/wordmove/doctor/mysql.rb
wordmove-2.4.0 lib/wordmove/doctor/mysql.rb