39 lines
1.8 KiB
Bash
39 lines
1.8 KiB
Bash
#!/usr/bin/env bash
|
|
# Called on the server after uploading incoming.db.gz and import_database.py.
|
|
# `db.sh push` does both and then runs this; by hand it is the same two files
|
|
# in the same two places.
|
|
#
|
|
# This replaces the database and does nothing else. It does not stop the app
|
|
# and it does not start it: whoever is running a deployment knows what else is
|
|
# moving and in what order, and a tool that helpfully restarts things underneath
|
|
# that is a tool fighting its caller. By hand it is
|
|
# `docker compose stop app` and `docker compose up -d --wait app` around this,
|
|
# with SERVICE (deploy.sh says why it is here) in place of app.
|
|
#
|
|
# What it will not do is import into a running app. The replacement is a file
|
|
# swap under SQLite's feet, and the live database is checkpointed first, which
|
|
# needs every writer gone — `import_database.py` refuses a busy database, and
|
|
# this refuses an app that is up, because the clearer error is the earlier one.
|
|
SERVICE=app
|
|
|
|
set -euo pipefail
|
|
cd "${1:?server directory required}"
|
|
|
|
# Same source of truth as the compose file, deploy.sh and backup.sh.
|
|
DATA=$(sed -n 's/^[[:space:]]*APP_DATA=\([^#]*\).*/\1/p' .env | tail -1 | xargs || true)
|
|
DATA=${DATA:-./data}
|
|
|
|
[[ -s "$DATA/incoming.db.gz" && -s import_database.py ]]
|
|
|
|
if [[ -n "$(docker compose ps -q --status running "$SERVICE" 2>/dev/null)" ]]; then
|
|
echo "$SERVICE is running. Stop it before importing:" >&2
|
|
echo " docker compose stop $SERVICE" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# The one-off container uses compose's uid and its mounted /data. `--pull
|
|
# never`: an import runs on the image already here, whatever the installed
|
|
# compose file says about pulling. Getting a new image is deploy.sh's job.
|
|
docker compose run --rm --no-deps --pull never -T --entrypoint python "$SERVICE" - < import_database.py
|
|
rm -f "$DATA/incoming.db.gz"
|