![](/img/trans.png)
[英]Ruby on Rails: How to set a database timeout in application configuration?
[英]How to set up test database configuration in Rails
我正在研究現有的Rails項目,並且剛剛完成一些基本的RSpec
測試。 在我們的database.yml
文件中,我們可以連接3-4個不同的數據庫,所有數據庫都指向production
數據庫或staging
數據庫。 對於測試環境,我創建了另一個名為database.test.yml
yml
文件,然后將其符號鏈接到database.yml
文件,以便創建正確的測試數據庫連接。
我的database.test.yml
看起來像這樣:
abc:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
database: abc
pqr:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
database: pqr
test:
adapter: mysql2
host: localhost
reconnect: true
username: monty
password: python
pool: 50
database: testing
我現在的問題是,如何強制其他開發人員/測試人員使用此database.test.yml
而不意外地運行包含與staging
數據庫和production
數據庫的連接的main database.yml
文件。
編輯:
為了進一步闡明我的問題,我在連接到不同數據庫的各種模型中都有establish_connection
連接。 因此,我不確定早期的建議會解決我的問題。
例:
class Abc < ActiveRecord::Base
establish_connection :abc
end
我可以想到的解決問題的一種方法是執行以下操作:
class Abc < ActiveRecord::Base
establish_connection "abc_#{Rails.env}"
end
並在database.yml
:
defaults: &defaults
adapter: mysql2
encoding: utf8
username: root
password: secret
abc_production:
database: abc
<<: *defaults
abc_development:
database: abc_devlopment
<<: *defaults
abc_test:
database: abc_test
<<: *defaults
但是我不確定這是否是最佳實踐,並且我真的不覺得更改應用程序代碼來設置測試環境是否是一種好習慣。 這個想法基於: http : //blog.nistu.de/2012/03/25/multi-database-setup-with-rails-and-rspec
任何輕推或指向校正方向的指針將大有幫助。
您可能會有一些東西,例如:
default: &default
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
development:
<<: *default
database: xxxx_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: xxxx_test
production:
<<: *default
database: xxxx_production
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.