Skip to content

Instantly share code, notes, and snippets.

@sbellity
Created June 3, 2010 08:46
Show Gist options
  • Save sbellity/423655 to your computer and use it in GitHub Desktop.
Save sbellity/423655 to your computer and use it in GitHub Desktop.
require "pp"
AppEnv = ENV["APP_ENV"] || "development"
DBConfig = YAML::load(open("config/database.yml"))[AppEnv]
def mysql_cmd cmd="mysql", *args
opts = {
:u => DBConfig["username"],
:h => DBConfig["host"],
:p => DBConfig["password"]
}.inject([]) { |c, o| o[1].nil? ? c : c << "-#{o[0]}#{o[1]}" }.join(" ")
"`which #{cmd}` #{opts} #{args.join(" ")}"
end
def mysqldump out=nil
filename = out || "./#{DBConfig["database"]}-#{Time.now.strftime('%Y%m%d%H%M%S')}.sql"
mysql_cmd "mysqldump", "#{DBConfig["database"]} >> db/#{filename}"
end
def mysqldrop
mysql_cmd "mysqladmin", "-f drop #{DBConfig["database"]}"
end
def mysqlcreate
mysql_cmd "mysqladmin", "create #{DBConfig["database"]}"
end
def mysqlload file
mysql_cmd "mysql", "#{DBConfig["database"]} < #{file}"
end
namespace :db do
task :migrate do
cmd = "cd library/_data/ && liquibase --driver=com.#{DBConfig["adapter"]}.jdbc.Driver --username=#{DBConfig["username"]} #{"--password=#{DBConfig["password"]}" unless DBConfig["password"].nil?} --url=jdbc:#{DBConfig["adapter"]}://#{DBConfig["host"]}/#{DBConfig["database"]} --changeLogFile=#{DBConfig["changelog"] || "schema.xml"} --logLevel=#{DBConfig["log_level"] || "warning"} update"
system cmd
end
task :dump do
system mysqldump
end
task :drop do
system mysqldrop
end
task :create do
system mysqlcreate
end
task :load, :file do |t, args|
args.with_defaults(:file => "db/bootstrap.sql")
system mysqlload args.file if File.exist? args.file
end
task :reset => [:drop, :create, :load]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment