ids.alfacom.it/database-schema/apply_migrations.sh
marco370 661e945f57 Implement automatic database cleanup and schema updates
Adds scripts for automatic database log cleanup, schema migration application, and cron job setup. Modifies the update script to apply SQL migrations before pushing Drizzle schema.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 9a659f15-d68a-4b7d-99f8-3eccc59afebe
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/4LjHWWz
2025-11-21 16:49:13 +00:00

56 lines
1.6 KiB
Bash
Executable File

#!/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}"