Files
touchbase/scripts/db-bootstrap.sh

27 lines
948 B
Bash
Raw Normal View History

2026-05-01 18:24:09 -04:00
#!/usr/bin/env bash
# Idempotent: ensures touchbase_test DB exists and both DBs have required extensions.
# Run after `docker-compose up -d postgres` if the init scripts didn't fire (bind-mount race on first start).
set -euo pipefail
PGUSER="${PGUSER:-touchbase}"
PGPASSWORD="${PGPASSWORD:-touchbase}"
PGHOST="${PGHOST:-localhost}"
PGPORT="${PGPORT:-5432}"
export PGPASSWORD
psql_run() { docker exec -e PGPASSWORD touchbase-postgres-1 psql -U "$PGUSER" -d "$1" -v ON_ERROR_STOP=1 -c "$2"; }
# Create test DB if missing
exists=$(docker exec -e PGPASSWORD touchbase-postgres-1 psql -U "$PGUSER" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='touchbase_test'")
if [ -z "$exists" ]; then
psql_run postgres "CREATE DATABASE touchbase_test OWNER $PGUSER"
fi
for db in touchbase_dev touchbase_test; do
psql_run "$db" "CREATE EXTENSION IF NOT EXISTS btree_gist; CREATE EXTENSION IF NOT EXISTS pgcrypto;"
done
echo "DB bootstrap OK."