From c2bd8a5a08d5705e284d7a44297af440b2baa7fa Mon Sep 17 00:00:00 2001 From: Mackie Date: Fri, 22 May 2026 07:47:51 +0800 Subject: [PATCH] wooork --- Dockerfile | 3 ++- scripts/README.md | 21 +++++++++++++++++++++ scripts/migrate.sh | 33 +++++++++++++++++++++++++++++++++ scripts/migrate.ts | 21 +++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 scripts/README.md create mode 100644 scripts/migrate.sh create mode 100644 scripts/migrate.ts diff --git a/Dockerfile b/Dockerfile index a10a45d..3325165 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,6 +28,7 @@ COPY --from=builder /app/src ./src RUN mkdir .next RUN chown nextjs:nodejs .next +RUN chown -R nextjs:nodejs /app/public COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static @@ -38,4 +39,4 @@ EXPOSE 3001 ENV PORT=3001 ENV HOSTNAME="0.0.0.0" -CMD ["sh", "-c", "node --import tsx/esm /app/node_modules/payload/dist/bin/index.js migrate && node server.js"] \ No newline at end of file +CMD ["node", "server.js"] \ No newline at end of file diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 0000000..9cb3493 --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,21 @@ +# 1. Exec into the app container +docker exec -it $(docker ps | grep portfolio-web | awk '{print $1}') sh + +# 2. Write and run the migration +cat > /tmp/migrate.ts << 'EOF' +import { drizzle } from 'drizzle-orm/node-postgres' +import pg from 'pg' + +async function main() { + const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }) + const { up } = await import('/app/src/migrations/20260521_173007.ts') + console.log('running migration...') + await up({ db: drizzle(pool) }) + console.log('done') + await pool.end() +} + +main().catch(console.error) +EOF + +DATABASE_URL=postgresql://postgres:TrojanHero1@portfolio-portfoliodb-22lpk0:5432/postgres node_modules/.bin/tsx /tmp/migrate.ts \ No newline at end of file diff --git a/scripts/migrate.sh b/scripts/migrate.sh new file mode 100644 index 0000000..d5e50e9 --- /dev/null +++ b/scripts/migrate.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Run this from the VPS when you need to manually run migrations +# Usage: bash scripts/migrate.sh + +NETWORK=$(docker inspect $(docker ps | grep portfoliodb | awk '{print $1}' | head -1) --format '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}{{end}}') +APP_IMAGE=$(docker images | grep portfolio-web | awk '{print $1}' | head -1) + +docker run --rm \ + --network $NETWORK \ + -e DATABASE_URL=postgresql://postgres:TrojanHero1@portfolio-portfoliodb-22lpk0:5432/postgres \ + -e PAYLOAD_SECRET=n8Kj2vR5pL9mQx7tY3wE6bZ1aD4sF0hU9cV2xB5nM8pL1q \ + $APP_IMAGE \ + sh -c "cat > /tmp/migrate.ts << 'EOF' +import { drizzle } from 'drizzle-orm/node-postgres' +import pg from 'pg' + +async function main() { + const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }) + const migrations = await import('/app/src/migrations/index.ts') + console.log('running migrations...') + for (const [name, migration] of Object.entries(migrations)) { + if (typeof migration === 'function') { + console.log('running:', name) + await migration({ db: drizzle(pool) }) + } + } + console.log('done') + await pool.end() +} + +main().catch(console.error) +EOF +node_modules/.bin/tsx /tmp/migrate.ts" \ No newline at end of file diff --git a/scripts/migrate.ts b/scripts/migrate.ts new file mode 100644 index 0000000..ac6a56f --- /dev/null +++ b/scripts/migrate.ts @@ -0,0 +1,21 @@ +import { drizzle } from 'drizzle-orm/node-postgres' +import pg from 'pg' +import * as migrations from './src/migrations/index.ts' + +async function main() { + const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL }) + const db = drizzle(pool) + + for (const [name, fn] of Object.entries(migrations)) { + if (typeof fn === 'function' && name === 'up') continue + if (typeof fn === 'function') { + console.log(`running: ${name}`) + await fn({ db }) + } + } + + console.log('all migrations done') + await pool.end() +} + +main().catch(console.error) \ No newline at end of file