Sha256: 39073846e4ce4db4061c30eaa87ab3f335d606e7560da639d7937e2c95678b9c

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require "rake"

module Backhoe
  class Load < Struct.new(:database, :file_path, :drop_and_create)
    include Rake::DSL

    def call
      case database.adapter
      when "mysql2", "trilogy"
        sh mysql_command
      when "postgresql"
        sh psql_command
      else
        raise "don't know how to load #{database.adapter}"
      end
    end

    private

    def mysql_command
      cmd = "#{cat} #{file_path} | "
      cmd += if drop_and_create
        "#{pipe} | #{mysql} #{database.to_mysql_options}"
      else
        "#{mysql} #{database.to_mysql_options} #{database.name}"
      end
    end

    def psql_command
      cmd = "#{cat} #{file_path} | "
      if drop_and_create
        cmd = "dropdb -f #{database.name}; createdb #{database.name}; #{cmd}"
      end
      cmd += "#{psql} -P pager=off -q -d#{database.name}"
      cmd
    end

    def cat
      file_path =~ /\.gz$/ ? "zcat" : "cat"
    end

    def pipe
      if drop_and_create
        "(echo -n '#{<<~SQL}' && cat)"
          DROP DATABASE IF EXISTS #{database.name};
          CREATE DATABASE #{database.name};
          USE #{database.name};
        SQL
      else
        "cat"
      end
    end

    def mysql
      cmd = `which mysql`.strip
      raise RuntimeError, "Cannot find mysql." if cmd.blank?
      cmd
    end

    def psql
      cmd = `which psql`.strip
      raise RuntimeError, "Cannot find psql." if cmd.blank?
      cmd
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
backhoe-0.9.0 lib/backhoe/load.rb
backhoe-0.8.3 lib/backhoe/load.rb
backhoe-0.8.2 lib/backhoe/load.rb