Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
DATABASE_URL: ${{ secrets.DATABASE_URL }}

- name: Run database migrations
run: npm run db:migrate
run: npm run db:migrate:retry
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
DATABASE_URL: ${{ secrets.DATABASE_URL }}

- name: Run database migrations
run: npm run db:migrate
run: npm run db:migrate:retry
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"build": "next build",
"db:migrate": "prisma format && prisma migrate dev",
"db:migrate:retry": "prisma format && node scripts/run-migrations.js",
"db:reset": "prisma migrate reset",
"db:view": "prisma studio",
"dev": "next dev",
Expand Down
46 changes: 46 additions & 0 deletions scripts/run-migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { exec } = require('child_process');

const MIGRATION_COMMAND = 'npx prisma migrate deploy';
const MAX_RETRIES = 3;
const INITIAL_DELAY_MS = 2000;

let attempt = 0;

function runMigration() {
attempt++;
console.log(`\n🚀 Attempt ${attempt}: Running database migrations...`);

const child = exec(MIGRATION_COMMAND);

child.stdout.on('data', (data) => {
process.stdout.write(data);
});

child.stderr.on('data', (data) => {
process.stderr.write(data);
});

child.on('exit', (code) => {
if (code === 0) {
console.log('\n✅ Database migration successful!');
process.exit(0);
} else {
console.error(
`\n❌ Attempt ${attempt}: Migration failed with exit code ${code}.`
);
if (attempt < MAX_RETRIES) {
const delay = INITIAL_DELAY_MS * Math.pow(2, attempt - 1);
console.log(`\n⏳ Retrying in ${delay / 1000} seconds...`);
setTimeout(runMigration, delay);
} else {
console.error(
`\n🚨 All ${MAX_RETRIES} migration attempts failed. Aborting.`
);
process.exit(1);
}
}
});
}

runMigration();