Skip to content

Commit

Permalink
Migrations up-to-date check no longer requires Redis
Browse files Browse the repository at this point in the history
Avoid relying on Redis for up-to-date checks of
applied DB migrations by instead inspecting the
results of the 'showmigrations' listing command
and checking for the presence of the unchecked
checkbox ASCII representation '[ ]'.

This change makes the DB migration status check
more self-contained and less fragile.
  • Loading branch information
jmurty committed Jun 5, 2018
1 parent 32a62e8 commit 177ea18
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions ixc_django_docker/bin/migrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,35 @@ if [[ DJANGO_VERSION_LESS_THAN_1_7 == 'True' ]]; then
fi

if [[ DJANGO_VERSION_LESS_THAN_1_10 == 'True' ]]; then
manage.py migrate --list > "$DIR/migrate.txt"
manage.py migrate --list > "$DIR/migrate.txt" 2>/dev/null
else
manage.py showmigrations > "$DIR/migrate.txt"
manage.py showmigrations > "$DIR/migrate.txt" 2>/dev/null
fi

# Is local listing of migrations the same as one cached in Redis
# (i.e. as has already been completed and cached by another server instance)?
if ! redis-cache.py -v -x match ixc-django-docker:migrate-list < "$DIR/migrate.txt"; then
echo 'Migrations are out of date.'
# Rely on the formatted text output of Django's migration listing to tell us
# whether there are any pending migrations, which are flagged with an empty
# ASCII checkbox versus a checked one. Grep for the unapplied migration '[ ]'
# text as a flag for when migrations need to be applied, and when grep returns
# an **error** status we know there are **no unapplied migrations**
#
# Here is an example of the output of 'showmigrations' etc:
#
# wagtailusers
# [X] 0001_initial
# [ ] 0002_add_verbose_name_on_userprofile
set +e
grep '\[ \]' "$DIR/migrate.txt" > /dev/null
HAS_ALL_MIGRATIONS_APPLIED=$?
set -e

if [[ HAS_ALL_MIGRATIONS_APPLIED -ne 0 ]]; then
echo 'Migrations are up-to-date - no migration listing items contain "[ ]"'
else
echo 'Migrations are out of date - one or more migration listing items contain "[ ]"'

# Skip initial migration if all tables created by the initial migration
# already exist.
if [[ $(python -c 'import django; print(django.get_version());') < 1.7 ]]; then
if [[ DJANGO_VERSION_LESS_THAN_1_7 == 'True' ]]; then
manage.py migrate --noinput # South has no `--fake-initial` option
else
manage.py migrate --fake-initial --noinput
fi

if [[ DJANGO_VERSION_LESS_THAN_1_10 == 'True' ]]; then
manage.py migrate --list > "$DIR/migrate.txt"
else
manage.py showmigrations > "$DIR/migrate.txt"
fi

# Cache listing of up-to-date migrations
redis-cache.py -vv -x set ixc-django-docker:migrate-list < "$DIR/migrate.txt"
fi

0 comments on commit 177ea18

Please sign in to comment.