From e0f5fafc949a539a2d9f0a94ee1a28f6dfa2bfb8 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Sat, 5 Jul 2025 19:56:31 +0100 Subject: [PATCH] Fix deployment issues: URL-encode database password, add auto-migrations, fix dynamic server error --- package.json | 3 ++- scripts/migrate.js | 28 ++++++++++++++++++++++++++++ src/app/page.tsx | 14 +++++++++++--- src/lib/db/index.ts | 7 ++++++- vercel.json | 8 ++++++++ 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 scripts/migrate.js create mode 100644 vercel.json diff --git a/package.json b/package.json index 2ab6d4a..b83adc4 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dev": "NODE_ENV=development next dev -p 3002 --turbo & local-ssl-proxy --config ./ssl-proxy.json", "dev:plain": "next dev --turbo", "dev:ssl": "NODE_ENV=development next dev -p 3002 --turbo & local-ssl-proxy --config ./ssl-proxy.json", - "build": "next build", + "build": "node scripts/migrate.js && next build", + "build:local": "next build", "start": "next start", "lint": "next lint", "db:generate": "drizzle-kit generate", diff --git a/scripts/migrate.js b/scripts/migrate.js new file mode 100644 index 0000000..01b173f --- /dev/null +++ b/scripts/migrate.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node + +import { execSync } from 'child_process'; + +async function runMigrations() { + try { + console.log('🔄 Running database migrations...'); + + // Check if we have a DATABASE_URL + if (!process.env.DATABASE_URL) { + console.log('⚠️ No DATABASE_URL found, skipping migrations'); + return; + } + + // Run migrations + execSync('npx drizzle-kit migrate', { + stdio: 'inherit', + env: process.env + }); + + console.log('✅ Database migrations completed successfully!'); + } catch (error) { + console.error('❌ Migration failed:', error.message); + process.exit(1); + } +} + +runMigrations(); diff --git a/src/app/page.tsx b/src/app/page.tsx index 1e9e598..1b402c6 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -12,6 +12,9 @@ import { import { eq, desc, inArray } from "drizzle-orm"; import { sql } from "drizzle-orm"; +// Force dynamic rendering for this page +export const dynamic = 'force-dynamic'; + export default async function Home() { try { const session = await getServerAuthSession(); @@ -154,11 +157,16 @@ export default async function Home() { ); } catch (error) { console.error("Error loading homepage:", error); + + // Return a fallback UI instead of throwing return (
-
-

- Unable to load images. Please try again later. +

+

+ Oops! Something went wrong loading the images. +

+

+ Please try refreshing the page. If the problem persists, contact support.

diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index 4354a78..c9a7e0c 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -4,5 +4,10 @@ import * as schema from './schema'; const connectionString = process.env.DATABASE_URL || 'postgres://postgres:hackme@localhost:5432/opengifame'; -const client = postgres(connectionString); +// Handle connection string more robustly +const client = postgres(connectionString, { + ssl: process.env.NODE_ENV === 'production' ? 'require' : false, + max: 1, // Limit connections for serverless +}); + export const db = drizzle(client, { schema }); diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..46f4428 --- /dev/null +++ b/vercel.json @@ -0,0 +1,8 @@ +{ + "buildCommand": "bun run db:migrate && bun run build:local", + "devCommand": "bun run dev:plain", + "installCommand": "bun install", + "env": { + "DATABASE_URL": "@database_url" + } +}