Created
February 12, 2016 12:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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