Skip to content

Instantly share code, notes, and snippets.

@ataube
Created December 22, 2016 09:35
Show Gist options
  • Save ataube/c6545deee23ec49642a09d7f1dca23b5 to your computer and use it in GitHub Desktop.
Save ataube/c6545deee23ec49642a09d7f1dca23b5 to your computer and use it in GitHub Desktop.
NodeJs db migration wrapper based on Go mattes/migrate
const exec = require('child_process').execFile;
const path = require('path');
const config = require('config');
const url = config.get('datasource');
const command = process.argv[2] || 'up';
const basePath = path.join(process.cwd(), 'migrations')
const args = [`-url=${url}`, '-path=./sql', command];
/*
executes the migration cli => https://github.com/mattes/migrate
Folders:
sql/0001_init.down.sql
sql/0001_init.up.sql
...
*/
const migrate = exec(`./migrate`, args, { cwd: basePath }, (err, stdout, stderr) => {
if (err) {
throw err;
}
});
migrate.stdout.on('data', data => {
console.log(data);
});
migrate.stderr.on('data', data => {
console.error(data);
});
migrate.on('exit', () => {
process.exit();
});
{
"scripts": {
"migrate": "node migrations/migrate.js up"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment