Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 弹性豆茎上的knex迁移_Node.js_Amazon Web Services_Amazon Elastic Beanstalk_Knex.js - Fatal编程技术网

Node.js 弹性豆茎上的knex迁移

Node.js 弹性豆茎上的knex迁移,node.js,amazon-web-services,amazon-elastic-beanstalk,knex.js,Node.js,Amazon Web Services,Amazon Elastic Beanstalk,Knex.js,我只是将node.js项目放在elastic beanstalk上,并对其进行了正确配置 我现在通过knex插件将应用程序连接到我的RDS postgres DB 在本地,要运行knex迁移来更新本地数据库,我只需要在控制台“knex migrate:latest”中运行它,但是这对elastic beanstalk不起作用,因为我不能从项目文件夹中运行命令(至少我认为我不能) 如何在elastic beanstalk应用程序上运行knex命令 请记住,我对elastic beanstalk非常

我只是将node.js项目放在elastic beanstalk上,并对其进行了正确配置

我现在通过knex插件将应用程序连接到我的RDS postgres DB

在本地,要运行knex迁移来更新本地数据库,我只需要在控制台“knex migrate:latest”中运行它,但是这对elastic beanstalk不起作用,因为我不能从项目文件夹中运行命令(至少我认为我不能)

如何在elastic beanstalk应用程序上运行knex命令


请记住,我对elastic beanstalk非常熟悉。

elastic beanstalk将运行package.json文件中的
启动前
启动后
脚本

{
    "name": "...",
    "version": "1.0.0",
    "description": "...",
    "scripts": {
        "prestart": "node ./node_modules/knex/lib/bin/cli.js migrate:latest",
        "poststart": "..."
    }
}
或者,您可以在启动服务器之前在代码中运行迁移:

knex.migrate.latest([config]) 

经过数小时的搜索,我的工作安排如下:

.ebextensions/01-migrate-db.config

container_commands:
  01_node_binary:
    command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
    leader_only: true
  02_npm_binary:
    command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
    leader_only: true
  03_migrate_db:
    command: "sudo DB_HOST=${DB_HOST} DB_PORT=${DB_PORT} DB_NAME=${DB_NAME} DB_USER=${DB_USER} DB_PASSWORD=${DB_PASSWORD} npm run db:migration:run"
    leader_only: true
package.json

...
scripts: {
  "db:migration:run": "knex migrate:latest"
}

如果您使用的npm版本大于
5.2
。它通常附带一个
npx
包,使您能够从本地项目
节点模块运行脚本/库,而不是从全局安装的模块运行脚本/库。这使得直接从项目目录运行
knex
变得容易。无需在预安装脚本中全局安装knex软件包。您可以简单地使用如下节点命令脚本:

"scripts": {
  "start": "npx knex migrate:latest && node server.js"
}

小更改…bin文件夹不在lib目录中(至少在我的knex 0.15.2版本中)<代码>“预启动”:“node./node\u modules/knex/bin/cli.js migrate:latest”
也可以只运行
knex migrate:latest
,而不运行node\u modules。但是,
prestart
是这样做的正确地方吗?如果存在表冲突,服务器将无法启动。你们正在使用
createTableIfNotExists
?据说它已被弃用,因此不确定如何处理已经存在的表。我想这对于多实例设置并不理想