Skip to content

Commit

Permalink
Fix migration atomicity by using django-dbbackup to backup and rest…
Browse files Browse the repository at this point in the history
…ore database (#2094)

* fix: use `django-dbbackup` to prevent DB malformation during migration
* fix: issues in viewer; use a fixed hostname for consistency in backup names
  • Loading branch information
nicomiguelino authored Oct 15, 2024
1 parent 30e5568 commit 211b900
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
10 changes: 6 additions & 4 deletions anthias_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dbbackup',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -108,10 +109,6 @@
else '/data/.screenly/screenly.db'
),
},
'backup': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/data/.screenly/backup.db',
},
}


Expand Down Expand Up @@ -179,3 +176,8 @@
'TITLE': 'Anthias API',
'VERSION': '1.2.0',
}

# `django-dbbackup` settings
DBBACKUP_STORAGE = 'django.core.files.storage.FileSystemStorage'
DBBACKUP_STORAGE_OPTIONS = {'location': '/data/.screenly/backups'}
DBBACKUP_HOSTNAME = 'anthias'
11 changes: 6 additions & 5 deletions bin/start_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ENVIRONMENT=${ENVIRONMENT:-production}
mkdir -p \
/data/.config \
/data/.screenly \
/data/.screenly/backups \
/data/screenly_assets

cp -n /usr/src/app/ansible/roles/screenly/files/screenly.conf /data/.screenly/screenly.conf
Expand All @@ -16,12 +17,12 @@ echo "Running migration..."
# database is not left in an inconsistent state if the migration fails.

if [ -f /data/.screenly/screenly.db ]; then
cp /data/.screenly/screenly.db /data/.screenly/backup.db && \
cp /data/.screenly/screenly.db /data/.screenly/screenly.db.bak && \
./manage.py migrate --fake-initial --database=backup && \
mv /data/.screenly/backup.db /data/.screenly/screenly.db
./manage.py dbbackup --noinput --clean && \
./manage.py migrate --fake-initial --noinput || \
./manage.py dbrestore --noinput
else
./manage.py migrate
./manage.py migrate && \
./manage.py dbbackup --noinput --clean
fi

if [[ "$ENVIRONMENT" == "development" ]]; then
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cryptography==3.3.2
Cython==0.29.33
Django==3.2.18
djangorestframework==3.14.0
django-dbbackup==4.2.1
drf-spectacular==0.27.2
future==0.18.3
gevent-websocket==0.10.1
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.viewer.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ configparser==4.0.2
cryptography==3.3.2
Cython==0.29.33
Django==3.2.18
django-dbbackup==4.2.1
drf-spectacular==0.27.2
future==0.18.3
idna==3.7
Expand Down

0 comments on commit 211b900

Please sign in to comment.