Created
December 22, 2016 09:35
-
-
Save ataube/c6545deee23ec49642a09d7f1dca23b5 to your computer and use it in GitHub Desktop.
NodeJs db migration wrapper based on Go mattes/migrate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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