【问题标题】:Rails does not consider `DATABASE_URL` in the `test` environment?Rails 不考虑 `test` 环境中的 `DATABASE_URL`?
【发布时间】:2019-10-10 21:44:12
【问题描述】:

我正在使用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 数据库

(即使环境为developmentdb:create 任务也会自动同时创建testdevelopment 数据库)

这是我的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 guides,您可以通过指定DATABASE_URL来覆盖上述配置,我将其设置为:

DATABASE_URL=postgres://feeder:password123@db/

所以据我所知-

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

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

那么 - 有没有办法让 Rails 在 test 环境中尊重我的 DATABASE_URL ENV?

【问题讨论】:

    标签: ruby-on-rails postgresql docker


    【解决方案1】:

    在您的 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"
    }
    

    请注意,数据库名称保持不变,但主机已被覆盖。

    【讨论】:

    • 哇,多么简单的解决方案 - 谢谢! test 默认不尊重 DATABASE_URL 看起来不是很奇怪吗?必须明确指定这一点感觉违反直觉。
    【解决方案2】:

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

    test:
      <<: *default
      database: <%= ENV['DATABASE_URL'] || 'feeder_test' %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多