#!/bin/bash set -e # 1. Pull latest code echo "Pulling latest code..." cd /opt/microdao-daarion git pull origin main # 2. Check if database exists, create if not echo "Checking database..." if docker exec dagi-postgres psql -U postgres -lqt | cut -d \| -f 1 | grep -qw daarion_memory; then echo "Database daarion_memory exists." else echo "Database daarion_memory does not exist. Creating..." docker exec dagi-postgres psql -U postgres -c "CREATE DATABASE daarion_memory;" fi # 3. Apply all migrations in order echo "Applying migrations..." for f in migrations/*.sql; do echo "Applying $f..." # Check if migration needed? # For simplicity in this recovery script, we assume psql will handle "IF NOT EXISTS" errors # or we just run them. # Real production migration tools track versions. # Here we rely on SQL idempotency or manual check. # Most of our recent migrations use "CREATE TABLE IF NOT EXISTS". # Let's run them. docker exec -i dagi-postgres psql -U postgres -d daarion_memory < "$f" done # 4. Rebuild and restart city-service echo "Restarting city-service..." docker-compose up -d --build city-service echo "Deployment and DB restoration complete!"