#!/bin/bash
# r893b-obs.sh LABEL — positive observables of the Rallly database (read-only)
P="docker exec rallly-postgres psql -U rallly -d rallly -Atq"
echo "=== $1 $(date -u +%T) UTC"
docker ps -a --filter label=com.docker.compose.project=rallly --format '{{.Names}} {{.Image}} {{.Status}}'
echo "markers (phase=rows): $($P -c "select string_agg(phase||'='||n, ', ' order by phase) from (select phase, count(*) n from r893_marker group by phase) x" 2>&1)"
for t in r893_after r893_only_old r893_new2; do
  ex=$($P -c "select count(*) from information_schema.tables where table_schema='public' and table_name='$t'")
  if [ "$ex" = 1 ]; then echo "$t exists: 1 rows: $($P -c "select count(*) from $t")"; else echo "$t exists: 0"; fi
done
echo "public tables: $($P -c "select count(*) from information_schema.tables where table_schema='public' and table_type='BASE TABLE'")"
echo "prisma migrations: $($P -c 'select count(*) from _prisma_migrations' 2>&1)"
echo "db volume created: $(docker volume inspect -f '{{.CreatedAt}}' rallly_rallly_postgres_data 2>&1)"
S1=$(docker exec rallly-postgres pg_dump -U rallly -d rallly --schema-only | grep -Ev '^\\(un)?restrict ' | md5sum | cut -c1-32)
S2=$(docker exec rallly-postgres pg_dump -U rallly -d rallly --schema-only | grep -Ev '^\\(un)?restrict ' | md5sum | cut -c1-32)
echo "schema-only md5: $S1 (control, second dump: $S2)"
echo "data-only md5: $(docker exec rallly-postgres pg_dump -U rallly -d rallly --data-only | grep -Ev '^\\(un)?restrict ' | md5sum | cut -c1-32)"
RC=$(for t in $($P -c "select table_name from information_schema.tables where table_schema='public' and table_type='BASE TABLE' order by table_name"); do echo "$t=$($P -c "select count(*) from \"$t\"")"; done)
echo "per-table row counts md5: $(echo "$RC" | md5sum | cut -c1-32)"
echo "per-table row counts: $(echo "$RC" | tr '\n' ' ')"
echo "definition: $(grep -E '^\s*image:' /opt/docker/stacks/rallly/docker-compose.yml | tr -s ' ' | tr '\n' ' ')"
echo "restore-undo folder: $(ls -A /mnt/sys_drive/felhom-data/backups/restore-undo/rallly 2>&1 | tr '\n' ' ')"
