Skip to content

Instantly share code, notes, and snippets.

@bosunolanrewaju
Created April 19, 2020 17:03
Show Gist options
  • Save bosunolanrewaju/4e698ae7fdf59e8d25b6da6ba2a3bcbd to your computer and use it in GitHub Desktop.
Save bosunolanrewaju/4e698ae7fdf59e8d25b6da6ba2a3bcbd to your computer and use it in GitHub Desktop.
List pending migrations in Node.js using Sequelize ORM (similar to Rails)
// call this in your app entry file
// Prints:
// ==================================================
// ABORT: You have pending migrations
// ==================================================
// 1. 20200419162657-create_site_settings.js
//
// Please run `yarn migrate` to run all pending migrations
export const checkPendingMigrations = () => {
const commandResult = childProcess.execSync('npx sequelize db:migrate:status');
const commandResultArray = Buffer.from(commandResult).toString('utf-8').split('\n');
const pendingMigrations = commandResultArray.filter(line => line.startsWith('down'));
if (pendingMigrations.length) {
console.log('='.repeat(50), '\n');
console.log('ABORT: You have pending migrations', '\n');
console.log('='.repeat(50), '\n');
console.log(
pendingMigrations.map(
(line, i) => `${i + 1}. ${line.replace('down ', '')}`,
).join('\n'),
'\n\n',
);
console.log('Please run `yarn migrate` to run all pending migrations', '\n');
process.exit(-1);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment