...

Text file src/github.com/letsencrypt/boulder/test/create_db.sh

Documentation: github.com/letsencrypt/boulder/test

     1#!/usr/bin/env bash
     2set -o errexit
     3cd $(dirname $0)/..
     4
     5
     6# If you modify DBS or ENVS, you must also modify the corresponding keys in
     7# sa/db/dbconfig.yml, see: https://github.com/rubenv/sql-migrate#readme
     8
     9DBS="boulder_sa
    10incidents_sa"
    11
    12ENVS="test
    13integration"
    14
    15# /path/to/boulder/repo
    16root_dir=$(dirname $(dirname $(readlink -f "$0")))
    17
    18# posix compliant escape sequence
    19esc=$'\033'"["
    20res="${esc}0m"
    21
    22function print_heading() {
    23  echo
    24  # newline + bold magenta
    25  echo -e "${esc}0;34;1m${1}${res}"
    26}
    27
    28function exit_err() {
    29  if [ ! -z "$1" ]
    30  then
    31    echo $1 > /dev/stderr
    32  fi
    33  exit 1
    34}
    35
    36function create_empty_db() {
    37  local db="${1}"
    38  local dbconn="${2}"
    39  create_script="drop database if exists \`${db}\`; create database if not exists \`${db}\`;"
    40  mysql ${dbconn} -e "${create_script}" || exit_err "unable to create ${db}"
    41}
    42
    43# set db connection for if running in a separate container or not
    44dbconn="-u root"
    45if [[ $MYSQL_CONTAINER ]]
    46then
    47	dbconn="-u root -h boulder-mysql --port 3306"
    48fi
    49
    50# MariaDB sets the default binlog_format to STATEMENT,
    51# which causes warnings that fail tests. Instead set it
    52# to the format we use in production, MIXED.
    53mysql ${dbconn} -e "SET GLOBAL binlog_format = 'MIXED';"
    54
    55# MariaDB sets the default @@max_connections value to 100. The SA alone is
    56# configured to use up to 100 connections. We increase the max connections here
    57# to give headroom for other components (ocsp-responder for example).
    58mysql ${dbconn} -e "SET GLOBAL max_connections = 500;"
    59
    60for db in $DBS; do
    61  for env in $ENVS; do
    62    dbname="${db}_${env}"
    63    print_heading "${dbname}"
    64    if mysql ${dbconn} -e 'show databases;' | grep "${dbname}" > /dev/null; then
    65      echo "Already exists - skipping create"
    66    else
    67      echo "Doesn't exist - creating"
    68      create_empty_db "${dbname}" "${dbconn}"
    69    fi
    70
    71    if [[ "${BOULDER_CONFIG_DIR}" == "test/config-next" ]]
    72    then
    73      dbpath="./sa/db-next"
    74    else
    75      dbpath="./sa/db"
    76    fi
    77
    78    # sql-migrate will default to ./dbconfig.yml and treat all configured dirs
    79    # as relative.
    80    cd "${dbpath}"
    81    r=`sql-migrate up -env="${dbname}" | xargs -0 echo`
    82    if [[ "${r}" == "Migration failed"* ]]
    83    then
    84      echo "Migration failed - dropping and recreating"
    85      create_empty_db "${dbname}" "${dbconn}"
    86      sql-migrate up -env="${dbname}" || exit_err "Migration failed after dropping and recreating"
    87    else
    88      echo "${r}"
    89    fi
    90
    91    USERS_SQL="../db-users/${db}.sql"
    92    if [[ ${MYSQL_CONTAINER} ]]
    93    then
    94      sed -e "s/'localhost'/'%'/g" < ${USERS_SQL} | \
    95        mysql ${dbconn} -D "${dbname}" -f || exit_err "Unable to add users from ${USERS_SQL}"
    96    else
    97      sed -e "s/'localhost'/'127.%'/g" < $USERS_SQL | \
    98        mysql ${dbconn} -D "${dbname}" -f < $USERS_SQL || exit_err "Unable to add users from ${USERS_SQL}"
    99    fi
   100    echo "Added users from ${USERS_SQL}"
   101    
   102    # return to the root directory
   103    cd "${root_dir}"
   104  done
   105done
   106
   107echo
   108echo "database setup complete"

View as plain text