Skip to content

Instantly share code, notes, and snippets.

Created August 16, 2010 15:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/527117 to your computer and use it in GitHub Desktop.
Save anonymous/527117 to your computer and use it in GitHub Desktop.
desc "creates database & database user"
task :create_database do
set :root_password, Capistrano::CLI.password_prompt("MySQL root password: ")
set :db_user, Capistrano::CLI.ui.ask("Application database user: ")
set :db_pass, Capistrano::CLI.password_prompt("Password: ")
set :db_name, Capistrano::CLI.ui.ask("Database name: ")
run "mysql --user=root --password=#{root_password} -e \"CREATE DATABASE IF NOT EXISTS #{db_name}\""
run "mysql --user=root --password=#{root_password} -e \"GRANT ALL PRIVILEGES ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_pass}' WITH GRANT OPTION\""
end
@JohnSmall
Copy link

what's the problem with running rake db:create:all ?

@matthewcase
Copy link

You can't run db:create:all if the code isn't already deployed. The problem I'm running into is I'm deploying to a fresh Vagrant box but it's dying because the DB isn't already created. However when the deploy fails the code is rolled back so there's no way for me to manually do "rake db:create:all".

@joost
Copy link

joost commented Feb 13, 2014

Capistrano 3 version:

namespace :deploy do

  desc "Create database and database user"
  task :create_mysql_database do
    ask :db_root_password, ''
    ask :db_name, fetch(:application)
    ask :db_user, 'deploy'
    ask :db_pass, ''

    on primary fetch(:migration_role) do
      execute "mysql --user=root --password=#{fetch(:db_root_password)} -e \"CREATE DATABASE IF NOT EXISTS #{fetch(:db_name)}\""
      execute "mysql --user=root --password=#{fetch(:db_root_password)} -e \"GRANT ALL PRIVILEGES ON #{fetch(:db_name)}.* TO '#{fetch(:db_user)}'@'localhost' IDENTIFIED BY '#{fetch(:db_pass)}' WITH GRANT OPTION\""
    end
  end

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment