Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 370
laimison Avatar asked Jun 17 '17 17:06

laimison


2 Answers

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.

like image 148
jvillian Avatar answered Nov 19 '22 20:11

jvillian


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
like image 3
Eliot Sykes Avatar answered Nov 19 '22 19:11

Eliot Sykes