#!/bin/sh set -e echo "⏳ Waiting for MySQL to be ready..." MAX_RETRIES=30 RETRY=0 until node -e " const net = require('net'); const client = new net.Socket(); client.setTimeout(2000); client.connect(Number(process.env.DB_PORT), process.env.DB_HOST, () => { client.destroy(); process.exit(0); }); client.on('error', () => { client.destroy(); process.exit(1); }); client.on('timeout', () => { client.destroy(); process.exit(1); }); " 2>/dev/null; do RETRY=$((RETRY + 1)) if [ "$RETRY" -ge "$MAX_RETRIES" ]; then echo "❌ MySQL did not become ready in time. Exiting." exit 1 fi echo " MySQL not ready yet, retrying in 3s... ($RETRY/$MAX_RETRIES)" sleep 3 done # Extra grace period for MySQL to finish internal auth setup sleep 2 echo "✅ MySQL is ready!" # Run Prisma schema push (Prisma v7: no --skip-generate flag) echo "🔄 Pushing Prisma schema to database..." npx prisma db push # Seed only if no admin user exists yet echo "🌱 Checking if seed is needed..." ADMIN_EXISTS=$(node -e " const mariadb = require('mariadb'); const pool = mariadb.createPool({ host: process.env.DB_HOST, port: Number(process.env.DB_PORT), user: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, connectionLimit: 1 }); pool.getConnection() .then(async conn => { const rows = await conn.query('SELECT COUNT(*) as cnt FROM User WHERE role = ?', ['ADMIN']); conn.release(); await pool.end(); console.log(rows[0].cnt > 0 ? 'yes' : 'no'); }) .catch(async (err) => { try { await pool.end(); } catch(e) {} console.log('no'); }); " 2>/dev/null) echo " Admin exists check: $ADMIN_EXISTS" if [ "$ADMIN_EXISTS" = "yes" ]; then echo "✅ Admin user already exists, skipping seed." else echo "🌱 Seeding database with initial data..." node prisma/seed.js echo "✅ Seed complete!" fi echo "🚀 Starting Next.js server..." exec npm start