github.com/letsencrypt/boulder@v0.20251208.0/test/create_db.sh (about)

     1  #!/usr/bin/env bash
     2  set -o errexit
     3  cd $(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  
     9  DBS="boulder_sa
    10  incidents_sa"
    11  
    12  ENVS="test
    13  integration"
    14  
    15  # /path/to/boulder/repo
    16  root_dir=$(dirname $(dirname $(readlink -f "$0")))
    17  
    18  # posix compliant escape sequence
    19  esc=$'\033'"["
    20  res="${esc}0m"
    21  
    22  function print_heading() {
    23    echo
    24    # newline + bold magenta
    25    echo -e "${esc}0;34;1m${1}${res}"
    26  }
    27  
    28  function exit_err() {
    29    if [ ! -z "$1" ]
    30    then
    31      echo $1 > /dev/stderr
    32    fi
    33    exit 1
    34  }
    35  
    36  function 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
    44  dbconn="-u root"
    45  if [[ $MYSQL_CONTAINER ]]
    46  then
    47    dbconn="-u root -h ${DB_HOST} --port ${DB_PORT}"
    48  fi
    49  
    50  if ! mysql ${dbconn} -e "select 1" >/dev/null 2>&1; then
    51    exit_err "unable to connect to ${DB_HOST}:${DB_PORT}"
    52  fi
    53  
    54  if [[ ${SKIP_CREATE} -eq 0 ]]
    55  then
    56    # MariaDB sets the default binlog_format to STATEMENT,
    57    # which causes warnings that fail tests. Instead set it
    58    # to the format we use in production, MIXED.
    59    mysql ${dbconn} -e "SET GLOBAL binlog_format = 'MIXED';"
    60  
    61    # MariaDB sets the default @@max_connections value to 100. The SA alone is
    62    # configured to use up to 100 connections. We increase the max connections here
    63    # to give headroom for other components.
    64    mysql ${dbconn} -e "SET GLOBAL max_connections = 500;"
    65  fi
    66  
    67  for db in $DBS; do
    68    for env in $ENVS; do
    69      dbname="${db}_${env}"
    70      print_heading "${dbname}"
    71      if [[ ${SKIP_CREATE} -eq 0 ]]
    72      then
    73        if mysql ${dbconn} -e 'show databases;' | grep -q "${dbname}"
    74        then
    75          echo "Already exists - skipping create"
    76        else
    77          echo "Doesn't exist - creating"
    78          create_empty_db "${dbname}" "${dbconn}"
    79        fi
    80      else
    81        echo "Skipping database create for ${dbname}"
    82      fi
    83  
    84      if [[ "${BOULDER_CONFIG_DIR}" == "test/config-next" ]]
    85      then
    86        dbpath="./sa/db-next"
    87      else
    88        dbpath="./sa/db"
    89      fi
    90  
    91      # sql-migrate will default to ./dbconfig.yml and treat all configured dirs
    92      # as relative.
    93      cd "${dbpath}"
    94      r=`sql-migrate up -config="${DB_CONFIG_FILE}" -env="${dbname}" | xargs -0 echo`
    95      if [[ "${r}" == "Migration failed"* ]]
    96      then
    97        echo "Migration failed - dropping and recreating"
    98        create_empty_db "${dbname}" "${dbconn}"
    99        sql-migrate up -config="${DB_CONFIG_FILE}" -env="${dbname}" || exit_err "Migration failed after dropping and recreating"
   100      else
   101        echo "${r}"
   102      fi
   103  
   104      USERS_SQL="../db-users/${db}.sql"
   105      if [[ ${SKIP_USERS} -eq 1 ]]
   106      then
   107        echo "Skipping user grants for ${dbname}"
   108      else
   109        if [[ $MYSQL_CONTAINER ]]
   110        then
   111          sed -e "s/'localhost'/'%'/g" < "${USERS_SQL}" | \
   112            mysql ${dbconn} -D "${dbname}" -f || exit_err "Unable to add users from ${USERS_SQL}"
   113        else
   114          sed -e "s/'localhost'/'127.%'/g" < "${USERS_SQL}" | \
   115            mysql ${dbconn} -D "${dbname}" -f || exit_err "Unable to add users from ${USERS_SQL}"
   116        fi
   117        echo "Added users from ${USERS_SQL}"
   118      fi
   119  
   120      # return to the root directory
   121      cd "${root_dir}"
   122    done
   123  done
   124  
   125  echo
   126  echo "database setup complete"