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

Commit

Permalink
chore: Stop running migrations automatically, run migrations by hand. (
Browse files Browse the repository at this point in the history
…#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 <micneeley14@gmail.com>
  • Loading branch information
vinnyjth and redreceipt authored Aug 17, 2021
1 parent 4e284b6 commit 14ac8c6
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions apollos-church-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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

0 comments on commit 14ac8c6

Please sign in to comment.