Files
loki/scripts/hetzner/droppable-db.sh
CatoandClaude 9597e1f75d ops(db): a dispatch-only workflow that drops one leftover host database (#1516)
Nothing dropped a host database: retire-site.sh keeps a site's data by
design, so signin, renamed to morulia, left its empty database behind.
drop-db.yml drops exactly one, and refuses unless the register and the
box both say nothing can still mean it:

- droppable-db.sh, against main's register and the box's checkout of it:
  not a system database, no row named after it, no row's db column
  using it;
- drop-db-on-box.sh, on the box: not a template, no env file under /opt
  naming it in DATABASE_URL, no open connection. Then the database, and
  the role of the same name if Postgres lets it go.

An empty confirm is a dry run; confirm must repeat the name to drop.
Names, counts and sizes only in the log. Tested in test:ops
(test-drop-db.sh).


Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-01 19:05:50 +01:00

51 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# droppable-db.sh <database> — print <database> if the register allows dropping
# it from the box's host Postgres, or refuse.
#
# The gate in front of .github/workflows/drop-db.yml, the one path that drops a
# host database. retire-site.sh never does (a site taken down keeps its data),
# so a renamed or retired service leaves its database behind — signin, renamed
# to morulia on 2026-11-01. Dropping is the one step here nobody can undo, so a
# name is accepted only when nothing in the register can still mean it:
#
# - it is a plain identifier (what provision-db-on-box.sh creates);
# - it is not a system database (postgres, template0, template1);
# - no apps.conf row is named after it (`signin` or, for `my_app`, `my-app`):
# a row is a site that may still be provisioned under that name;
# - no row's `db` column names it — the database an app is using.
#
# The workflow runs this against main's register AND the box's checkout of it
# (where register-site.sh writes a new row first), and the box script then
# checks the box itself (env files, connections). Prints names only.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source "$HERE/lib.sh"
DB_NAME="${1:-}"
[[ "$DB_NAME" =~ ^[a-z][a-z0-9_]{0,62}$ ]] || {
echo "not a plain host database name: '$DB_NAME'" >&2; exit 2; }
case "$DB_NAME" in
postgres|template0|template1)
echo "$DB_NAME is a system database; refusing" >&2; exit 1 ;;
esac
[ -f "$MANIFEST" ] || { echo "no register at $MANIFEST" >&2; exit 1; }
rows="$(grep -vE '^[[:space:]]*(#|$)' "$MANIFEST" | awk -F'|' 'NF > 5')"
for name in "$DB_NAME" "${DB_NAME//_/-}"; do
if awk -F'|' -v n="$name" '$1 == n { found = 1 } END { exit !found }' <<<"$rows"; then
echo "apps.conf has a row named '$name'; refusing (retire the site and remove its row first)" >&2
exit 1
fi
done
users="$(awk -F'|' -v d="$DB_NAME" '$6 == d { print $1 }' <<<"$rows" | tr '\n' ' ')"
if [ -n "${users// /}" ]; then
echo "'$DB_NAME' is the db column of: ${users% }; refusing" >&2
exit 1
fi
printf '%s\n' "$DB_NAME"