Sinatra App on Heroku ActiveRecord::StatementInvalid: PGError: ERROR: - postgresql

SCENARIO:
I currently have anapplication on Heroku that is Sinatra Application + ActiveRecord. What currently works:
• I am able to view static pages of my site
• I am able to run migrations
• I am able to push my local development db to my Heroku DB (heroku db:push postgres://localhost/dev)
• I am able to pull from the Heroku DB down to my localhost DB (heroku db:pull postgres://localhost/dev)
THE ERROR:
When I navigate to a page that makes a simple query like:
#pics = Picture.find(:all)
I get this error in my log file
DEBUG -- : NoMethodError: undefined method `values' for #<PGresult:0x00000002844098>: SHOW client_min_messages
DEBUG -- : PGError: ERROR: invalid value for parameter "client_min_messages": ""
: SET client_min_messages TO ''
ActiveRecord::StatementInvalid - PGError: ERROR: invalid value for parameter "client_min_messages": ""
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
At this point I'm not sure what is wrong so I decided to log onto the heroku console and run this command to see if my tables are in the DB
> heroku console
> ActiveRecord::Base.connection.tables
AND I get a similar errors
ActiveRecord::StatementInvalid: PGError: ERROR: invalid value for parameter "client_min_messages": ""
: SET client_min_messages TO ''
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in execute'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
Then I decided to try this in the console...
> ActiveRecord::Base.establish_connection
> ActiveRecord::AdapterNotSpecified: ActiveRecord::AdapterNotSpecified
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:54:in `establish_connection'
/home/heroku_rack/lib/console.rb:158:in `block (2 levels) in <top (required)>'
/home/heroku_rack/lib/console.rb:148:in `eval'
/home/heroku_rack/lib/console.rb:148:in `_eval'
At this point it would seem that I have no tables and adapter connection but how can that be when I'm able to run migrations and push and pull from the Heroku DB?
UPDATE 1 - NOT MUCH FURTHER ALONG BUT MORE INSIGHT
So I decided to start from scratch and do this over again. I seem to have made some progress but I'm stuck. At re-ran my scenario I mentioned above and succeed. So my database, migration and data are all in the DB. But my app still cannot connect to the DB. BUT what's weird is my console interaction.
> heroku console
> ActiveRecord::Base.connection_config
> {:adapter=>"postgresql", :database=>"1e2e12e21e", :username=>"1e2e12e21e", :password=>"i2j3i23joo12", :host=>"1e2-12e-2e1212-e2e2e1.compute-1.amazonaws.com", :port=>5432, :encoding=>"unicode"}
NOTE: I replaced the real details with this fake stuff just to illustrate
> ActiveRecord::Base.connection_handler
> #<ActiveRecord::ConnectionAdapters::ConnectionHandler:0x00000002f8a578 #connection_pools={"….
NOTE: This a really long string but it has content
> ActiveRecord::Base.connected?
> false
At this point I really don't get it? I don't get how I can keep running db migrations, keeping push and pulling data, log into Heroku Console see my connection_config details, BUT when I run my app internal server error. AND when I run ActiveRecord::Base.connection.tables in heroku console I get ActiveRecord::StatementInvalid: PGError: ERROR: invalid value for parameter "client_min_messages": ""
Any thoughts on what I could be doing wrong?
FINAL UPDATE
Here is what the problem is:
Heroku support kindly pointed to me to a known issue with activerecord 3.1.3. It's a bug that should be fixed in future updates: https://github.com/rails/rails/commit/92a3c487bb0d125c437e53a7f45c31fcca97f2d9
How to solve the problem:
I decided to use a much lower version of activerecord in my gemfile gem "activerecord", "~> 3.0.9 and my site is up and running with no problems so far.
Why I never tried this out before I don't know. I hope this post anyone else.

I had this problem for a long time and it was driving me nuts. I couldn't find a solution anywhere (and many rails updates passed by not solving the "problem"), so i decided to just hunt down the issue myself. It turned out to be an easy fix. Just add an additional key, value pair: :min_messages => "warn" (or whatever message level you want) to the ActiveRecord setup in your configure block. here's what I'm using in my Sinatra app's right now, using Sinatra 1.3.2 and ActiveRecord 3.2.2:
db = URI.parse(ENV['DATABASE_URL'] || "postgres://user:password#localhost/database")
adapter = (db.scheme == "postres") ? "postgresql" : db.scheme
ActiveRecord::Base.configurations[:production] = {
:adapter => adapter,
:username => db.user,
:password => db.password,
:port => db.port,
:database => db.path.sub(%r(^/),""),
:host => db.host,
:min_messages => "warn"
}
unless ActiveRecord::Base.connected?
ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[:production]
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
hope this helps you and any other people having these issues!

None of the other suggestions worked for me. I ultimately fixed this by switching to the Heroku cedar stack:
heroku create your app --stack cedar

I had this problem on both bamboo-ree-1.8.7 and bamboo-mri-1.9.2 stacks. The application worked fine on localhost, and migrated OK on heroku, but reading the database inside the heroku web stack failed with a client_min_messages complaint.
Setting the :min_messages (as suggested in another answer) to either "warn" or "warning" produced different errors.
Forcing my ActiveRecord down to 3.0.11 (from 3.2.3) resolved the issue. (Not that I see anything in AR to explain this. I speculate it's related to differing postgres versions ... I'm using 9.0.3, suspect the heroku bamboo shared is 8.4)

Pushing this up in my gem file worked for me.
group :production do
gem 'pg'
gem 'activerecord', '~> 3.0.9'
end

Related

Undefined method [] for nil when trying to run cap deploy:restart

I have a Rails 5.2 app, and with cap 3.4.1 we are suddenly getting this weird error:
[b35efe76] Phusion Passenger(R) 6.0.8
DEBUG [b35efe76] Finished in 0.305 seconds with exit status 0 (successful).
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#host.com: undefined method `[]' for nil:NilClass
Trying to narrow it down, it happens when attempting to restart, because this is the line that fails:
cap production deploy:restart
The question is, how do I go about finding what file is trying to call [] on a nil value on? Running cap with --trace is of no value because it just gives me internal errors - nothing in my code. Basically, how do I find out what is nil?
One more clue, currently, I have a bunch of servers, if I run the cap restart command on server A, it restarts fine, on server B, it throws this error, so I'm guessing there's an environment variable on server A but not B, but the error is just so opaque I don't know where to begin.
Thanks for any help,
kevin
A wild guess: I had a similar problem and I could solve it by upgrading capistrano-passenger to >= 0.2.1
Looks like the version change of passenger from 6.0.7 to 6.0.8 has introduces a problem. I see you are also on 6.0.8, so it might affect you, too!
Link to the capistrano-passenger issue

Could not find rake-12.3.0 in any of the sources (Bundler::GemNotFound) - Bitnami Redmine Stack 3.3.1-0 on Ubuntu 16.04.2 LTS

I am using the Bitnami Redmine Stack 3.3.1-0 on Ubuntu 16.04.2 LTS.
I recently installed a plugin (Issue Templates - http://www.redmine.org/plugins/redmine_issue_templates).
In the process, I updated the gems using bundle install and bundle update.
After performing this update, the Redmine website no longer works. I get the error:
"We're sorry, but something went wrong. We've been notified about this issue and we'll take a look at it shortly."
Upon analyzing the apache error log, I find the error:
"Could not find rake-12.3.0 in any of the sources (Bundler::GemNotFound)"
However, I have checked /opt/bitnami/apps/redmine/htdocs/vendor/ and /opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems and rake-12.3.0 is included in both locations. I am not sure why this issue is occuring.
I uninstalled the plugin and removed the files, but I still have the issue. I tried bundle install, bundle update, gem install rake -v '12.3.0', bundle exec rake rails:update:bin in the /opt/bitnami/apps/redmine/htdocs/ folder, and I also tried ./gem update in the /opt/bitnami/ruby/bin folder, but I still have the issue.
Please advise me what to do.
[ 2018-03-15 15:35:23.8349 28957/7f4ce96ec700 age/Hel/Req/CheckoutSession.cpp:252 ]: [Client 4-1] Cannot checkout session because a spawning error occurred. The identifier of the error is 35671c4a. Please see earlier logs for details about the error.
App 4445 stdout:
App 4445 stderr: /opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/lib/phusion_passenger/config/system_metrics_command.rb:34: warning: Insecure world writable dir /opt/bitnami/ruby/bin in PATH, mode 040757
App 4445 stdout:
[ 2018-03-15 15:35:29.9175 28957/7f4ce86a9700 App/Implementation.cpp:287 ]: Could not spawn process for application /opt/bitnami/apps/redmine/htdocs/: An error occured while starting up the preloader.
Error ID: 16a39427
Error details saved to: /tmp/passenger-error.3ZEVOb
Message from application: <p>It looks like Bundler could not find a gem. Maybe you didn't install all the gems that this application needs. To install your gems, please run:</p>
<pre class="commands">bundle install</pre>
<p>If that didn't work, then the problem is probably caused by your application being run under a different environment than it's supposed to. Please check the following:</p>
<ol>
<li>Is this app supposed to be run as the <code>daemon</code> user?</li>
<li>Is this app being run on the correct Ruby interpreter? Below you will
see which Ruby interpreter Phusion Passenger attempted to use.</li>
</ol>
<p>-------- The exception is as follows: -------</p>
Could not find rake-12.3.0 in any of the sources (Bundler::GemNotFound)
<pre> /opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/spec_set.rb:88:in `block in materialize'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/spec_set.rb:82:in `map!'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/spec_set.rb:82:in `materialize'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/definition.rb:170:in `specs'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/definition.rb:237:in `specs_for'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/definition.rb:226:in `requested_specs'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:108:in `block in definition_method'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:20:in `setup'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler.rb:107:in `setup'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/bundler-1.16.1/lib/bundler/setup.rb:20:in `<top (required)>'
/opt/bitnami/ruby/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `require'
/opt/bitnami/ruby/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
/opt/bitnami/ruby/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/lib/phusion_passenger/loader_shared_helpers.rb:278:in `block in run_load_path_setup_code'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/lib/phusion_passenger/loader_shared_helpers.rb:381:in `running_bundler'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/lib/phusion_passenger/loader_shared_helpers.rb:276:in `run_load_path_setup_code'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/helper-scripts/rack-preloader.rb:99:in `preload_app'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/helper-scripts/rack-preloader.rb:157:in `<module:App>'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
/opt/bitnami/ruby/lib/ruby/gems/2.1.0/gems/passenger-5.0.6/helper-scripts/rack-preloader.rb:28:in `<main>'</pre>
[ 2018-03-15 15:35:29.9342 28957/7f4ceaeef700 age/Hel/Req/CheckoutSession.cpp:252 ]: [Client 1-2] Cannot checkout session because a spawning error occurred. The identifier of the error is 16a39427. Please see earlier logs for details about the error.
Did you load the Bitnami environment before running the "bundle" commands? There's a script at /opt/bitnami/ called "use_redmine". Please run it before running the commands.
For instance:
/opt/bitnami/use_redmine
cd /opt/bitnami/apps/redmine/htdocs/
bundle ...
Note: if you installed the Redmine Stack as superuser "root" on your Ubuntu, you need to load the environment using sudo /opt/bitnami/use_redmine

Why I get 500 Error while on XAMPP while loading CodeIgniter 3 database library?

I have gone through number of threads on stackoverflow and on other forums to find the answer but could not find relevant answer. People have applied different approaches but none has worked for me thus far.
I am using CI 3 on XAMPP and whenever I load database library in the model, I run into 500 Error.
Here is my CI database config info:
'hostname' => 'localhost',
'username' => 'my_user',
'password' => 'my_pass',
'database' => 'prod_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
PHP ini db libraries are as below:
extension=php_bz2.dll
extension=php_curl.dll
extension=php_fileinfo.dll
;extension=php_ftp.dll
extension=php_gd2.dll
extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
extension=php_mbstring.dll
extension=php_exif.dll ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=php_snmp.dll
My model code is as below:
class loginmodel extends CI_Model {
public function validate_login($username, $password) {
$this->load->database();
$var = 0;
return $var;
}
}
Whenever I remove $this->load->database(); from the model, my code runs and i am able to redirect to a "Login Failed" page. BUT, whenever I load my database library in autoload.php or load it in above model, code fails.
Autoload.php
$autoload['libraries'] = array('database');
Apache error logs showed below errors but I could not figure as why this was happening.
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysqli.dll' - The specified module could not be found.\r\n in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_pdo_mysql.dll' - The specified module could not be found.\r\n in Unknown on line 0
Please advise.
EDIT:
I verified all config to be OK and as stated in the comments, I added environment variables to windows and verified through PHPInfo that mysqli and mysqlnd were loaded.
Despite getting all the config right, error still persisted and I did a clean install of CI to reproduce library load failure. This time CI returned an error that db access was denied.
I then realized that after migration, database user was not created in the new environment. So once I created the desired user and assigned it required privileges, I was able to run the code just fine.
Therefore, if anyone encounters such issue in future, below steps might help:
First and foremost, verify if your desired user can access the
database.
Modify your .htaccess to use your desired URL schemes only after
verifying that database connection is working
Then verify that absolute path is available in PHP.ini on XAMPP.
Plus, verify that PHP is added in the environment variables. My
Values on Windows 10 are as follows: Variable Name: PHP , Variable
Value: C:\xampp\php\ext
In CI, mysqli is selected as database driver
In PHP.ini mysqli extension is enabled. i.e. php_mysqli.dll
Echo PHPInfo to verify that mysqli and mysqlnd are available in your
installation.
If nothing works, do a clean install of CI on a separate directory and load database library after setting up your database configuration and before setting up .htaccess until you can reproduce the error.
Hope that error will be eliminated!

MongoMapper, Padrino and Passenger - Connection Failure?

I'm currently working on a Padrino project which has been working absolutely fine in developement, but after pushing it to my live environment, I'm experiencing problems. Checked the logs and the error I'm getting is:
[31m ERROR[0m -[33m24/Jul/2012 11:32:53[0mMongo::ConnectionFailure - Operation failed with the following exception: #<Mongo::ConnectionFailure:0xa762528>:
My database.rb file is the standard one generated by Padrino, namely:
MongoMapper.connection = Mongo::Connection.new('localhost', nil, :logger => logger)
case Padrino.env
when :development then MongoMapper.database = 'licensing_development'
when :production then MongoMapper.database = 'licensing_production'
when :test then MongoMapper.database = 'licensing_test'
end
Everything works perfectly in the console, so I'm assuming that the problem is to do with Passenger. Any ideas where I might be going wrong?
OK, so ignore me. I forgot to set RACK_ENV when running the rake task that imported my data, as well as when starting the console, so there was no data in my production database.

Undefined method `FactoryGirl' -- upgrading from 2.0.2 to 3.4.2

I'm in the process of upgrading from factory_girl (2.0.2 to 3.4.2) and factory_girl_rails (1.1.0 -> 3.4.0)
and I'm having issues with my rspec tests seeing factory girl.
I think I've successfully altered my factories to deal with the new syntax, and have removed the extra require statements that were bringing in multiple copies of the same files. My server now starts up, so I know that the factories.rb file is correctly getting parsed.
Now when I run my rspec tests, I'm getting this error:
NoMethodError: undefined method `FactoryGirl' for #
it 'can be created' do
course = FactoryGirl(:course)
….
end
With Factory Girl 3.4.2, you will need to explicitly use the create method.
course = FactoryGirl.create(:course)