Skip to content

Commit

Permalink
implement migration handling script
Browse files Browse the repository at this point in the history
  • Loading branch information
jthrilly committed May 20, 2024
1 parent 0d07e70 commit 3acb734
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ RUN chown nextjs:nodejs .next
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/handle-migrations.js ./
COPY --from=builder --chown=nextjs:nodejs /app/migrate-and-start.sh ./
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma

Expand Down
47 changes: 47 additions & 0 deletions handle-migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
import { PrismaClient } from '@prisma/client';
import { execSync } from 'child_process';
import dotenv from 'dotenv';

dotenv.config();

const prisma = new PrismaClient();

/**
* This function checks if the database is in a state where the workaround is needed.
*
* The workaround is needed when the database is not empty and the _prisma_migrations
* table does not exist.
*/
async function shouldApplyWorkaround() {
const tables = await prisma.$queryRaw`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'`;

const databaseNotEmpty = tables.length > 0;
const migrationsTableExists = tables.some(table => table.table_name === '_prisma_migrations');


return !migrationsTableExists && databaseNotEmpty;
}


async function handleMigrations() {
try {
if (await shouldApplyWorkaround()) {
console.log('Workaround needed! Running: prisma migrate resolve --applied 0_init');
execSync('npx prisma migrate resolve --applied 0_init', { stdio: 'inherit' });
}

console.log('Running: prisma migrate deploy');
execSync('npx prisma migrate deploy', { stdio: 'inherit' });
} catch (error) {
console.error('Error during migration process:', error);
process.exit(1);
} finally {
await prisma.$disconnect();
}
}

handleMigrations();
2 changes: 1 addition & 1 deletion migrate-and-start.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh

npx prisma migrate deploy
node handleMigrations.js
node server.js
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
"ts-lint": "SKIP_ENV_VALIDATION=true tsc --noEmit --incremental",
"ts-lint:watch": "SKIP_ENV_VALIDATION=true tsc --noEmit --incremental --watch",
"start": "next start",
"start:prod": "cross-env NODE_ENV=production pnpm db:migrate:prod && next start",
"db:push": "cross-env NODE_ENV=development prisma db push",
"db:migrate:prod": "cross-env NODE_ENV=production prisma migrate deploy",
"vercel-build": "prisma generate && prisma migrate deploy && next build",
"vercel-build": "node ./handle-migrations.js && next build",
"knip": "knip",
"test": "vitest"
},
Expand Down
6 changes: 0 additions & 6 deletions postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@ import { execSync } from 'child_process';

// Always run prisma generate
execSync('prisma generate', { stdio: 'inherit' });

// Only run prisma db push if VERCEL_ENV is production
// eslint-disable-next-line no-process-env
if (process.env.VERCEL_ENV === 'production') {
execSync('prisma db push', { stdio: 'inherit' });
}

0 comments on commit 3acb734

Please sign in to comment.