diff --git a/Dockerfile b/Dockerfile index 771e22c..e69c0d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,12 +28,9 @@ COPY . . # Set a dummy DATABASE_URL for build time (Prisma needs it to generate client) ENV DATABASE_URL="file:/tmp/build.db" -# Generate Prisma client +# Generate Prisma client (no DB needed at build time) RUN pnpm prisma generate -# Initialize the database schema for build time -RUN pnpm prisma migrate deploy || pnpm prisma db push - # Build the application RUN pnpm run build @@ -65,12 +62,9 @@ RUN chown nextjs:nodejs .next COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static -# Copy Prisma schema +# Copy Prisma schema and migrations COPY --from=builder /app/prisma ./prisma -# Copy scripts (including init-db.js) -COPY --from=builder /app/scripts ./scripts - # Copy pnpm node_modules (includes .pnpm store with Prisma client) COPY --from=builder /app/node_modules ./node_modules @@ -86,5 +80,7 @@ USER nextjs EXPOSE 3000 -# Start the application with smart database initialization -CMD ["sh", "-c", "node scripts/init-db.js && node server.js"] +# Start the application with Prisma migrations +# For fresh DBs: use db push to apply schema, then mark migrations as applied +# For existing DBs: use migrate deploy to apply incremental migrations +CMD ["sh", "-c", "set +e; if ! pnpm prisma migrate deploy; then echo 'Migration failed, using db push for fresh database...'; pnpm prisma db push --accept-data-loss --skip-generate; for migration in prisma/migrations/*/; do if [ -d \"$migration\" ] && [ -f \"$migration/migration.sql\" ]; then migration_name=$(basename \"$migration\"); pnpm prisma migrate resolve --applied \"$migration_name\" 2>/dev/null || true; fi; done; fi; set -e; exec node server.js"] diff --git a/docker-compose.yml b/docker-compose.yml index 76f106f..990d646 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,16 +5,22 @@ services: dockerfile: Dockerfile target: runner ports: - - '3006:3000' + - '${PORT:-3007}:3000' environment: - NODE_ENV: production - DATABASE_URL: 'file:../data/dev.db' # Prisma - BACKUP_DATABASE_PATH: './data/dev.db' # Base de données à sauvegarder - BACKUP_STORAGE_PATH: './data/backups' # Dossier des sauvegardes - TZ: Europe/Paris + NODE_ENV: ${NODE_ENV:-production} + DATABASE_URL: ${DATABASE_URL:-file:../data/dev.db} + BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db} + BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups} + TZ: ${TZ:-Europe/Paris} # NextAuth.js - NEXTAUTH_SECRET: 'TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=' - NEXTAUTH_URL: 'http://localhost:3006' + NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=} + NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3006} + # Jira (optionnel) + JIRA_BASE_URL: ${JIRA_BASE_URL:-} + JIRA_EMAIL: ${JIRA_EMAIL:-} + JIRA_API_TOKEN: ${JIRA_API_TOKEN:-} + # Debug + VERBOSE_LOGGING: ${VERBOSE_LOGGING:-false} volumes: - ./data:/app/data # Dossier local data/ vers /app/data restart: unless-stopped @@ -31,16 +37,22 @@ services: dockerfile: Dockerfile target: base ports: - - '3005:3000' + - '${PORT_DEV:-3005}:3000' environment: - NODE_ENV: development - DATABASE_URL: 'file:../data/dev.db' # Prisma - BACKUP_DATABASE_PATH: './data/dev.db' # Base de données à sauvegarder - BACKUP_STORAGE_PATH: './data/backups' # Dossier des sauvegardes - TZ: Europe/Paris + NODE_ENV: ${NODE_ENV:-development} + DATABASE_URL: ${DATABASE_URL:-file:../data/dev.db} + BACKUP_DATABASE_PATH: ${BACKUP_DATABASE_PATH:-./data/dev.db} + BACKUP_STORAGE_PATH: ${BACKUP_STORAGE_PATH:-./data/backups} + TZ: ${TZ:-Europe/Paris} # NextAuth.js - NEXTAUTH_SECRET: 'TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=' - NEXTAUTH_URL: 'http://localhost:3005' + NEXTAUTH_SECRET: ${NEXTAUTH_SECRET:-TbwIWAmQgBcOlg7jRZrhkeEUDTpSr8Cj/Cc7W58fAyw=} + NEXTAUTH_URL: ${NEXTAUTH_URL:-http://localhost:3005} + # Jira (optionnel) + JIRA_BASE_URL: ${JIRA_BASE_URL:-} + JIRA_EMAIL: ${JIRA_EMAIL:-} + JIRA_API_TOKEN: ${JIRA_API_TOKEN:-} + # Debug + VERBOSE_LOGGING: ${VERBOSE_LOGGING:-false} volumes: - .:/app # code en live - /app/node_modules # vol anonyme pour ne pas écraser ceux du conteneur @@ -49,7 +61,7 @@ services: command: > sh -c "pnpm install && pnpm prisma generate && - node scripts/init-db.js && + (pnpm prisma migrate deploy || (echo 'Migration failed, using db push for fresh database...' && pnpm prisma db push --accept-data-loss --skip-generate && for migration in prisma/migrations/*/; do if [ -d \"\$migration\" ] && [ -f \"\$migration/migration.sql\" ]; then migration_name=\$(basename \"\$migration\"); pnpm prisma migrate resolve --applied \"\$migration_name\" 2>/dev/null || true; fi; done)) && pnpm run dev" profiles: - dev @@ -59,5 +71,6 @@ services: # ├── dev.db -> Base de données développement # └── backups/ -> Sauvegardes automatiques # -# 🔧 Configuration via .env.docker -# 📚 Documentation : ./data/README.md +# 🔧 Configuration via variables d'environnement (.env ou .env.local) +# Les variables utilisent la syntaxe ${VAR:-default} pour les fallbacks +# 📚 Documentation : ./data/README.md et env.example diff --git a/scripts/init-db.js b/scripts/init-db.js deleted file mode 100755 index b73a9eb..0000000 --- a/scripts/init-db.js +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env node -/** - * Smart database initialization script - * - Fresh DB (no tables) -> prisma db push (fast, applies full schema) - * - Existing DB with data -> prisma migrate deploy (safe, incremental) - */ - -const { execSync } = require('child_process'); -const fs = require('fs'); -const path = require('path'); - -function exec(command) { - try { - return execSync(command, { encoding: 'utf-8', stdio: 'pipe' }); - } catch (error) { - return null; - } -} - -function getDatabasePath() { - const dbUrl = process.env.DATABASE_URL || 'file:./data/prod.db'; - // Extract path from SQLite URL (file:./path or file:/absolute/path) - return dbUrl.replace(/^file:/, ''); -} - -function isDatabaseEmpty() { - const dbPath = getDatabasePath(); - const absolutePath = path.resolve(dbPath); - - console.log(`🔍 Checking database at: ${absolutePath}`); - - // Si le fichier DB n'existe pas, c'est vide - if (!fs.existsSync(absolutePath)) { - console.log('📦 Database file does not exist - fresh install'); - return true; - } - - // Vérifier si des tables existent - const result = exec('pnpm prisma db execute --stdin <<< ".tables"'); - if (!result || result.trim() === '') { - console.log('📦 Database exists but has no tables - fresh install'); - return true; - } - - console.log('🔄 Database has existing tables'); - return false; -} - -function main() { - console.log('🚀 Starting database initialization...\n'); - - if (isDatabaseEmpty()) { - console.log('✨ Applying full schema with prisma db push...'); - execSync('pnpm prisma db push --skip-generate', { stdio: 'inherit' }); - } else { - console.log('📝 Applying migrations with prisma migrate deploy...'); - execSync('pnpm prisma migrate deploy', { stdio: 'inherit' }); - } - - console.log('\n✅ Database ready!'); -} - -main(); -