Sha256: 836d1b1b007f7082b763ee51b6f48394a698627f9102835f8749e65fd047f97a

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

require 'yaml'
require 'capistrano'
require 'capistrano/cli'

module MySQLMethods
  
  def execute(sql, user)
    run "mysql --user=#{user} -p --execute=\"#{sql}\"" do |channel, stream, data|
      handle_mysql_password(user, channel, stream, data)
    end
  end
  
  private
  def handle_mysql_password(user, channel, stream, data)
    logger.info data, "[database on #{channel[:host]} asked for password]"
    if data =~ /^Enter password:/
      pass = Capistrano::CLI.password_prompt "Enter database password for '#{user}':"
      channel.send_data "#{pass}\n" 
    end
  end
end

Capistrano.plugin :mysql, MySQLMethods

Capistrano.configuration(:must_exist).load do
  
  set :mysql_admin, nil
  
  desc "Execute MySQL statements using --execute option. Set the 'sql' variable."
  task :execute_mysql, :roles => :db, :only => { :primary => true } do
    set_mysql_admin
    mysql.execute sql, mysql_admin
  end
  
  desc "Create MySQL database and user based on config/database.yml"
  task :setup_mysql, :roles => :db, :only => { :primary => true } do
    # on_rollback {}
    
    set_mysql_admin
    read_config
    
    sql = "CREATE DATABASE #{db_name};"
    sql += "GRANT ALL PRIVILEGES ON #{db_name}.* TO #{db_user}@localhost IDENTIFIED BY '#{db_password}';"  
    mysql.execute sql, mysql_admin
  end
  
  def read_config
    db_config = YAML.load_file('config/database.yml')
    set :db_user, db_config[rails_env]["username"]
    set :db_password, db_config[rails_env]["password"] 
    set :db_name, db_config[rails_env]["database"]
  end
  
  def set_mysql_admin
    set :mysql_admin, user unless mysql_admin
  end
  
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
mmmultiworks-0.1.0 lib/mmmultiworks/recipes/mysql.rb
mmmultiworks-0.2.0 lib/mmmultiworks/recipes/mysql.rb
railsmachine-0.1.1 lib/railsmachine/recipes/mysql.rb
railsmachine-0.1.0 lib/railsmachine/recipes/mysql.rb
railsmachine-0.1.2 lib/railsmachine/recipes/mysql.rb