Skip to content

Instantly share code, notes, and snippets.

@aergonaut
Last active December 26, 2015 09:39
Show Gist options
  • Save aergonaut/7131493 to your computer and use it in GitHub Desktop.
Save aergonaut/7131493 to your computer and use it in GitHub Desktop.
namespace :db do
namespace :sql do
desc "Load database from SQL dump"
task :load => [:environment, :reset] do
ARGV.shift
file = ARGV.shift
name_and_extensions = file.split(".")
name = name_and_extensions.shift
extensions = name_and_extensions
processed_io = extensions.reverse.reduce(File.open(file)) do |io, ext|
case ext
when /gz/
io = Zlib::GzipReader.new(io)
when /tar/
tar = Gem::Package::TarReader.new(io)
tar.rewind
contents = ""
tar.each { |e| contents << e.read }
io = StringIO.new(contents)
end
io
end
config = Rails.configuration.database_configuration[Rails.env]
Tempfile.new(name) do |tmpfile|
tmpfile << processed_io.read
command = ["mysql"]
command.concat(["-u", config["username"]) if config["username"]
command << "--password=#{config['password']}" if config["password"]
command << config["database"]
command.concat(["<", tmpfile])
sh command.join(" ")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment