- 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
51 lines
2.3 KiB
Bash
Executable File
51 lines
2.3 KiB
Bash
Executable File
#!/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"
|
|
|