From 14ac8c6cd10f50d5c6382ba041d67e1843a812e5 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 10:42:49 -0400 Subject: [PATCH] chore: Stop running migrations automatically, run migrations by hand. (#452) * Cause migrations to be manually applied by default * Log current server name * Update apollos-church-api/package.json * Update migration documentation * Update README.md * Update README.md * Github editor + find in file = text getting butchered Co-authored-by: Michael Neeley --- README.md | 13 +++++++++++++ apollos-church-api/package.json | 1 + apollos-church-api/src/migrator.js | 31 ++++++++++++++++++++++++++++++ apollos-church-api/src/server.js | 18 ++++++++++++++++- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 apollos-church-api/src/migrator.js diff --git a/README.md b/README.md index 280758cc9..62dde8316 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,19 @@ heroku open To get started with different API integrations, check out our [docs](https://apollosapp.io)! +#### Migrations + +Database migrations can be run locally via +``` +yarn migrator up +``` +and in production / heroku via + +``` +heroku --app YOUR_HEROKU_APP_NAME_HERE run yarn migrator up +``` +**Make sure you `yarn build` before running `yarn migrator` if you have made any changes to your app.** + ### Mobile App This will outline the steps required to get your Android and iOS apps up and running. You will need a functioning production API from the previous section before moving forward. diff --git a/apollos-church-api/package.json b/apollos-church-api/package.json index 2ba49e358..dde0114c4 100644 --- a/apollos-church-api/package.json +++ b/apollos-church-api/package.json @@ -5,6 +5,7 @@ "main": "lib/index.js", "scripts": { "setup": "./scripts/init.sh", + "migrator": "node ./lib/migrator.js", "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", diff --git a/apollos-church-api/src/migrator.js b/apollos-church-api/src/migrator.js new file mode 100644 index 000000000..72366cf84 --- /dev/null +++ b/apollos-church-api/src/migrator.js @@ -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'); + } +})(); diff --git a/apollos-church-api/src/server.js b/apollos-church-api/src/server.js index d334a0525..ae3b1cf40 100644 --- a/apollos-church-api/src/server.js +++ b/apollos-church-api/src/server.js @@ -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(); } })();