From fb5b5c943143bad9b978731ddcd8ddfd5831db17 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 09:30:19 -0400 Subject: [PATCH 1/7] Cause migrations to be manually applied by default --- apollos-church-api/package.json | 2 ++ apollos-church-api/src/migrator.js | 31 ++++++++++++++++++++++++++++++ apollos-church-api/src/server.js | 13 ++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 apollos-church-api/src/migrator.js diff --git a/apollos-church-api/package.json b/apollos-church-api/package.json index 2feee9ccb..dbf4d92fb 100644 --- a/apollos-church-api/package.json +++ b/apollos-church-api/package.json @@ -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", "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..27c7271b8 100644 --- a/apollos-church-api/src/server.js +++ b/apollos-church-api/src/server.js @@ -84,7 +84,18 @@ 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('\x1b[31m', '██████████████████████████████████', '\x1b[0m'); + } + if (ApollosConfig.AUTO_MIGRATE) await migrationRunner.up(); } })(); From 1494017a1f44a8b70f73541971cf32a9d0830231 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 09:36:09 -0400 Subject: [PATCH 2/7] Log current server name --- apollos-church-api/src/server.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apollos-church-api/src/server.js b/apollos-church-api/src/server.js index 27c7271b8..ae3b1cf40 100644 --- a/apollos-church-api/src/server.js +++ b/apollos-church-api/src/server.js @@ -93,6 +93,11 @@ apolloServer.applyMiddleware({ app, path: '/' }); '\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(); From 765c356417e96822d45f780a3d8152ead4e211e1 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 10:13:37 -0400 Subject: [PATCH 3/7] Update apollos-church-api/package.json --- apollos-church-api/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/apollos-church-api/package.json b/apollos-church-api/package.json index dbf4d92fb..adea13aaf 100644 --- a/apollos-church-api/package.json +++ b/apollos-church-api/package.json @@ -6,7 +6,6 @@ "scripts": { "setup": "./scripts/init.sh", "migrator": "node ./lib/migrator.js", - "migrator:production": "heroku --app YOUR_HEROKU_APP_NAME_HERE run yarn migrator up", "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", From 96b9672f696b74682d193a93785c6928491b19b8 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 10:15:55 -0400 Subject: [PATCH 4/7] Update migration documentation --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 280758cc9..03ac567c2 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. From d592c500178ac54cabed438ef4c21fccee8ed6c8 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 10:17:21 -0400 Subject: [PATCH 5/7] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 03ac567c2..139e27940 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,8 @@ heroku open To get started with different API integrations, check out our [docs](https://apollosapp.io)! -#### Migrations +#### +rations Database migrations can be run locally via ``` @@ -93,7 +94,7 @@ 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. +**Make sure you `yarn build` before running `yarn migrator` if you have made any changes to your app.** ### Mobile App From 3a30067e00668970c7eaa5fd912e55178360d276 Mon Sep 17 00:00:00 2001 From: Michael Neeley Date: Tue, 17 Aug 2021 10:19:20 -0400 Subject: [PATCH 6/7] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 139e27940..62dde8316 100644 --- a/README.md +++ b/README.md @@ -82,8 +82,7 @@ heroku open To get started with different API integrations, check out our [docs](https://apollosapp.io)! -#### -rations +#### Migrations Database migrations can be run locally via ``` From 64b4d1400f12b254d407765623b10243c88867a3 Mon Sep 17 00:00:00 2001 From: Vincent Wilson Date: Tue, 17 Aug 2021 10:19:38 -0400 Subject: [PATCH 7/7] Github editor + find in file = text getting butchered