簡體   English   中英

如何有選擇地從Gemfile中排除gem pg?

[英]How to optionally exclude gem pg from the Gemfile?

在我們的團隊中,有些人的機器上沒有安裝pg gem。 目前,我們使用兩個單獨的數據庫配置,這些配置將被復制到database.yml。 我們在使用該方法時遇到了問題,因為我們不得不在Gemfile中不斷注釋掉gem pg。 所以我嘗試在我們的Gemfile中執行以下操作:

unless ['host1, 'host2'].include? `hostname`.strip!
  gem 'pg'
end

它似乎可行,但老板希望有一個更好的解決方案,以便他可以在筆記本電腦上測試該應用程序,而不必安裝Postgres且在Gemfile中沒有其主機名。

Gem::Specification.all_names

盡管'gem list pg --local'顯示已安裝pg,但未顯示正在安裝pg。

嘗試在gem list pg --local中使用gem list pg --local ,因為如果未安裝pg,系統似乎會陷入無限循環。

是否有類似於'Gem :: Specification.all_names'的內容正確顯示了已安裝的寶石的列表,這些寶石可用於可選的排除Gemfile中的寶石。 還是針對上述情況,在不同的機器上使用巨型寶石的更好方法?

注意

if File.open('./config/database.yml').read.each_line.first.index('Postgre').is_a?(Integer)
 gem 'pg'
end

似乎可以正常工作,但是現在我在運行捆綁安裝時得到了以下信息:

Your bundle is complete!
Gems in the group postgres were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

知道它來自哪里嗎?

筆記2

運行后修復了“未安裝postgres組中的寶石”的問題:rm -r ./.bundle

一種可能的解決方案是使用自定義環境和捆綁程序組。

您可能已經在config/application.rb注意到這一行:

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

這意味着運行rails s -e bossmode將需要以下寶石:

group :bossmode do
  # no pg in here...
end

更好的解決方案是說服您的老板使用KISS並使用PG。 即使Macbook播放速度很慢,性能成本也很小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM