From 01a84edee9edbab202e12a9de1be99d4db84c52a Mon Sep 17 00:00:00 2001 From: DavidHerran Date: Mon, 4 May 2026 15:04:35 -0500 Subject: [PATCH] fix: prisma migrate now runs at container startup + dotenv optional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two related fixes for the deploy pipeline so DB schema changes never again leave the site half-deployed. PRISMA CONFIG (prisma.config.ts) - "import 'dotenv/config'" was hard-required, but dotenv isn't installed in the production runtime image (env vars come from docker-compose). - Wrapped in try/catch so it loads .env locally and silently no-ops in the container — `prisma migrate deploy` works in both environments. DOCKERFILE - Copies node_modules/prisma + prisma.config.ts to the runner stage so the CLI is available at runtime, not just at build. - New CMD runs `prisma migrate deploy` before booting the server. Idempotent — already-applied migrations are skipped. If the DB is unreachable, the container exits and docker-compose retries. - This means: from now on, `git pull && docker compose up -d --build app` is the entire deploy. No more "did you remember to run migrations?". DEFENSIVE TRY/CATCH (applications/[slug]/page.tsx) - prisma.application.findUnique and prisma.globalNode.findMany now have try/catch with logged errors. A transient DB hiccup or missing Application slug now degrades gracefully (renders "not found" or empty cases wall) instead of triggering a 500 Internal Server Error. DEPLOY (David, this is the recovery sequence on the VPS) cd /opt/flux-srl git pull docker compose up -d --build app # The container will run pending migrations on its own. # No need to run `prisma migrate deploy` manually anymore. --- Dockerfile | 11 +++++-- prisma.config.ts | 12 ++++++-- src/app/[locale]/applications/[slug]/page.tsx | 30 ++++++++++++------- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9cada88..127de1d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -54,10 +54,14 @@ COPY --from=builder /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static -# Copy Prisma schema + generated client (needed for migrations at runtime) +# Copy Prisma schema + generated client + CLI binaries (the CLI is needed +# at runtime so the entrypoint can run `prisma migrate deploy` before the +# server boots — avoids the "table does not exist" race after schema changes) COPY --from=builder /app/prisma ./prisma +COPY --from=builder /app/prisma.config.ts ./prisma.config.ts COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma +COPY --from=builder /app/node_modules/prisma ./node_modules/prisma # Copy sharp binary explicitly — Next.js standalone trace usually picks it # up, but the @img/sharp-linuxmusl-x64 prebuilt is platform-conditional and @@ -75,4 +79,7 @@ EXPOSE 3000 ENV PORT=3000 ENV HOSTNAME="0.0.0.0" -CMD ["node", "server.js"] +# Run pending migrations on startup, then boot the Next.js server. +# `migrate deploy` is idempotent — it skips already-applied migrations. +# If the DB is unreachable the container exits and docker-compose retries. +CMD ["sh", "-c", "node ./node_modules/prisma/build/index.js migrate deploy && node server.js"] diff --git a/prisma.config.ts b/prisma.config.ts index e6c49b3..88b7e48 100644 --- a/prisma.config.ts +++ b/prisma.config.ts @@ -1,7 +1,13 @@ +// Local dev loads .env via dotenv. Production (Docker) injects env vars +// directly through docker-compose, so dotenv isn't installed in the runtime +// image — load it conditionally so `prisma migrate deploy` works there too. +try { + // eslint-disable-next-line @typescript-eslint/no-require-imports + require("dotenv/config"); +} catch { + // dotenv not installed in this environment — env vars expected from process +} -// This file was generated by Prisma and assumes you have installed the following: -// npm install --save-dev prisma dotenv -import "dotenv/config"; import { defineConfig, env } from "prisma/config"; export default defineConfig({ diff --git a/src/app/[locale]/applications/[slug]/page.tsx b/src/app/[locale]/applications/[slug]/page.tsx index 56937e8..4350e9a 100644 --- a/src/app/[locale]/applications/[slug]/page.tsx +++ b/src/app/[locale]/applications/[slug]/page.tsx @@ -93,9 +93,12 @@ export default async function ApplicationPage({ params }: { params: Promise<{ sl const { slug, locale } = resolvedParams; // 1. Buscamos la Teoría General de la Aplicación - const rawData = await prisma.application.findUnique({ - where: { slug } - }); + let rawData: any = null; + try { + rawData = await prisma.application.findUnique({ where: { slug } }); + } catch (error) { + console.error(`[applications/${slug}] DB error fetching application:`, error); + } if (!rawData) { return ( @@ -110,14 +113,19 @@ export default async function ApplicationPage({ params }: { params: Promise<{ sl const data = getLocalizedData(rawData, locale); // 2. Buscamos el "Muro de Soluciones" (Casos Reales específicos de esta app) - const rawRealCases = await prisma.globalNode.findMany({ - where: { - application: slug, - isActive: true, - projectOverview: { not: null } - }, - orderBy: { createdAt: 'desc' } - }); + let rawRealCases: any[] = []; + try { + rawRealCases = await prisma.globalNode.findMany({ + where: { + application: slug, + isActive: true, + projectOverview: { not: null }, + }, + orderBy: { createdAt: "desc" }, + }); + } catch (error) { + console.error(`[applications/${slug}] DB error fetching cases:`, error); + } // 🔥 TRADUCIMOS TODOS LOS CASOS DE ESTUDIO DEL MURO const realCases = rawRealCases.map((node: any) => getLocalizedData(node, locale));