Installing newest version of Rails 4 with Postgres - The PGconn, PGresult, and PGError constants are deprecated
Asked Answered
M

3

18

I'm not able to find this warning on Google so asking Stackowerflower's help.

I want to install Rails 4.2.8 on fresh Centos 7 box. Postgres version is 9.2.18. Ruby version is 2.3.4.

When Rails is installed I configure config/database.yml file as usual and pretty sure that database.yml file is ok to connect to DB successfully. Postgres is already running for other apps successfully and fresh role is created for this app.

In the next step there is an actual issue:

[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]$

Is this confirms that Rails successfully connected to Postgres? How to simply check it?

If yes - how long will I be able to use similar Postgres versions with Rails 4.2.8?

Interesting thing that I didn't get similar messages with very similar setup so I wanted to be sure that I will be able to use this setup well.

Many Thanks

Munsey answered 17/6, 2017 at 17:15 Comment(0)
F
34

I noticed the same deprecation warnings when upgrading from pg 0.20.0 to pg 0.21.0. I didn't seem to have any actual problems with pg and my apps (dev, staging, and production) all seemed to work fine.

I found the warning annoying, however, so I locked all my Gemfiles at pg 0.20.0.

Frazzle answered 17/6, 2017 at 17:20 Comment(3)
Added gem 'pg', '~> 0.20.0' to Gemfile and eliminated this warning as well. Originally, Rails tried to use 0.21.0Munsey
pity that this will not be backported to rails 4.xHagfish
I have pg 0.21. How can I just silence this warning?Centriole
A
3

To avoid using an older version than 0.21.0 of the pg gem, put this file in lib/pg/deprecated_constants.rb and ensure your app's $LOAD_PATH is configured to load files in your app's lib/ dir before your pg gem's installation path. See further notes in comments below:

# 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 <<MSG
-----------------------------------------------------------------------------------
The pg and/or activerecord gem version has changed, meaning deprecated pg constants
may no longer be in use, so try deleting this file to see if the
'The PGconn, PGresult, and PGError constants are deprecated...' message has gone:
#{__FILE__}
-----------------------------------------------------------------------------------

MSG
end

# Declare the deprecated constants as is done in the original 
# pg/deprecated_constants.rb so they can still be used by older
# versions of gems such as activerecord.
PGconn   = PG::Connection
PGresult = PG::Result
PGError  = PG::Error
Amoy answered 8/7, 2018 at 14:5 Comment(1)
great answer, great documentation, works great for my situation. one thing I did notice is this file could be setup as an initializer without having to modify config/application.rb if one so chooses.Aftertime
H
0

Or if you wanna use the newer pg version, you could monkey patch the annoying deprications. NOTE: this will overwrite the file in your gem bundle:

Create a file in your application root (i.e. bin/monkey_patch.rb) It should look like this:

#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

Then in your application.rb file after the first require line add a 2nd line like so which will monkey patch the depreciation before the app boots.

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

It's total hack but it works and you get to use the later version of ruby-pg gem. But from what I can tell with a git diff on v0.21.0 vs v0.20.0 there's not much that changed anyhow. But I wanted to try this and it worked :)

Homochromous answered 15/3, 2018 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.