在同一命令中运行Sequelize Migration和Node Server无法启动服务器 [英] Running Sequelize Migration and Node Server in Same Command Won't Start Server Up

查看:151
本文介绍了在同一命令中运行Sequelize Migration和Node Server无法启动服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试运行序列迁移,然后使用同一命令运行Node服务器,则会遇到服务器永远无法启动的问题.如果迁移之前已经运行过,则sequelize db:migrate命令不会超过未执行迁移,数据库架构已经更新".消息,并且我的第二个命令永远无法运行.如果以前没有进行过迁移,则一切将按顺序正常运行.

If I try to run my sequelize migrations and then run my Node server in the same command, I run into the issue of my server never starting up. If the migrations have already been run before, the sequelize db:migrate command doesn't go past the "No migrations were executed, database schema was already up to date." message, and my second command is never able to run. If the migration has not run before, everything runs properly in sequence.

这是我的npm start命令:sequelize db:migrate && node index.js

我假定在显示此日志消息的情况下,内部sequelize db:migrate不能解决任何问题,那么有什么办法可以让我在一段时间后终止"该命令并继续执行我的node命令?

I assume that internally sequelize db:migrate is not resolving anything in the case where this log message is shown, so is there a way I can "terminate" this command after some time and proceed to my node command?

推荐答案

对于遇到此问题的其他人,这就是我最终解决它的方式.

For anyone else running into this issue, this is how I ended up solving it.

1)创建一个新文件,该文件将在npm脚本中运行.

1) Create a new file that you will run in your npm script.

2)我最终将流程调用包装在child_process exec中,然后在收到上述console.log消息时终止了该流程,因为该库本身此时无法解决任何问题.

2) I ended up wrapping the process call in a child_process exec, and then terminated the process when I received the above console.log message since the library itself does not resolve anything at this point.

// myRuntimeFile.js --> Make sure this file is in the same directory where your .sequelizerc file lives

(async()=> {
  const { exec } = require('child_process');

  await new Promise((resolve, reject) => {
    const migrate = exec(
      'sequelize db:migrate',
      { env: process.env },
      (err, stdout, stderr) => {
        resolve();
      }
    );

    // Listen for the console.log message and kill the process to proceed to the next step in the npm script
    migrate.stdout.on('data', (data) => {
      console.log(data);
      if (data.indexOf('No migrations were executed, database schema was already up to date.') !== -1) {
        migrate.kill();
      }
    });
  });
})();

上面的代码显然不是理想的,但是希望这只是暂时的,直到在promise中正确解决此边缘情况的内部问题为止.

Obviously the above code is not ideal, but hopefully this is just temporary until the internals of this edge case are resolved properly in a promise.

3)使用以下命令更新您的npm脚本:

3) Update your npm script with the following:

"start": "node myRuntimeFile.js && node index.js"

或者如果您在Windows计算机上运行并且不能使用&&,则可以使用npm-run-all库.

Or if you are running on a Windows machine and cannot use &&, you can use the npm-run-all library.

这篇关于在同一命令中运行Sequelize Migration和Node Server无法启动服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆