我正在处理现有的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数据库的连接的主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
任何指向校正方向的轻推或指针都将非常有帮助。
发布于 2015-07-21 04:07:04
你可以有一些东西,比如:
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_productionhttps://stackoverflow.com/questions/31524260
复制相似问题