gpt4 book ai didi

ruby-on-rails - Rails 不考虑 `DATABASE_URL` 环境中的 `test`?

转载 作者:行者123 更新时间:2023-11-29 12:49:00 25 4
gpt4 key购买 nike

我正在使用 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'

如您所见,它成功创建了开发数据库,但是失败创建了测试数据库

(即使环境是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?

最佳答案

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

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

关于ruby-on-rails - Rails 不考虑 `DATABASE_URL` 环境中的 `test`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58331487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com