Javascript Knex迁移导致吞咽过程暂停

Javascript Knex迁移导致吞咽过程暂停,javascript,gulp,knex.js,Javascript,Gulp,Knex.js,在吞咽任务中使用in会导致任务进程挂起而不退出。这是什么原因造成的?我如何修复它 gulp.task('migrate:latest', function () { return knex.migrate.latest({ migrations: { tableName: 'migrations' } }) .then(function () { return knex.migrat

在吞咽任务中使用in会导致任务进程挂起而不退出。这是什么原因造成的?我如何修复它

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 to version: " + version);
        })
        .catch(function (err) {
            console.error(err);
        });
});

看起来Knex保留了对开放数据库连接的引用,在迁移完成后不会自动销毁该连接,这会导致进程挂起。要解决此问题,请在迁移解决后调用
knex.destroy
。这将允许吞咽过程正常退出

关于knex的连接池和显式销毁命令的文档如下所示

吞咽任务变成这样:

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 to version: " + version);
            knex.destroy();
        })
        .catch(function (err) {
            console.error(err);
            knex.destroy();
        });
});
需要注意的是,如果您将knex配置为gulpfile中的变量,那么即使您的所有任务不使用knex实例,也会发生这种情况。解决方案是将knex配置定义为一个函数,然后在需要时调用它,如下所示:

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 the function to set up your config.
        migrations: {
            tableName: 'migrations'
        }
    })
    ...
这样可以避免在不需要的任务中调用knex.destroy。希望这能帮助别人