使用Postgres安装最新版本的Rails 4 – 不推荐使用PGconn,PGresult和PGError常量

我无法在Google上找到此警告,因此请求Stackowerflower的帮助。

我想在新的Centos 7盒子上安装Rails 4.2.8。 Postgres版本是9.2.18。 Ruby版本是2.3.4。

安装Rails时,我像往常一样配置config / database.yml文件,并且非常确定database.yml文件可以成功连接到DB。 Postgres已经成功运行其他应用程序,并为此应用程序创建了新的角色。

在下一步中,存在一个实际问题:

[user@server dir]$ rake db:setup The PGconn, PGresult, and PGError constants are deprecated, and will be removed as of version 1.0. You should use PG::Connection, PG::Result, and PG::Error instead, respectively. Called from /home/user/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency' /home/rent/apps/rent/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/user/apps/rent/config/application.rb to limit the frameworks that will be loaded. [user@server dir]$ 

这是否证实Rails成功连接到Postgres? 如何简单检查一下?

如果是 – 我可以使用Rails 4.2.8使用类似的Postgres版本多长时间?

有趣的是,我没有得到类似的设置非常相似的消息,所以我想确保我能够很好地使用这个设置。

非常感谢

从pg 0.20.0升级到pg 0.21.0时,我注意到了相同的弃用警告。 我似乎没有任何实际的问题与pg和我的应用程序(开发,分期和生产)都似乎工作正常。

然而,我发现这个警告很烦人,所以我将所有的Gemfiles锁定在0.20.0。

为了避免使用旧版本而不是pg gem的0.21.0,请将此文件放在lib/pg/deprecated_constants.rb并确保您的应用程序的$LOAD_PATH配置为在pg gem的安装路径之前加载应用程序的lib/ dir中的文件。 请参阅以下评论中的进一步说明

 # File: lib/pg/deprecated_constants.rb # # This file overrides the pg gem's pg/deprecated_constants.rb file and so # its warning message is not printed. Avoiding this warning message helps # clean up the app startup and test output. # # This behaviour relies on lib/ being ahead of the pg gem in $LOAD_PATH and # these lines from the pg gem's lib/pg.rb file: # autoload :PGError, 'pg/deprecated_constants' # autoload :PGconn, 'pg/deprecated_constants' # autoload :PGresult, 'pg/deprecated_constants' # # Your config/application.rb may need to modify autoload_paths to ensure # the lib/ dir is ahead of the pg gem install path in $LOAD_PATH: # # config.autoload_paths << Rails.root.join('lib') # if ('0.21.0' != PG::VERSION) || (ActiveRecord.version.to_s != '4.2.8') puts < 

或者,如果你想使用更新的pg版本,你可以修补烦人的掠夺。 注意:这将覆盖gem包中的文件:

在应用程序根目录中创建一个文件(即bin/monkey_patch.rb )它应该如下所示:

 #bin/monkey_patch.rb if pg_path = `bundle show pg --paths`.strip pg_deprications = pg_path + '/lib/pg/deprecated_constants.rb' nag_string = 'The PGconn, PGresult, and PGError constants are deprecated, and will be removed as of version 1.0. You should use PG::Connection, PG::Result, and PG::Error instead, respectively. Called from #{callsite}' str = File.read pg_deprications if str.include? nag_string File.open(pg_deprications, 'w') {|f| f.puts str.gsub(nag_string, '')} end end 

然后在第一个require行之后的application.rb文件中添加第二行,这样可以在应用程序启动之前修补折旧。

 require File.expand_path('../boot', __FILE__) system('ruby ./bin/monkey_patch.rb') 

这是完全黑客但它的工作原理你可以使用更新版本的ruby-pg gem。 但是从我在v0.21.0 vs v0.20.0上的git diff可以看出,无论如何都没有太大改变。 但我想尝试这个,它有效:)