-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
50 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters