繁体   English   中英

在Rails中使用Sqlite3测试数据库和Postgres开发/生产数据库

[英]Using Sqlite3 test database and Postgres dev/production database in Rails

我目前有一个Rails项目,我将其部署到使用postgres数据库的生产服务器上。 我在Windows中开发我的Rails项目,这意味着,如果要在本地进行测试,则必须将database.yml文件中的所有数据库从postgres更改为sqlite3(因为将Windows设置为运行postgres服务器似乎是痛苦)。

我想做的是格式化我的database.yml像这样:

development:
  adapter: postgresql
  encoding: utf8
  database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
  pool: 5
  username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
  password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>

# 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:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  encoding: utf8
  database: <%= begin IO.read("/home/www-data/.db/.prod_name") rescue "" end %>
  pool: 5
  username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
  password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>

这样,我可以在本地运行rails s -e test并使用sqlite3数据库进行测试,但是当我将其部署到开发和生产服务器时,可以使用postgres。

我遇到的问题是,随着上面显示的对database.yml的更改,当我在本地运行rails s -e test ,我收到一条错误消息,指出Rails找不到pg gem,这似乎暗示它仍在尝试使用开发服务器或生产服务器。

确认所有警告后,问题的答案将是在Gemfile中使用group,例如

gem 'pg', group: [:development, :production]
gem 'sqlite3', group: :test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM