Carlos
11/08/2021, 1:01 AMDan Borstelmann
11/08/2021, 1:36 AMDan Borstelmann
11/08/2021, 1:37 AMimport { execFile } from 'child_process';
import path from 'path';
const SCHEMA_PATH = process.env.SERVICE_SCHEMA_PATH;
export interface MigrationsResponse {
body: string;
statusCode: number;
}
export const coreDeploy = async (): Promise<MigrationsResponse> => {
const command = 'deploy';
const options: string[] = [`--schema=./${SCHEMA_PATH}`];
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: ---------');
console.log(stdout);
console.log('STDOUT END ---------');
console.log('STDERR: ---------');
console.log(stderr);
console.log('STDERR END ---------');
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}`);
}
const response = {
body: JSON.stringify('Migrations successfully applied.'),
statusCode: 200,
};
console.log('Response: ' + JSON.stringify(response));
return response;
};
Dan Borstelmann
11/08/2021, 1:38 AMDan Borstelmann
11/08/2021, 1:41 AMDan Borstelmann
11/08/2021, 1:41 AMorg: dborstelmann
app: TODO
service:
name: TODO
provider:
stage: dev
region: us-east-2
name: aws
runtime: nodejs14.x
timeout: 10
vpc: TODO
environment:
SERVICE_DATABASE_URL: TODO?schema=public&connection_limit=1
SERVICE_SCHEMA_PATH: TODO/src/schema.prisma
functions:
deployMigrations:
handler: TODO/dist/migrations.coreDeploy
events:
- http:
path: /migrate
method: post
cors: true
package:
patterns:
- node_modules/@prisma/engines/migration-engine-rhel-*
- node_modules/@prisma/engines/libquery_engine-rhel-*
- node_modules/prisma/libquery_engine-rhel*
package:
individually: true
patterns:
- '!node_modules/typescript'
- '!node_modules/.bin/prisma*'
- '!node_modules/.prisma/client/libquery_engine-*'
- '!node_modules/@prisma/client/generator-build'
- '!node_modules/@prisma/engines/introspection-engine-*'
- '!node_modules/@prisma/engines/libquery_engine-*'
- '!node_modules/@prisma/engines/migration-engine-*'
- '!node_modules/@prisma/engines/prisma-fmt-*'
- '!node_modules/prisma/libquery_engine-*'
Dan Borstelmann
11/08/2021, 1:41 AMDan Borstelmann
11/08/2021, 1:42 AMimport axios from "axios";
process.on('unhandledRejection', (error) => {
console.log(`Failed to migrate database at: ${process.argv[2]}`);
console.log(error);
process.exit(1);
});
const migrate = async () => {
const response = await <http://axios.post|axios.post>(process.argv[2], {
headers: {
'Content-Type': 'application/json'
},
});
if (response.status !== 200) {
throw new Error(response);
}
console.log(`Successfully migrated database at: ${process.argv[2]}`);
};
migrate();
Dan Borstelmann
11/08/2021, 1:43 AMDan Borstelmann
11/08/2021, 1:43 AM{
"dependencies": {
"@prisma/client": "^3.4.1",
"prisma": "^3.4.0"
}
}
Dan Borstelmann
11/08/2021, 1:44 AMCarlos
11/08/2021, 1:45 AMDan Borstelmann
11/08/2021, 1:46 AMDan Borstelmann
11/08/2021, 1:46 AMDan Borstelmann
11/08/2021, 1:47 AMDan Borstelmann
11/08/2021, 1:47 AMDan Borstelmann
11/08/2021, 1:47 AMCarlos
11/08/2021, 1:51 AMCarlos
11/08/2021, 2:55 AMDan Borstelmann
11/08/2021, 2:59 AM