Files
microdao-daarion/scripts/db-health-check.sh
Apple 6a76cffb88 fix: Add automatic removal of test agents in health check
- Add remove-test-agents.sh script
- Integrate test agent removal into db-health-check.sh
- Prevents test agents (ag_atlas, ag_oracle, ag_builder, ag_greeter) from reappearing
2025-12-02 13:57:28 -08:00

83 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Database health check and auto-recovery script
# Run this periodically (e.g., via cron) to ensure database persistence
set -e
LOG_FILE="/var/log/db-health-check.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
log() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
log "🔍 Starting database health check..."
# Check if PostgreSQL container is running
if ! docker ps | grep -q daarion-postgres; then
log "❌ PostgreSQL container is not running!"
exit 1
fi
# Check if database exists
if ! docker exec daarion-postgres psql -U postgres -lqt | cut -d \| -f 1 | grep -qw daarion; then
log "⚠️ Database 'daarion' does not exist, creating..."
docker exec daarion-postgres psql -U postgres -c "CREATE DATABASE daarion;" || {
log "❌ Failed to create database"
exit 1
}
fi
# Check if critical tables exist
TABLE_COUNT=$(docker exec daarion-postgres psql -U postgres -d daarion -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public';" 2>/dev/null | tr -d ' ' || echo "0")
if [ "$TABLE_COUNT" -lt 5 ]; then
log "⚠️ Database has only $TABLE_COUNT tables (expected 20+), applying migrations..."
cd /opt/microdao-daarion
for f in migrations/*.sql; do
log " Applying $(basename $f)..."
docker exec -i daarion-postgres psql -U postgres -d daarion < "$f" 2>&1 | grep -v "already exists\|does not exist" || true
done
log "✅ Migrations applied"
fi
# Check for missing critical columns
log "🔍 Checking for missing columns..."
# Check banner_url
if ! docker exec daarion-postgres psql -U postgres -d daarion -t -c "SELECT 1 FROM information_schema.columns WHERE table_name = 'microdaos' AND column_name = 'banner_url';" 2>/dev/null | grep -q 1; then
log "⚠️ Missing banner_url column, adding..."
docker exec daarion-postgres psql -U postgres -d daarion -c "ALTER TABLE microdaos ADD COLUMN IF NOT EXISTS banner_url TEXT;" || log "⚠️ Failed to add banner_url"
fi
# Check logo_url
if ! docker exec daarion-postgres psql -U postgres -d daarion -t -c "SELECT 1 FROM information_schema.columns WHERE table_name = 'microdaos' AND column_name = 'logo_url';" 2>/dev/null | grep -q 1; then
log "⚠️ Missing logo_url column, adding..."
docker exec daarion-postgres psql -U postgres -d daarion -c "ALTER TABLE microdaos ADD COLUMN IF NOT EXISTS logo_url TEXT;" || log "⚠️ Failed to add logo_url"
fi
# Check data integrity
MICRODAO_COUNT=$(docker exec daarion-postgres psql -U postgres -d daarion -t -c "SELECT COUNT(*) FROM microdaos;" 2>/dev/null | tr -d ' ' || echo "0")
log "📊 MicroDAOs in database: $MICRODAO_COUNT"
if [ "$MICRODAO_COUNT" -eq 0 ]; then
log "⚠️ No MicroDAOs found in database!"
log "💡 Consider restoring from backup: /opt/microdao-daarion/db_backups/"
fi
# Remove test agents (they should never exist)
log "🧹 Checking for test agents..."
TEST_AGENT_COUNT=$(docker exec daarion-postgres psql -U postgres -d daarion -t -c "SELECT COUNT(*) FROM agents WHERE id IN ('ag_atlas', 'ag_oracle', 'ag_builder', 'ag_greeter');" 2>/dev/null | tr -d ' ' || echo "0")
if [ "$TEST_AGENT_COUNT" -gt 0 ]; then
log "⚠️ Found $TEST_AGENT_COUNT test agents, removing..."
docker exec daarion-postgres psql -U postgres -d daarion -c "DELETE FROM agents WHERE id IN ('ag_atlas', 'ag_oracle', 'ag_builder', 'ag_greeter');" 2>&1 | grep -v "DELETE\|^$" || true
log "✅ Test agents removed"
fi
# Check volume persistence
VOLUME_SIZE=$(du -sh /var/lib/docker/volumes/daarion_pgdata/_data/ 2>/dev/null | awk '{print $1}' || echo "unknown")
log "📦 Database volume size: $VOLUME_SIZE"
log "✅ Health check complete"