Rails 在“test”环境中不考虑“DATABASE_URL”?

use*_*003 6 postgresql ruby-on-rails docker

我正在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'
Run Code Online (Sandbox Code Playgroud)

可以看到,创建数据库成功development,但是创建数据库失败test

(即使环境是这样,db:create任务也会自动创建test数据库)developmentdevelopment

这是我的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
Run Code Online (Sandbox Code Playgroud)

根据Rails 指南,您可以通过指定 a 来覆盖上面的配置DATABASE_URL,我将其设置为:

DATABASE_URL=postgres://feeder:password123@db/
Run Code Online (Sandbox Code Playgroud)

据我所知 -

  1. Rails在尝试创建数据库时将DATABASE_URL信息与信息合并。一切都很好。database.ymldevelopment

  2. DATABASE_URLRails在创建数据库时根本不考虑这一点test。它仅使用database.yml信息,这就是它在 上寻找 psql 连接的原因localhost

那么 - 有没有办法让 Rails 尊重我的DATABASE_URLENV 环境test

jer*_*ith 1

我很确定您可以将 ERB 放入 database.yml 文件中,所以我建议更改test

test:
  <<: *default
  database: <%= ENV['DATABASE_URL'] || 'feeder_test' %>
Run Code Online (Sandbox Code Playgroud)