gitlab.com/beacon-software/gadget@v0.0.0-20181217202115-54565ea1ed5e/scripts/initialize_db.sh (about)

     1  #/bin/bash
     2  # This is to initialize a new database for tests
     3  
     4  EXECUTE=true
     5  if [ -z "$DBUSER" ]; then
     6  	#default to not verbose if not set.
     7  	echo "DB USER NOT SPECIFIED SETTING TO root" >&2
     8  	DBUSER="root"
     9  fi
    10  
    11  
    12  while getopts ":?ns" opt; do
    13  	case $opt in
    14  		n)
    15  			EXECUTE=false
    16  			;;
    17  		\?)
    18  			echo "example:"
    19  			echo "  > initialize_db.sh"
    20  			echo "options:"
    21  			echo "  -n test mode. only displays potential changes"
    22  			exit 1
    23  			;;
    24  	esac
    25  done
    26  
    27  shift $((OPTIND-1))
    28  
    29  db='test'
    30  prefix='TST'
    31  passwd=$db
    32  
    33  # Created the regular and test databases
    34  echo "CREATE DATABASE IF NOT EXISTS $db;
    35  CREATE USER IF NOT EXISTS '$db'@'%' IDENTIFIED BY '$passwd';
    36  GRANT ALL PRIVILEGES ON $db.* TO '$db'@'%';
    37  FLUSH PRIVILEGES;" > /tmp/$db.sql
    38  
    39  database_url="${prefix}_DATABASE_URL"
    40  
    41  # echo the ENV commands, do these before the exec so they can be added to the bash profile file even if
    42  # the databases already exist.
    43  echo "export ${database_url}='${db}:${passwd}@tcp(localhost:3306)/${db}?parseTime=true&charset=utf8mb4'"
    44  
    45  if $EXECUTE; then
    46  	mysql -u ${DBUSER} < /tmp/$db.sql || exit 1;
    47  else
    48  	cat /tmp/$db.sql
    49  fi