This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "dotenv/tasks" | |
require "uri" | |
namespace :development do | |
desc "run server" | |
task :server => :dotenv do | |
sh "php -S localhost:9292 " | |
end | |
desc "open a local database shell" | |
task :local_database_shell => :dotenv do | |
sh "mysql #{local_connection_options}" | |
end | |
end | |
namespace :production do | |
desc "open a shell to the remote database" | |
task :remote_database_shell do | |
sh "mysql #{remote_connection_options}" | |
end | |
desc "backup remote database" | |
task :backup_db => :dotenv do | |
datestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S") | |
backup_file = File.join(ENV["BACKUP_DIR"], "#{datestamp}_#{remote_database_options[:database]}.sql") | |
Rake::Task["deploy:dump_remote"].invoke(backup_file) | |
sh "gzip -9 #{backup_file}" | |
dir = Dir.new(ENV["BACKUP_DIR"]) | |
puts "Created backup: #{backup_file}.gz" | |
end | |
end | |
namespace :deploy do | |
task :dump_local => :dotenv do | |
sh "mysqldump #{local_connection_options} > local_database_dump.sql" | |
end | |
task :dump_remote, :dump_file do |t, args| | |
dump_file = args[:dump_file] || "./remote_database_dump.sql" | |
sh "mysqldump #{remote_connection_options} > #{dump_file}" | |
end | |
desc "push to production" | |
task :push do | |
sh "git push heroku production:master" | |
end | |
desc "dump remote database and import into local" | |
task :remote_in_local => [:dotenv, :dump_remote] do | |
puts "Are you sure? (y/n)" | |
return unless STDIN.gets.strip == "y" | |
sh "mysql #{local_connection_options} < remote_database_dump.sql" | |
end | |
desc "dump local database and import into remote" | |
task :local_in_remote => :dump_local do | |
puts "Are you sure? (y/n)" | |
return unless STDIN.gets.strip == "y" | |
sh "mysql #{remote_connection_options} < local_database_dump.sql" | |
end | |
end | |
def local_connection_options | |
uri = URI.parse(ENV["DATABASE_URL"]) | |
options = "-u #{uri.user} " | |
options += "-p #{uri.password} " unless uri.password.empty? | |
database = uri.path[1..-1] | |
options += "#{database} " | |
options | |
end | |
def remote_database_options | |
remote_uri = `heroku config:get DATABASE_URL`.strip | |
uri = URI.parse(remote_uri) | |
{ user: uri.user, | |
host: uri.host, | |
password: uri.password, | |
database: uri.path[1..-1] } | |
end | |
def remote_connection_options | |
connection_options = remote_database_options | |
options = "-u #{connection_options[:user]} " | |
options += "-h #{connection_options[:host]} " | |
options += "-p#{connection_options[:password]} " | |
options += "#{connection_options[:database]} " | |
options | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment