fix: Add database persistence and health check scripts
- Add apply-migrations.sh for automatic migration application - Add ensure-db-persistence.sh for database integrity checks - Add db-health-check.sh for periodic health monitoring - Improve PostgreSQL configuration in docker-compose.db.yml - Add proper shutdown settings to prevent data loss
This commit is contained in:
46
scripts/apply-migrations.sh
Executable file
46
scripts/apply-migrations.sh
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
# Apply database migrations automatically
|
||||
# This script should be run on container startup or periodically
|
||||
|
||||
set -e
|
||||
|
||||
POSTGRES_HOST="${POSTGRES_HOST:-db}"
|
||||
POSTGRES_DB="${POSTGRES_DB:-daarion}"
|
||||
POSTGRES_USER="${POSTGRES_USER:-postgres}"
|
||||
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-postgres}"
|
||||
|
||||
MIGRATIONS_DIR="${MIGRATIONS_DIR:-/opt/microdao-daarion/migrations}"
|
||||
|
||||
echo "📊 Applying database migrations..."
|
||||
echo "Host: $POSTGRES_HOST"
|
||||
echo "Database: $POSTGRES_DB"
|
||||
echo "Migrations dir: $MIGRATIONS_DIR"
|
||||
|
||||
# Wait for PostgreSQL to be ready
|
||||
echo "⏳ Waiting for PostgreSQL..."
|
||||
for i in {1..30}; do
|
||||
if PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$POSTGRES_HOST" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 1;" > /dev/null 2>&1; then
|
||||
echo "✅ PostgreSQL is ready"
|
||||
break
|
||||
fi
|
||||
if [ $i -eq 30 ]; then
|
||||
echo "❌ PostgreSQL is not ready after 30 attempts"
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
|
||||
# Apply all migrations in order
|
||||
echo "📋 Applying migrations..."
|
||||
for migration_file in "$MIGRATIONS_DIR"/*.sql; do
|
||||
if [ -f "$migration_file" ]; then
|
||||
migration_name=$(basename "$migration_file")
|
||||
echo " Applying: $migration_name"
|
||||
|
||||
# Apply migration (ignore errors for already applied migrations)
|
||||
PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$POSTGRES_HOST" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -f "$migration_file" 2>&1 | grep -v "already exists\|does not exist" || true
|
||||
fi
|
||||
done
|
||||
|
||||
echo "✅ Migrations applied"
|
||||
|
||||
73
scripts/db-health-check.sh
Executable file
73
scripts/db-health-check.sh
Executable file
@@ -0,0 +1,73 @@
|
||||
#!/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
|
||||
|
||||
# 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"
|
||||
|
||||
50
scripts/ensure-db-persistence.sh
Executable file
50
scripts/ensure-db-persistence.sh
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/bin/bash
|
||||
# Ensure database persistence and apply migrations
|
||||
# Run this script periodically or on container startup
|
||||
|
||||
set -e
|
||||
|
||||
echo "🔍 Checking database persistence..."
|
||||
|
||||
# Check if database exists
|
||||
if docker exec daarion-postgres psql -U postgres -lqt | cut -d \| -f 1 | grep -qw daarion; then
|
||||
echo "✅ Database 'daarion' exists"
|
||||
else
|
||||
echo "⚠️ Database 'daarion' does not exist, creating..."
|
||||
docker exec daarion-postgres psql -U postgres -c "CREATE DATABASE daarion;"
|
||||
fi
|
||||
|
||||
# Check if volume has data
|
||||
VOLUME_SIZE=$(du -sh /var/lib/docker/volumes/daarion_pgdata/_data/ 2>/dev/null | awk '{print $1}' || echo "0")
|
||||
echo "📦 Volume size: $VOLUME_SIZE"
|
||||
|
||||
# Check if migrations are applied
|
||||
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")
|
||||
echo "📊 Tables in database: $TABLE_COUNT"
|
||||
|
||||
if [ "$TABLE_COUNT" -lt 10 ]; then
|
||||
echo "⚠️ Database seems empty or incomplete, applying migrations..."
|
||||
cd /opt/microdao-daarion
|
||||
for f in migrations/*.sql; do
|
||||
echo " Applying $(basename $f)..."
|
||||
docker exec -i daarion-postgres psql -U postgres -d daarion < "$f" 2>&1 | grep -E 'ERROR|ALTER|CREATE|INSERT|UPDATE|COMMENT' | tail -1 || true
|
||||
done
|
||||
echo "✅ Migrations applied"
|
||||
else
|
||||
echo "✅ Database has tables, checking for missing columns..."
|
||||
|
||||
# Check for missing banner_url column
|
||||
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
|
||||
echo "⚠️ 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;"
|
||||
fi
|
||||
|
||||
# Check for missing logo_url column
|
||||
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
|
||||
echo "⚠️ 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;"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "✅ Database persistence check complete"
|
||||
|
||||
Reference in New Issue
Block a user