Skip to content

Instantly share code, notes, and snippets.

@bsa7
Created February 12, 2016 12:38
Show Gist options
  • Save bsa7/c7c76f778b73ee7daa72 to your computer and use it in GitHub Desktop.
Save bsa7/c7c76f778b73ee7daa72 to your computer and use it in GitHub Desktop.
bash script for create Rails database and user, using settings from config/database.yml file
#!/bin/bash
if [ ! -f config/database.yml ]
then
echo -e "\e[0;32m error:\e[0m"
echo "put me in root of Rails app."
echo "and define your config/database.yml file"
exit
fi
function dbconfig_parse()
{
local res=`cat config/database.yml | grep "$1: "| grep -oh -P '(?<=:).+$' | grep -oh -P '[^'"'"']+'`
echo $res
}
echo -e "\e[0;32m database and user will be created:\e[0m"
echo -e "\e[0;32m database name: \033[0m $(dbconfig_parse database)"
echo -e "\e[0;32m user name: \033[0m $(dbconfig_parse username)"
echo -e "\e[0;32m password: \033[0m $(dbconfig_parse password)"
appname=`cat env_production`
database_name=$(dbconfig_parse database)
base_user=$(dbconfig_parse username)
base_password=$(dbconfig_parse password)
mysql -u root -p -e "
create database $database_name CHARACTER SET utf8;
create user '$base_user'@'localhost' identified by '$database_name';
set password for '$base_user'@'localhost' = password('$base_password');
grant all privileges on $base_user.* to $database_name@localhost;
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment