Sha256: 194d986a39e7f1d0801a638bc45ae8d4f689d17109602269a4a2953985ebf551

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'shellwords'

module SequelRails
  module Storage
    class Mysql < Abstract
      def _create
        execute("CREATE DATABASE IF NOT EXISTS `#{database}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
      end

      def _drop
        execute("DROP DATABASE IF EXISTS `#{database}`")
      end

      def _dump filename
        commands = %w(mysqldump --no-data)
        commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
        commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
        commands << "--host=#{host}" unless host.blank?
        commands << "--result-file" << filename
        commands << database
        system(*commands)
      end

      def _load filename
        commands = %w(mysql)
        commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
        commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
        commands << "--host=#{host}" unless host.blank?
        commands << '--execute' << %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}
        commands << '--database' << database
        system(*commands)
      end

      private

      def execute(statement)
        commands = ["mysql"]
        commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
        commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
        commands << "--host=#{host}" unless host.blank?
        commands << "-e" << statement
        system(*commands)
      end

      def collation
        @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sequel-rails-0.5.0 lib/sequel_rails/storage/mysql.rb
sequel-rails-0.4.4 lib/sequel_rails/storage/mysql.rb