The Decider said on Fri Dec 21 10:59:14 +0000 2007 | permalink
Tagged: capistrano database

create your db when 'cap deploy:setup'

Create a sql file that creates your db and user. I put mine in RAILS_ROOT/bin/create_db_and_user.sql

CREATE DATABASE IF NOT EXISTS `residenthq`;
CREATE DATABASE IF NOT EXISTS `residenthq_development`;
CREATE DATABASE IF NOT EXISTS `residenthq_test`;
GRANT ALL PRIVILEGES ON residenthq.* TO 'residenthq'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON residenthq_development.* TO 'residenthq'@'localhost' IDENTIFIED BY password';
GRANT ALL PRIVILEGES ON residenthq_test.* TO 'residenthq'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

in config/deploy.rb try:

after "deploy:setup", 'create_db'
@mysql_password_needed = true

desc "updload the sql to create the db and user" 
task :create_db do
  put File.read("#{File.dirname(__FILE__)}/../bin/create_db_and_user.sql"), "#{shared_path}/system/create_db_and_user.sql", :mode => 0644
  cmd = "cat #{shared_path}/system/create_db_and_user.sql | mysql -u root" 
  cmd += ' --password=roots_password' if @mysql_password_needed
  run "#{cmd}" 
  run "rm #{shared_path}/system/create_db_and_user.sql" 
end