reset-test-db.sh 3.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 25 26
#!/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.
#
############################################################################

DB_CACHE_DIR="common/test/db_cache"

27 28 29
# Ensure the test database exists.
echo "CREATE DATABASE IF NOT EXISTS edxtest;" | mysql -u root

30
# Clear out the test database
31 32 33 34 35 36 37 38 39
#
# 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.
#
# Note that as a side effect, this might render the --serversonly / --testsonly
# paver options useless or broken.
echo "Issuing a reset_db command to the bok_choy MySQL database."
40
./manage.py lms --settings bok_choy reset_db --traceback --noinput
41 42

# If there are cached database schemas/data, load them
43
if [[ -f $DB_CACHE_DIR/bok_choy_schema.sql && -f $DB_CACHE_DIR/bok_choy_migrations_data.sql && -f $DB_CACHE_DIR/bok_choy_data.json ]]; then
44

45
    echo "Found the bok_choy DB cache files. Loading them into the database..."
46
    # Load the schema, then the data (including the migration history)
47
    echo "Loading the schema from the filesystem into the MySQL DB."
48
    mysql -u root edxtest < $DB_CACHE_DIR/bok_choy_schema.sql
49
    echo "Loading the migration data from the filesystem into the MySQL DB."
50
    mysql -u root edxtest < $DB_CACHE_DIR/bok_choy_migrations_data.sql
51
    echo "Loading the fixture data from the filesystem into the MySQL DB."
52 53 54
    ./manage.py lms --settings bok_choy loaddata $DB_CACHE_DIR/bok_choy_data.json

    # Re-run migrations to ensure we are up-to-date
55
    echo "Running the lms migrations on the bok_choy DB."
56
    ./manage.py lms --settings bok_choy migrate --traceback --noinput
57
    echo "Running the cms migrations on the bok_choy DB."
58
    ./manage.py cms --settings bok_choy migrate --traceback --noinput
59 60 61

# Otherwise, update the test database and update the cache
else
62
    echo "Did not find a bok_choy DB cache. Creating a new one from scratch."
63 64 65 66
    # Clean the cache directory
    rm -rf $DB_CACHE_DIR && mkdir -p $DB_CACHE_DIR

    # Re-run migrations on the test database
67
    echo "Issuing a migrate command to the bok_choy MySQL database for the lms django apps."
68
    ./manage.py lms --settings bok_choy migrate --traceback --noinput
69
    echo "Issuing a migrate command to the bok_choy MySQL database for the cms django apps."
70
    ./manage.py cms --settings bok_choy migrate --traceback --noinput
71 72

    # Dump the schema and data to the cache
73
    echo "Using the dumpdata command to save the fixture data to the filesystem."
74 75
    ./manage.py lms --settings bok_choy dumpdata > $DB_CACHE_DIR/bok_choy_data.json
    # dump_data does not dump the django_migrations table so we do it separately.
76
    echo "Saving the django_migrations table of the bok_choy DB to the filesystem."
77
    mysqldump -u root --no-create-info edxtest django_migrations > $DB_CACHE_DIR/bok_choy_migrations_data.sql
78
    echo "Saving the schema of the bok_choy DB to the filesystem."
79
    mysqldump -u root --no-data --skip-comments --skip-dump-date edxtest > $DB_CACHE_DIR/bok_choy_schema.sql
80 81
fi