Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"error while trying to load the gem" when database does not exist, or connection not yet established #740

Closed
josisusan opened this issue Mar 28, 2016 · 13 comments
Milestone

Comments

@josisusan
Copy link

Hi,
I have a fresh sinatra application on which paper_trail has been included. After I bundled it and ran

bundle exec rake db:create

It gave me error:

Bundler::GemRequireError: There was an error while trying to load the gem 'paper_trail'.
.........
ActiveRecord::NoDatabaseError: FATAL:  database "university_dev" does not exist
........
PG::ConnectionBad: FATAL:  database "university_dev" does not exist

If I use version below 4.0.0 (i.e. 3.0.9), it works fine. But after I upgrade it to 4 or above, it gives me same error as above.

@jaredbeck
Copy link
Member

PaperTrail requires your database to exist and to have a versions table.

@batter
Copy link
Collaborator

batter commented Mar 28, 2016

Can you provide a full stack trace?

@josisusan
Copy link
Author

Thanks for the response.
Full stack trace:

Bundler::GemRequireError: There was an error while trying to load the gem 'paper_trail'.
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:80:in `rescue in block (2 levels) in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `each'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `each'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler.rb:99:in `require'
/Users/josisusan/Projects/university/Rakefile:1:in `<top (required)>'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:93:in `load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:77:in `block in run'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/rake:23:in `load'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/rake:23:in `<main>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
ActiveRecord::NoDatabaseError: FATAL:  database "university_devel" does not exist
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_handling.rb:87:in `connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/model_schema.rb:230:in `table_exists?'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/config.rb:32:in `track_associations'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/version_concern.rb:14:in `block in <module:VersionConcern>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/concern.rb:120:in `class_eval'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/concern.rb:120:in `append_features'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:5:in `include'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:5:in `<class:Version>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:4:in `<module:PaperTrail>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:3:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record.rb:4:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail.rb:161:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in `block (2 levels) in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `each'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `each'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler.rb:99:in `require'
/Users/josisusan/Projects/university/Rakefile:1:in `<top (required)>'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:93:in `load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:77:in `block in run'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/rake:23:in `load'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/rake:23:in `<main>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
PG::ConnectionBad: FATAL:  database "university_devel" does not exist
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/connection_handling.rb:87:in `connection'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.5/lib/active_record/model_schema.rb:230:in `table_exists?'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/config.rb:32:in `track_associations'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/version_concern.rb:14:in `block in <module:VersionConcern>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/concern.rb:120:in `class_eval'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/concern.rb:120:in `append_features'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:5:in `include'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:5:in `<class:Version>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:4:in `<module:PaperTrail>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb:3:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail/frameworks/active_record.rb:4:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/paper_trail-4.1.0/lib/paper_trail.rb:161:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in `block (2 levels) in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `each'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:72:in `block in require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `each'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler/runtime.rb:61:in `require'
/Users/josisusan/.rvm/gems/ruby-2.2.2/gems/bundler-1.11.2/lib/bundler.rb:99:in `require'
/Users/josisusan/Projects/university/Rakefile:1:in `<top (required)>'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:93:in `load_rakefile'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:77:in `block in run'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/josisusan/.rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/rake:23:in `load'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/rake:23:in `<main>'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
/Users/josisusan/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'

I have on Rakefile:1

Bundler.require

@Bivek
Copy link

Bivek commented Mar 29, 2016

@jaredbeck, is this not an inconvenience. Imagine a situation where a new developer joins your team but unable to setup his development environment using the rake task.

@seanlinsley
Copy link
Member

The commit history:

It looks like the simplest possible solution to #465 would've been to only add dependent: :destroy to the association if the table exists.

The only other place this config is used, is in save_associations. I'm not confident that that check can be removed, since the original commit didn't add tests.

I think it would make sense that such a method would fail if the table is missing, so the check may need to be left there.

But then again, I'm not sure I'm sold on this decision from #465 to have Paper Trail not raise an error when you try modifying (deleting) data. It seems like if something isn't set up correctly Paper Trail should notify you.

The problem described here: #465 (comment) seems to be completely unrelated -- the fact that they didn't set up the version_associations table is the issue, not the fact that they're using a custom versions table for that model.

The other example on that ticket is a cap staging db:pull command, that presumably copies a remote database. That sounds like they're just trying to run commands in the wrong order. Or, their staging database hasn't been upgraded to have the version_associations table.

@seanlinsley
Copy link
Member

So unless there's something I'm not understanding, we should either remove all the checks and let the error come through (it would only happen when destroying or creating records), or we should wrap the error with a more useful message, pointing people to some documentation.

@seanlinsley
Copy link
Member

It sounds like as it is, it's impossible to start Rails / Sinatra with a missing database because the table_exists? command raises an error if the database is missing. So, it's impossible to run commands like rake db:create without also setting track_associations = false.

@seanlinsley
Copy link
Member

Testing with my Rails 4.2 app, it appears that when the database doesn't already exist rake db:create works, but rails console doesn't. So it sounds like rake db:create doesn't attempt to load the full environment.

@josisusan do you have something in your Rakefile that might be causing the entire environment to be loaded?

In my opinion, rails console should:

  • work if the database doesn't exist (it does when not using paper_trail)
  • work if a table doesn't exist
    • until you try using it for something (User.count, or versions.last, or save_associations)

@batter
Copy link
Collaborator

batter commented Mar 29, 2016

To be fair, this has nothing to do with Sinatra or Rack. This is due to the fact that you are requiring the gem before setting up your database.

Try changing your Rakefile accordingly:

require 'bundler/setup'
# now add a require statement for the file that initializes your database connection

If you're using the Sinatra ActiveRecord extension, that database initializer could look like this]:

require "sinatra/activerecord"
set :database, {adapter: "sqlite3", database: "foo.sqlite3"}

Then at the top of your Sinatra file you can should be able to add your Bundler.require statement without issue.

We might be able to at some point add some code to lazily evaluate whether the database has the setup expected in cases where ActiveRecord is not connected initially, that would be ideal probably. That being said I don't think I would call this a bug either.

@josisusan
Copy link
Author

Hi, I have made some changes and create a pull request (#742). @batter Could you look over the pull if its necessary or not?

@josisusan
Copy link
Author

@seanlinsley its working fine for rails application. I have created repo based on sinatra which will generate same error on paper_trail(>= 4) if we run:

$ bundle exec rake -T

@jaredbeck
Copy link
Member

Thanks Susan. I tried your repo (at josisusan/cafe_app@30d7fa7) and I can confirm that it fails to load PT:

bundle exec rake -T
rake aborted!
Bundler::GemRequireError: There was an error while trying to load the gem 'paper_trail'.
/Users/jared/Desktop/cafe_app/Rakefile:3:in `<top (required)>'
PG::ConnectionBad: FATAL:  database "cafe_app_dev" does not exist
/Users/jared/Desktop/cafe_app/Rakefile:3:in `<top (required)>'

Given this new information, I'll label this as a confirmed bug. Thanks, Susan!

You know, if we just required everyone to have the version_associations table, we wouldn't need the table_exists? check. We could still allow people to disable the tracking of associations.

@jaredbeck jaredbeck changed the title Usage on Sinatra/Rack based application "error while trying to load the gem" when database does not exist, or connection not yet established Apr 15, 2016
@jaredbeck jaredbeck added this to the 5.0.0 milestone Apr 19, 2016
jaredbeck added a commit that referenced this issue Apr 27, 2016
@jaredbeck
Copy link
Member

Fixed by #785

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants