javascript - Knex Migrations cause gulp process to hang -


using migrations api in knex.js inside gulp task causes task process hang , not exit. causes , how can fix it?

gulp.task('migrate:latest', function () {      return knex.migrate.latest({         migrations: {             tablename: 'migrations'         }     })         .then(function () {             return knex.migrate.currentversion();         })         .then(function (version) {             console.log("kicked database version: " + version);         })         .catch(function (err) {             console.error(err);         }); }); 

it looks though knex keeps reference open database connection, doesn't automatically destroy after migration completes - causes process hang. fix this, call knex.destroy after migration resolves. allow gulp process exit normally.

documentation on knex's connection pooling , explicit destroy command here.

the gulp task becomes this:

gulp.task('migrate:latest', function () {         return knex.migrate.latest({         migrations: {             tablename: 'migrations'         }     })         .then(function () {             return knex.migrate.currentversion();         })         .then(function (version) {             console.log("kicked database version: " + version);             knex.destroy();         })         .catch(function (err) {             console.error(err);             knex.destroy();         }); }); 

as note, if configure knex variable in gulpfile, happen of tasks if task doesn't use knex instance. solution define knex configuration function, , call when need it, so:

var knex = function () {     return require('knex')({         client: 'postgresql',         connection: {             host: process.env.db_hostname,             user: process.env.db_username,             password: process.env.db_password,             database: process.env.db_database,             charset: 'utf8'         },         pool: {             min: 2,             max: 10         },         migrations: {             tablename: 'migrations'         }     }); };  gulp.task('migrate:latest', function () {         return knex().migrate.latest({ // call function set config.         migrations: {             tablename: 'migrations'         }     })     ... 

this save having call knex.destroy in tasks don't need it. hope can someone.


Comments