【发布时间】:2020-07-09 04:58:38
【问题描述】:
我的应用使用 Express、Knex 和 PG 库。我已经在 Heroku 中设置了我的 NODE_ENV 以使用命令 https://devcenter.heroku.com/articles/nodejs-support#runtime-behavior (source) 进行暂存:
Setting NODE_ENV and restarting
NODE_ENV: staging
我在 knexfile 中有一个暂存环境:
require('dotenv').config();
module.exports = {
test: {
client: 'pg',
connection: process.env.DB_URL_TEST,
migrations: {
directory: './db/migrations',
},
seeds: {
directory: './db/seeds/dev',
},
useNullAsDefault: true,
},
development: {
client: 'pg',
connection: process.env.DB_URL,
migrations: {
directory: './db/migrations',
},
seeds: {
directory: './db/seeds/dev',
},
useNullAsDefault: true,
},
staging: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './db/migrations',
},
seeds: {
directory: './db/seeds/dev',
},
useNullAsDefault: true,
},
production: {
client: 'pg',
connection: process.env.DB_URL_PRODUCTION,
migrations: {
directory: './db/migrations',
},
seeds: {
directory: './db/seeds/production',
},
useNullAsDefault: true,
},
};
我的数据库配置文件监听NODE_ENV:
const knex = require('knex');
const config = require('../knexfile');
const dbEnv = process.env.NODE_ENV;
module.exports = knex(config[dbEnv]);
Heroku 中没有其他配置变量:
当我运行 heroku 迁移时,它一直默认为生产环境:
Running knex migrate:latest
Using environment: production
我发现强制迁移到 staging env 的唯一方法是将 NODE_ENV 显式设置为 Heroku env 变量。我不明白为什么在运行迁移时没有拾取暂存环境。
编辑:将第一个和第三个屏幕截图转换为文本。不能真正改变另一个,它只是Heroku env变量的一部分的截图。
** 编辑 2**:添加了将 NODE_ENV 设置为暂存的命令
【问题讨论】:
-
Please don't post screenshots of text。它们无法被搜索或复制,并且可用性差。相反,将代码作为文本直接粘贴到您的问题中。如果选择它并单击
{}按钮或Ctrl+K,则代码块将缩进四个空格,这将导致其呈现为代码。 -
“我已经在 Heroku 中设置了我的 NODE_ENV 以进行暂存”——具体如何?
-
@Chris 谢谢,编辑了原始帖子删除了几个屏幕截图。我已经设置了我的 3 个开发环境:在我的机器中本地,在 Heroku 中暂存和在 Heroku 中生产。每个都有一个 PG DB:local 和最后一个 Heroku PG,因此我想区分 Heroku 中的两个 env。如果您认为这不是最佳方案,欢迎提出想法
-
感谢您更新您的问题,但您仍未回答我的问题:您如何将
NODE_ENV设置为暂存? -
@Chris 'heroku config:set NODE_ENV=staging' 来源:devcenter.heroku.com/articles/nodejs-support#runtime-behavior,我也编辑了添加命令的问题
标签: postgresql heroku knex.js heroku-postgres