#!/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. Setup extensions echo "Enabling extensions..." docker exec dagi-postgres psql -U postgres -d daarion_memory -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";' || echo "Warning: Could not enable uuid-ossp" docker exec dagi-postgres psql -U postgres -d daarion_memory -c 'CREATE EXTENSION IF NOT EXISTS "pgcrypto";' || echo "Warning: Could not enable pgcrypto" # 4. Apply all migrations in order echo "Applying migrations..." for f in migrations/*.sql; do echo "Applying $f..." docker exec -i dagi-postgres psql -U postgres -d daarion_memory < "$f" || echo "Warning: Migration $f had errors, continuing..." done # 5. Rebuild and restart city-service echo "Restarting city-service..." if command -v docker-compose &> /dev/null; then docker-compose up -d --build --no-deps city-service else docker compose up -d --build --no-deps city-service fi echo "Deployment and DB restoration complete!"