<commit_before>require "hanami/utils/blank"

module Hanami
  module Model
    class Migrator
      # MySQL adapter
      #
      # @since 0.4.0
      # @api private
      class MySQLAdapter < Adapter
        # @since x.x.x
        # @api private
        HOST = 'MYSQL_HOST'.freeze

        # @since x.x.x
        # @api private
        PORT = 'MYSQL_TCP_PORT'.freeze

        # @since 0.7.0
        # @api private
        PASSWORD = '<PASSWORD>'.freeze

        # @since 1.0.0
        # @api private
        DB_CREATION_ERROR = 'Database creation failed. If the database exists, ' \
                            'then its console may be open. See this issue for more details: ' \
                            'https://github.com/hanami/model/issues/250'.freeze

        # @since 0.4.0
        # @api private
        def create
          new_connection(global: true).run %(CREATE DATABASE `#{database}`;)
        rescue Sequel::DatabaseError => e
          message = if e.message.match(/database exists/) # rubocop:disable Performance/RedundantMatch
                      DB_CREATION_ERROR
                    else
                      e.message
                    end

          raise MigrationError.new(message)
        end

        # @since 0.4.0
        # @api private
        def drop
          new_connection(global: true).run %(DROP DATABASE `#{database}`;)
        rescue Sequel::DatabaseError => e
          message = if e.message.match(/doesn\'t exist/) # rubocop:disable Performance/RedundantMatch
                      "Cannot find database: #{database}"
                    else
                      e.message
                    end

          raise MigrationError.new(message)
        end

        # @since 0.4.0
        # @api private
        def dump
          set_environment_variables
          dump_structure
          dump_migrations_data
        end

        # @since 0.4.0
        # @api private
        def load
          set_environment_variables
          load_structure
        end

        private

        # @since 0.7.0
        # @api private
        def set_environment_variables
          ENV[HOST]     = host     unless Utils::Blank.blank?(host)
          ENV[PORT]     = port     unless Utils::Blank.blank?(port)
          ENV[PASSWORD] = password unless Utils::Blank.blank?(password)
        end

        # @since 0.7.0
        # @api private
        def password
          <PASSWORD>
        end

        # @since 0.4.0
        # @api private
        def dump_structure
          system "mysqldump --user=#{username} --no-data --skip-comments --ignore-table=#{database}.#{migrations_table} #{database} > #{schema}"
        end

        # @since 0.4.0
        # @api private
        def load_structure
          system "mysql --user=#{username} #{database} < #{escape(schema)}" if schema.exist?
        end

        # @since 0.4.0
        # @api private
        def dump_migrations_data
          system "mysqldump --user=#{username} --skip-comments #{database} #{migrations_table} >> #{schema}"
        end
      end
    end
  end
end<commit_msg>Specify --host, --port arguments for MySQL CLI tools<commit_after>module Hanami
  module Model
    class Migrator
      # MySQL adapter
      #
      # @since 0.4.0
      # @api private
      class MySQLAdapter < Adapter
        # @since 0.7.0
        # @api private
        PASSWORD = '<PASSWORD>'.freeze

        # @since 1.0.0
        # @api private
        DB_CREATION_ERROR = 'Database creation failed. If the database exists, ' \
                            'then its console may be open. See this issue for more details: ' \
                            'https://github.com/hanami/model/issues/250'.freeze

        # @since 0.4.0
        # @api private
        def create
          new_connection(global: true).run %(CREATE DATABASE `#{database}`;)
        rescue Sequel::DatabaseError => e
          message = if e.message.match(/database exists/) # rubocop:disable Performance/RedundantMatch
                      DB_CREATION_ERROR
                    else
                      e.message
                    end

          raise MigrationError.new(message)
        end

        # @since 0.4.0
        # @api private
        def drop
          new_connection(global: true).run %(DROP DATABASE `#{database}`;)
        rescue Sequel::DatabaseError => e
          message = if e.message.match(/doesn\'t exist/) # rubocop:disable Performance/RedundantMatch
                      "Cannot find database: #{database}"
                    else
                      e.message
                    end

          raise MigrationError.new(message)
        end

        # @since 0.4.0
        # @api private
        def dump
          set_environment_variables
          dump_structure
          dump_migrations_data
        end

        # @since 0.4.0
        # @api private
        def load
          set_environment_variables
          load_structure
        end

        private

        # @since 0.7.0
        # @api private
        def set_environment_variables
          ENV[PASSWORD] = password unless password.nil?
        end

        # @since 0.7.0
        # @api private
        def password
          <PASSWORD>
        end

        # @since 0.4.0
        # @api private
        def dump_structure
          system "mysqldump --host=#{host} --port=#{port} --user=#{username} --no-data --skip-comments --ignore-table=#{database}.#{migrations_table} #{database} > #{schema}"
        end

        # @since 0.4.0
        # @api private
        def load_structure
          system "mysql --host=#{host} --port=#{port} --user=#{username} #{database} < #{escape(schema)}" if schema.exist?
        end

        # @since 0.4.0
        # @api private
        def dump_migrations_data
          system "mysqldump --host=#{host} --port=#{port} --user=#{username} --skip-comments #{database} #{migrations_table} >> #{schema}"
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10079084637388525, 'fraction_numerical': 0.013124684502776375, 'mean_word_length': 1.4985287936107607, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7309086', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1643, 'n_words': 414}