forked from aws-samples/prisma-lambda-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration-runner.ts
More file actions
44 lines (39 loc) · 1.62 KB
/
Copy pathmigration-runner.ts
File metadata and controls
44 lines (39 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Handler } from "aws-lambda";
import { execFile } from "child_process";
import path from "path";
export const handler: Handler = async (event, _) => {
// Available commands are:
// deploy: create new database if absent and apply all migrations to the existing database.
// reset: delete existing database, create new one, and apply all migrations. NOT for production environment.
// If you want to add commands, please refer to: https://www.prisma.io/docs/concepts/components/prisma-migrate
const command: string = event.command ?? "deploy";
let options: string[] = [];
if (command == "reset") {
// skip confirmation and code generation
options = ["--force", "--skip-generate"];
}
// Currently we don't have any direct method to invoke prisma migration programmatically.
// As a workaround, we spawn migration script as a child process and wait for its completion.
// Please also refer to the following GitHub issue: https://github.com/prisma/prisma/issues/4703
try {
const exitCode = await new Promise((resolve, _) => {
execFile(
path.resolve("./node_modules/prisma/build/index.js"),
["migrate", command].concat(options),
(error, stdout, stderr) => {
console.log(stdout);
if (error != null) {
console.log(`prisma migrate ${command} exited with error ${error.message}`);
resolve(error.code ?? 1);
} else {
resolve(0);
}
},
);
});
if (exitCode != 0) throw Error(`command ${command} failed with exit code ${exitCode}`);
} catch (e) {
console.log(e);
throw e;
}
};