<commit_before>#!/bin/bash
set -e

LOCAL_DUMP="/app/scripts/prod_db_dump.sql"

DBURI=$1
MAGIC_WORD=$2

# This operation is destructive, we must ensure we never run it in prod
echo "Performing sanity check..."
if [[ $ENV != "stage" || $MAGIC_WORD != "destroythedatabase" ]]
then
  echo "Not in stage environment (found \"${ENV}\" in \$ENV, and \"${MAGIC_WORD}\" as the magic word), cannot proceed"
  exit 1
fi

# Because the "mysql" command doesn't accept URIs...
username=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.netloc.split('@')[0].split(':')[0]")
password=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.netloc.split('@')[0].split(':')[1]")
host=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.netloc.split('@')[-1]")
database=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.path.lstrip('/')")

echo -n "Proceeding with reset of $database on $host in "
for i in `seq 10 -1 1`; do echo -n $i... && sleep 1; done
echo

# Because the production database dumps contain no permissions, we need to
# make sure we maintain stage's permissions, otherwise automation will not
# work after the reset has been completed.
echo "Backing up existing permissions, roles, and required signoffs..."
mysqldump --skip-add-drop-table --no-create-info -h "$host" -u "$username" --password="$password" "$database" permissions user_roles product_req_signoffs permissions_req_signoffs > backup.sql

echo "Download prod dump..."
python scripts/get-prod-db-dump.py

echo "Overriding current database with production dump..."
cat $LOCAL_DUMP | mysql -h "$host" -u "$username" --password="$password" "$database"

echo "Upgrading database to the latest version..."
python scripts/manage-db.py -d $DBURI upgrade

echo "Re-adding permissions, roles, and required signoffs..."
cat backup.sql | mysql -h "$host" -u "$username" --password="$password" "$database"
<commit_msg>Add debug info to stage db reset script.<commit_after>#!/bin/bash
set -e
set -x

LOCAL_DUMP="/app/scripts/prod_db_dump.sql"

DBURI=$1
MAGIC_WORD=$2

# This operation is destructive, we must ensure we never run it in prod
echo "Performing sanity check..."
if [[ $ENV != "stage" || $MAGIC_WORD != "destroythedatabase" ]]
then
  echo "Not in stage environment (found \"${ENV}\" in \$ENV, and \"${MAGIC_WORD}\" as the magic word), cannot proceed"
  exit 1
fi

# Because the "mysql" command doesn't accept URIs...
username=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.netloc.split('@')[0].split(':')[0]")
password=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.netloc.split('@')[0].split(':')[1]")
host=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.netloc.split('@')[-1]")
database=$(python -c "import urlparse; result = urlparse.urlparse('${DBURI}'); print result.path.lstrip('/')")

echo -n "Proceeding with reset of $database on $host in "
for i in `seq 10 -1 1`; do echo -n $i... && sleep 1; done
echo

# Because the production database dumps contain no permissions, we need to
# make sure we maintain stage's permissions, otherwise automation will not
# work after the reset has been completed.
echo "Backing up existing permissions, roles, and required signoffs..."
mysqldump --skip-add-drop-table --no-create-info -h "$host" -u "$username" --password="$password" "$database" permissions user_roles product_req_signoffs permissions_req_signoffs > backup.sql
cat backup.sql

echo "Download prod dump..."
python scripts/get-prod-db-dump.py

echo "Overriding current database with production dump..."
cat $LOCAL_DUMP | mysql -h "$host" -u "$username" --password="$password" "$database"

echo "Upgrading database to the latest version..."
python scripts/manage-db.py -d $DBURI upgrade

echo "Re-adding permissions, roles, and required signoffs..."
cat backup.sql
cat backup.sql | mysql -h "$host" -u "$username" --password="<PASSWORD>" "$database"
mysqldump --skip-add-drop-table --no-create-info -h "$host" -u "$username" --password="$password" "$database" permissions user_roles product_req_signoffs permissions_req_signoffs

{'fraction_non_alphanumeric': 0.16067203028868907, 'fraction_numerical': 0.006152389966871746, 'mean_word_length': 4.838397790055248, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1748895', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1297, 'n_words': 456}