Skip to content

Commit

Permalink
Merge pull request #462 from getodk/ktuite/migrate-fail
Browse files Browse the repository at this point in the history
Check for pending migrations before running server
  • Loading branch information
ktuite authored Mar 8, 2022
2 parents e857686 + 59f055d commit 3462cd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ node_modules:
migrations: node_modules
node -e 'const { withDatabase, migrate } = require("./lib/model/migrate"); withDatabase(require("config").get("default.database"))(migrate);'

base: node_modules migrations
check-migrations: node_modules
node -e 'const { withDatabase, checkMigrations } = require("./lib/model/migrate"); withDatabase(require("config").get("default.database"))(checkMigrations);'

base: node_modules migrations check-migrations

run: base
node lib/bin/run-server.js
Expand Down
10 changes: 9 additions & 1 deletion lib/model/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ const withDatabase = (config) => (mutator) => {
// Given a database, initiates migrations on it.
const migrate = (db) => db.migrate.latest({ directory: `${__dirname}/migrations` });

module.exports = { connect, withDatabase, migrate };
// Checks for pending migrations and returns an exit code of 1 if any are
// still pending/unapplied (e.g. automatically running migrations just failed).
const checkMigrations = (db) => db.migrate.list({ directory: `${__dirname}/migrations` })
.then((res) => {
if (res[1].length > 0)
process.exitCode = 1;
});

module.exports = { checkMigrations, connect, withDatabase, migrate };

0 comments on commit 3462cd5

Please sign in to comment.