mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-11 17:27:31 +02:00
Automate database migrations on Docker startup for production
Update Dockerfile and entrypoint script to automatically run database migrations and apply schema changes upon container initialization. Replit-Commit-Author: Agent Replit-Commit-Session-Id: f9197b9b-a5b3-4469-a27d-930a9e9ee736 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/f9197b9b-a5b3-4469-a27d-930a9e9ee736/i2gFnxm
This commit is contained in:
1 parent
f55a7a6217
commit
a2554c4962
4 files changed
+98
-32
No files matched your search
+5
-3
@@ -107,8 +107,10 @@ USER nextjs
|
|||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||||
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1
|
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1
|
||||||
|
|
||||||
# Make migration script executable
|
# Make scripts executable
|
||||||
RUN chmod +x /app/scripts/auto-migrate-production.sh
|
RUN chmod +x /app/scripts/auto-migrate-production.sh && \
|
||||||
|
chmod +x /app/scripts/docker-entrypoint.sh
|
||||||
|
|
||||||
# Start the application normally (migration will be manual or via init script)
|
# Use entrypoint script for automatic migrations
|
||||||
|
ENTRYPOINT ["/app/scripts/docker-entrypoint.sh"]
|
||||||
CMD ["node", "dist/index.js"]
|
CMD ["node", "dist/index.js"]
|
||||||
@@ -23,6 +23,8 @@ services:
|
|||||||
depends_on:
|
depends_on:
|
||||||
logiflow-db:
|
logiflow-db:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
|
volumes:
|
||||||
|
- ./migrations:/app/migrations
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/health"]
|
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/health"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
|
|||||||
Regular → Executable
+89
-27
@@ -1,37 +1,99 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Point d'entrée Docker avec migration automatique
|
|
||||||
# Ce script remplace le point d'entrée par défaut du conteneur
|
# Script d'entrée Docker - Applique automatiquement les migrations au démarrage
|
||||||
|
# Auteur: Système de gestion automatisé
|
||||||
|
# Date: 2025-08-18
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
echo "🚀 [ENTRYPOINT] Démarrage du conteneur avec migration automatique..."
|
echo "🚀 [ENTRYPOINT] Démarrage du conteneur de production"
|
||||||
|
echo "=================================================="
|
||||||
|
|
||||||
# Attendre que la base de données soit prête
|
# Fonction de logging avec timestamp
|
||||||
echo "⏳ [ENTRYPOINT] Attente de la base de données..."
|
log() {
|
||||||
timeout=60
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
|
||||||
counter=0
|
}
|
||||||
|
|
||||||
while ! psql "$DATABASE_URL" -c "SELECT 1;" > /dev/null 2>&1; do
|
log "🔧 Initialisation des variables d'environnement..."
|
||||||
counter=$((counter + 1))
|
|
||||||
if [ $counter -gt $timeout ]; then
|
# Construire DATABASE_URL si nécessaire
|
||||||
echo "❌ [ENTRYPOINT] Timeout: impossible de se connecter à la base de données après ${timeout}s"
|
if [ -z "$DATABASE_URL" ]; then
|
||||||
exit 1
|
if [ -n "$POSTGRES_USER" ] && [ -n "$POSTGRES_PASSWORD" ] && [ -n "$POSTGRES_DB" ]; then
|
||||||
|
export DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@logiflow-db:5432/${POSTGRES_DB}"
|
||||||
|
log "✅ URL de base de données construite automatiquement"
|
||||||
|
else
|
||||||
|
log "⚠️ Variables PostgreSQL manquantes, utilisation de DATABASE_URL existant"
|
||||||
fi
|
fi
|
||||||
echo "⏳ [ENTRYPOINT] Tentative $counter/$timeout..."
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "✅ [ENTRYPOINT] Base de données accessible"
|
|
||||||
|
|
||||||
# Exécuter les migrations automatiques
|
|
||||||
if [ -f "/app/scripts/auto-migrate-production.sh" ]; then
|
|
||||||
echo "🔄 [ENTRYPOINT] Exécution des migrations automatiques..."
|
|
||||||
chmod +x /app/scripts/auto-migrate-production.sh
|
|
||||||
/app/scripts/auto-migrate-production.sh
|
|
||||||
else
|
|
||||||
echo "⚠️ [ENTRYPOINT] Script de migration non trouvé, poursuite sans migration"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Démarrer l'application
|
# Attendre que PostgreSQL soit prêt
|
||||||
echo "🎯 [ENTRYPOINT] Démarrage de l'application..."
|
log "⏳ Attente de PostgreSQL..."
|
||||||
|
max_attempts=30
|
||||||
|
attempt=1
|
||||||
|
|
||||||
|
while [ $attempt -le $max_attempts ]; do
|
||||||
|
if psql "$DATABASE_URL" -c "SELECT 1;" > /dev/null 2>&1; then
|
||||||
|
log "✅ PostgreSQL est prêt"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $attempt -eq $max_attempts ]; then
|
||||||
|
log "❌ PostgreSQL non accessible après $max_attempts tentatives"
|
||||||
|
log "🔍 Variables DB: USER=${POSTGRES_USER:-'non défini'} DB=${POSTGRES_DB:-'non défini'}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
log "⏳ Tentative $attempt/$max_attempts - attente 2 secondes..."
|
||||||
|
sleep 2
|
||||||
|
((attempt++))
|
||||||
|
done
|
||||||
|
|
||||||
|
# Exécuter les migrations automatiques
|
||||||
|
log "🔄 Exécution des migrations automatiques..."
|
||||||
|
if [ -f "/app/scripts/auto-migrate-production.sh" ]; then
|
||||||
|
log "📂 Script de migration trouvé, exécution..."
|
||||||
|
chmod +x /app/scripts/auto-migrate-production.sh
|
||||||
|
|
||||||
|
if /app/scripts/auto-migrate-production.sh; then
|
||||||
|
log "✅ Migrations exécutées avec succès"
|
||||||
|
else
|
||||||
|
log "❌ Erreur lors des migrations"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log "⚠️ Script de migration non trouvé à /app/scripts/auto-migrate-production.sh"
|
||||||
|
log "🔍 Contenu du répertoire scripts:"
|
||||||
|
ls -la /app/scripts/ || echo "Répertoire scripts non trouvé"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Vérification post-migration
|
||||||
|
log "🔍 Vérification des colonnes DLC..."
|
||||||
|
if psql "$DATABASE_URL" -tAc "SELECT EXISTS (SELECT FROM information_schema.columns WHERE table_name='dlc_products' AND column_name='stock_epuise');" | grep -q "t"; then
|
||||||
|
log "✅ Colonnes DLC stock épuisé présentes"
|
||||||
|
else
|
||||||
|
log "❌ Colonnes DLC stock épuisé manquantes"
|
||||||
|
log "🛠️ Tentative de migration directe..."
|
||||||
|
|
||||||
|
psql "$DATABASE_URL" << 'EOF'
|
||||||
|
ALTER TABLE dlc_products
|
||||||
|
ADD COLUMN IF NOT EXISTS stock_epuise boolean DEFAULT false NOT NULL,
|
||||||
|
ADD COLUMN IF NOT EXISTS stock_epuise_by varchar(255),
|
||||||
|
ADD COLUMN IF NOT EXISTS stock_epuise_at timestamp;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_dlc_products_stock_epuise ON dlc_products(stock_epuise);
|
||||||
|
EOF
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
log "✅ Migration directe réussie"
|
||||||
|
else
|
||||||
|
log "❌ Migration directe échouée"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Démarrage de l'application
|
||||||
|
log "🚀 Démarrage de l'application..."
|
||||||
|
log "=================================================="
|
||||||
|
|
||||||
|
# Exécuter la commande passée en paramètre (normalement "node dist/index.js")
|
||||||
exec "$@"
|
exec "$@"
|
||||||
Executable → Regular
+2
-2
@@ -95,8 +95,8 @@ else
|
|||||||
log "⚠️ Erreur lors de la migration - vérification manuelle nécessaire"
|
log "⚠️ Erreur lors de la migration - vérification manuelle nécessaire"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Démarrage complet de l'application
|
# Démarrage complet de l'application (la migration sera automatique)
|
||||||
log "🚀 Démarrage complet de l'application..."
|
log "🚀 Démarrage complet de l'application avec migration automatique..."
|
||||||
docker-compose up -d
|
docker-compose up -d
|
||||||
|
|
||||||
# Attendre que l'application soit prête
|
# Attendre que l'application soit prête
|
||||||
|
|||||||
Reference in new issue
Block a user