[英]Rails does not consider `DATABASE_URL` in the `test` environment?
我正在使用docker-compose
为我的 Rails 应用程序创建两个服务:
web
- 实际的 Rails/Web 应用程序db
- 运行 postgres 11.5 的 postgres 容器我正在使用RAILS_ENV=development
在本地运行这两个服务。
作为一次性手动步骤,我想第一次尝试运行rake db:create
来创建数据库。 但是,我收到一个 postgres 连接错误:
docker-compose up --build --no-start
docker-compose run --rm web bundle exec rake db:create
RAILS_ENV=development bundle exec rake db:create
Created database 'feeder_development'
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Couldn't create 'feeder_test' database. Please check your configuration.
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
如您所见,它成功创建了development
数据库,但创建test
数据库失败
(即使环境是development
环境, db:create
任务也会自动同时创建test
和development
数据库)
这是我的database.yml
。
default: &default
adapter: postgresql
encoding: utf8
host: localhost
min_messages: warning
pool: 5
timeout: 5000
test:
<<: *default
database: feeder_test
development:
<<: *default
database: feeder_development
根据Rails 指南,您可以通过指定DATABASE_URL
来覆盖上述配置,我将其设置为:
DATABASE_URL=postgres://feeder:password123@db/
所以据我所知——
在尝试创建development
数据库时,Rails 会将DATABASE_URL
信息与database.yml
信息合并。 一切都很好。
Rails 在创建test
数据库时根本不考虑DATABASE_URL
。 它仅使用database.yml
信息,这就是它在localhost
上寻找 psql 连接的原因。
那么 - 有没有办法让 Rails 在test
环境中尊重我的DATABASE_URL
ENV?
我很确定您可以将 ERB 放入 database.yml 文件中,因此我建议将test
更改为
test:
<<: *default
database: <%= ENV['DATABASE_URL'] || 'feeder_test' %>
在您的database.yml
中将url
设置为ENV['DATABASE_URL']
并将其他键保留为默认值:
test:
url: <%= ENV['DATABASE_URL'] %>
database: app_test
host: 127.0.0.1
DATABASE_URL='postgresql://192.168.0.2/' rails runner 'puts ActiveRecord::Base.configurations.to_json' | jq '.test'
{
"database": "app_test",
"host": "192.168.0.2",
"adapter": "postgresql"
}
请注意,数据库名称保持不变,但主机已被覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.