forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: run migrations conditionally (calcom#13925)
Co-authored-by: Keith Williams <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
- Loading branch information
1 parent
a4bc079
commit 502a324
Showing
6 changed files
with
75 additions
and
27 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
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,42 @@ | ||
import dotEnv from "dotenv"; | ||
import { exec as execCb } from "node:child_process"; | ||
import { promisify } from "node:util"; | ||
|
||
import { isPrismaAvailableCheck } from "./is-prisma-available-check"; | ||
|
||
dotEnv.config({ path: "../../.env" }); | ||
|
||
const exec = promisify(execCb); | ||
|
||
/** | ||
* TODO: re-write this when Prisma.io gets a programmatic migration API | ||
* Thanks to @olalonde for the idea. | ||
* @see https://github.com/prisma/prisma/issues/4703#issuecomment-1447354363 | ||
*/ | ||
async function main(): Promise<void> { | ||
if (!process.env.DATABASE_URL) { | ||
console.info("No DATABASE_URL found, skipping migrations"); | ||
return; | ||
} | ||
if (!process.env.DATABASE_DIRECT_URL) { | ||
console.info("No DATABASE_DIRECT_URL found, skipping migrations"); | ||
return; | ||
} | ||
if (!(await isPrismaAvailableCheck())) { | ||
console.info("Prisma can't be initialized, skipping migrations"); | ||
return; | ||
} | ||
// throws an error if migration fails | ||
const { stdout, stderr } = await exec("yarn prisma migrate deploy", { | ||
env: { | ||
...process.env, | ||
}, | ||
}); | ||
console.log(stdout); | ||
console.error(stderr); | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e.stdout || e.stderr || e.message); | ||
process.exit(1); | ||
}); |
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,17 @@ | ||
import { Prisma } from "@prisma/client"; | ||
|
||
import prisma from "."; | ||
|
||
export async function isPrismaAvailableCheck() { | ||
try { | ||
await prisma.$queryRaw`SELECT 1`; | ||
return true; | ||
} catch (e: unknown) { | ||
if (e instanceof Prisma.PrismaClientInitializationError) { | ||
// Database might not available at build time. | ||
return false; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} |
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