#!/bin/bash # ============================================================================= # IDS - Applica Migrazioni Database # ============================================================================= # Applica tutti gli script SQL in database-schema/migrations/ in ordine # ============================================================================= set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" MIGRATIONS_DIR="$SCRIPT_DIR/migrations" # Colori GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' echo -e "${BLUE}🗄️ Applicazione migrazioni database...${NC}" # Verifica DATABASE_URL if [ -z "$DATABASE_URL" ]; then echo -e "${RED}❌ DATABASE_URL non impostato${NC}" exit 1 fi # Crea directory migrations se non esiste mkdir -p "$MIGRATIONS_DIR" # Conta migrazioni MIGRATION_COUNT=$(find "$MIGRATIONS_DIR" -name "*.sql" 2>/dev/null | wc -l) if [ "$MIGRATION_COUNT" -eq 0 ]; then echo -e "${YELLOW}⚠️ Nessuna migrazione da applicare${NC}" exit 0 fi echo -e "${BLUE}📋 Trovate $MIGRATION_COUNT migrazioni${NC}" # Applica ogni migrazione in ordine for migration in $(find "$MIGRATIONS_DIR" -name "*.sql" | sort); do MIGRATION_NAME=$(basename "$migration") echo -e "${BLUE} Applicando: $MIGRATION_NAME${NC}" if psql "$DATABASE_URL" -f "$migration" > /dev/null 2>&1; then echo -e "${GREEN} ✅ $MIGRATION_NAME applicata${NC}" else echo -e "${RED} ❌ Errore in $MIGRATION_NAME${NC}" psql "$DATABASE_URL" -f "$migration" exit 1 fi done echo -e "${GREEN}✅ Tutte le migrazioni applicate con successo${NC}"