Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

chore: Stop running migrations automatically, run migrations by hand. #452

Merged
merged 11 commits into from
Aug 17, 2021
2 changes: 2 additions & 0 deletions apollos-church-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "lib/index.js",
"scripts": {
"setup": "./scripts/init.sh",
"migrator": "node ./lib/migrator.js",
"migrator:production": "heroku --app YOUR_HEROKU_APP_NAME_HERE run yarn migrator up",
vinnyjth marked this conversation as resolved.
Show resolved Hide resolved
"start": "NODE_ENV=production node ./lib/index.js",
"start:dev": "linkemon ./src/index.js --exec babel-node --delay 2 -e js,yaml,json,yml",
"postinstall": "yarn build",
Expand Down
31 changes: 31 additions & 0 deletions apollos-church-api/src/migrator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import dotenv from 'dotenv/config'; // eslint-disable-line
import '@apollosproject/data-connector-postgres/lib/postgres/pgEnum-fix';
import config from './config'; // eslint-disable-line

import ApollosConfig from '@apollosproject/config';
import { createMigrationRunner } from '@apollosproject/data-connector-postgres';

let dataObj;

if (ApollosConfig?.DATABASE?.URL) {
dataObj = require('./data/index.postgres');
} else {
dataObj = require('./data/index');
}

const { migrations } = dataObj;

// make sure this is called last.
// (or at least after the apollos server setup)
(async () => {
if (ApollosConfig?.DATABASE?.URL) {
try {
const migrationRunner = await createMigrationRunner({ migrations });
migrationRunner.runAsCLI();
} catch (e) {
console.log(e);
}
} else {
console.warn('Please specify a database URL to perform migrations');
}
})();
18 changes: 17 additions & 1 deletion apollos-church-api/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,23 @@ apolloServer.applyMiddleware({ app, path: '/' });
(async () => {
if (ApollosConfig?.DATABASE?.URL) {
const migrationRunner = await createMigrationRunner({ migrations });
await migrationRunner.up();
const pending = await migrationRunner.pending();
if (pending.length) {
console.log('\x1b[31m', '██████████████████████████████████', '\x1b[0m');
console.log(
'\x1b[36m',
'You currently have a number of pending migrations',
'\x1b[0m'
);
console.log(pending);
console.log(
`Keep in mind, you are currently connected to ${
migrationRunner?.options?.context?.sequelize?.options?.host
}`
);
console.log('\x1b[31m', '██████████████████████████████████', '\x1b[0m');
}
if (ApollosConfig.AUTO_MIGRATE) await migrationRunner.up();
}
})();

Expand Down