reset-test-db.sh 4.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#!/usr/bin/env bash

############################################################################
#
#   reset-test-db.sh
#
#   Resets the MySQL test database for the bok-choy acceptance tests.
#
#   If it finds a cached schema and migration history, it will start
#   from the cached version to speed up migrations.
#
#   If no cached database exists, it will create one.  This can be
#   checked into the repo to speed up future tests.
#
#   Note that we do NOT want to re-use the cache between test runs!
#   A newer commit could introduce migrations that do not exist
#   in other commits, which could cause migrations to fail in the other
#   commits.
#
#   For this reason, we always use a cache that was committed to master
#   at the time the branch was created.
#
############################################################################

25 26 27
# Fail fast
set -e

28 29
DB_CACHE_DIR="common/test/db_cache"

30
declare -A databases
31
declare -a database_order
32
databases=(["default"]="edxtest" ["student_module_history"]="student_module_history_test")
33
database_order=("default" "student_module_history")
34

35
# Ensure the test database exists.
36
for db in "${database_order[@]}"; do
37 38 39 40 41 42 43 44
    echo "CREATE DATABASE IF NOT EXISTS ${databases[$db]};" | mysql -u root

    # Clear out the test database
    #
    # We are using the django-extensions's reset_db command which uses "DROP DATABASE" and
    # "CREATE DATABASE" in case the tests are being run in an environment (e.g. devstack
    # or a jenkins worker environment) that already ran tests on another commit that had
    # different migrations that created, dropped, or altered tables.
45
    echo "Issuing a reset_db command to the $db bok_choy MySQL database."
46 47 48
    ./manage.py lms --settings bok_choy reset_db --traceback --noinput --router $db

    # If there are cached database schemas/data, load them
49 50
    if [[ ! -f $DB_CACHE_DIR/bok_choy_schema_$db.sql || ! -f $DB_CACHE_DIR/bok_choy_data_$db.json || ! -f $DB_CACHE_DIR/bok_choy_migrations_data_$db.sql ]]; then
        echo "Missing $DB_CACHE_DIR/bok_choy_schema_$db.sql or $DB_CACHE_DIR/bok_choy_data_$db.json, or $DB_CACHE_DIR/bok_choy_migrations_data_$db.sql rebuilding cache"
51 52 53 54 55
        REBUILD_CACHE=true
    fi

done

56
# If there are cached database schemas/data, load them
57
if [[ -z $REBUILD_CACHE ]]; then
58

59
    echo "Found the bok_choy DB cache files. Loading them into the database..."
60

61
    for db in "${database_order[@]}"; do
62
        # Load the schema, then the data (including the migration history)
63
        echo "Loading the schema from the filesystem into the $db MySQL DB."
64
        mysql -u root "${databases["$db"]}" < $DB_CACHE_DIR/bok_choy_schema_$db.sql
65
        echo "Loading the fixture data from the filesystem into the $db MySQL DB."
66 67
        ./manage.py lms --settings bok_choy loaddata --database $db $DB_CACHE_DIR/bok_choy_data_$db.json

68 69 70 71 72 73 74 75 76
        # Migrations are stored in the default database
        echo "Loading the migration data from the filesystem into the $db MySQL DB."
        mysql -u root "${databases["$db"]}" < $DB_CACHE_DIR/bok_choy_migrations_data_$db.sql

        # Re-run migrations to ensure we are up-to-date
        echo "Running the lms migrations on the $db bok_choy DB."
        ./manage.py lms --settings bok_choy migrate --database $db --traceback --noinput
        echo "Running the cms migrations on the $db bok_choy DB."
        ./manage.py cms --settings bok_choy migrate --database $db --traceback --noinput
77

78
    done
79 80 81

# Otherwise, update the test database and update the cache
else
82
    echo "Did not find a bok_choy DB cache. Creating a new one from scratch."
83
    # Clean the cache directory
84
    mkdir -p $DB_CACHE_DIR && rm -f $DB_CACHE_DIR/bok_choy*
85

86 87 88 89 90 91
    for db in "${database_order[@]}"; do
        # Re-run migrations on the test database
        echo "Issuing a migrate command to the $db bok_choy MySQL database for the lms django apps."
        ./manage.py lms --settings bok_choy migrate --database $db --traceback --noinput
        echo "Issuing a migrate command to the $db bok_choy MySQL database for the cms django apps."
        ./manage.py cms --settings bok_choy migrate --database $db --traceback --noinput
92

93
        # Dump the schema and data to the cache
94
        echo "Using the dumpdata command to save the $db fixture data to the filesystem."
95 96
        ./manage.py lms --settings bok_choy dumpdata --database $db > $DB_CACHE_DIR/bok_choy_data_$db.json --exclude=api_admin.Catalog
        echo "Saving the schema of the $db bok_choy DB to the filesystem."
97 98
        mysqldump -u root --no-data --skip-comments --skip-dump-date "${databases[$db]}" > $DB_CACHE_DIR/bok_choy_schema_$db.sql

99 100 101 102
        # dump_data does not dump the django_migrations table so we do it separately.
        echo "Saving the django_migrations table of the $db bok_choy DB to the filesystem."
        mysqldump -u root --no-create-info "${databases["$db"]}" django_migrations > $DB_CACHE_DIR/bok_choy_migrations_data_$db.sql
    done
103
fi