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

On Rails 4, causes rake assets:precompile incorrectly to attempt to initialize DB connection #396

Closed
patrick-minton opened this issue Sep 1, 2013 · 13 comments · Fixed by #403

Comments

@patrick-minton
Copy link

When using acts_as_taggable_on, rake assets:precompile will fail if there is no production database, because it tries to initialize the app, which it should not do (in rails 4 there is no longer a config.assets.initialize_on_precompile option):

|ruby-2.0.0-p247@boxscoregeeks| patricks-retina in ~/code/boxscoregeeks
± |master |  RAILS_ENV=production rake assets:precompile
rake aborted!
FATAL:  database "doesntexist" does not exist
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `initialize'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `new'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `initialize'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:203:in `table_exists?'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb:5:in `included'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:98:in `include'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:98:in `taggable_on'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:39:in `acts_as_taggable_on'
/Users/patrick/.rvm/gems/ruby-2.0.0-p247@boxscoregeeks/gems/acts-as-taggable-on-2.4.1/lib/acts_as_taggable_on/taggable.rb:15:in `acts_as_taggable'

This is a big problem because currently pre-compiliing assets is the only reliable way to get them to work on Heroku.

@trappist
Copy link

trappist commented Sep 4, 2013

There's a workaround, working for me until recently:

acts_as_taggable rescue nil

But that's no help if you're using something like activeadmin, which does the invocation without having the ability to control it like that.

@mdb
Copy link

mdb commented Sep 9, 2013

I can confirm this issue. Any word on a fix or workaround would be much appreciated.

Thanks!

@trappist
Copy link

trappist commented Sep 9, 2013

heroku labs:enable user-env-compile -a myapp is practically a fix on Heroku.

https://devcenter.heroku.com/articles/labs-user-env-compile

@mdb
Copy link

mdb commented Sep 9, 2013

@trappist Thanks for the fast response. I did see Heroku's documentation surrounding user-env-compile -- and enabling it does address the issue -- though I'm a bit wary of relying on this workaround as Heroku cites it as counter to best practices.

@trappist
Copy link

trappist commented Sep 9, 2013

I understand why they say that, but all it really does is force your Heroku instance to behave more like your local development instance, which does load your environment for tasks like assets:precompile. Heroku doesn't, probably because it saves time and (you're right) shouldn't be necessary. So, it's not a fix, but it's a very good workaround.

@patrick-minton
Copy link
Author

Actually, no. I encountered that early on in debugging this. It did not work for me -- I suspect it wouldn't for @mdb either, unless he also had the rescue nil fix from @trappist's earlier post. This doesn't solve anything, because running this in your development environment will ALSO fail unless you have a production database setup in your database.yml:

RAILS_ENV=production bundle exec rake assets:precompile

(again, unless you have the rescue nil fix).

The "RAILS_ENV=production" is relevant and has a great deal of impact. Of course, whatever you have setup in database.yml for production is very unlikely to work from the Heroku servers.

Once again:

In Rails 4, the Rake task assets:precompile IS NOT SUPPOSED TO LOAD YOUR ENVIRONMENT, EVEN LOCALLY. The fact that it does so when using acts_as_taggable_on is definitely a BUG within acts_as_taggable_on. Something within the gem libraries is trying to initialize the application stack, when it shouldn't.

The reason this is a compatibility bug is because the config.assets.initialize_on_precompile option was removed in Rails 4 -- meaning that if a gem causes this, there is no way to turn it off.

@nishantmodak
Copy link

+1

@jonysy
Copy link

jonysy commented Nov 14, 2013

Is there a temporary fix for this??

@shekibobo
Copy link
Contributor

Point your gem to the repo/branch for #403 and it should deploy just fine.

@jonysy
Copy link

jonysy commented Nov 26, 2013

Didn't seem to work... Using: https://github.com/shekibobo/acts-as-taggable-on

@shekibobo
Copy link
Contributor

gem 'acts-as-taggable-on', github: 'shekibobo/acts-as-taggable-on', branch: 'fix-heroku-precompile'?

@jonysy
Copy link

jonysy commented Nov 26, 2013

Worked, thank you!

@shekibobo
Copy link
Contributor

No problem.

@bf4 bf4 closed this as completed in #403 Dec 10, 2013
bf4 added a commit that referenced this issue Dec 10, 2013
Check to see if we can connect to the db before accessing it. Fixes #396.
tekniklr pushed a commit to tekniklr/acts-as-taggable-on that referenced this issue Mar 19, 2021
Check to see if we can connect to the db before accessing it. Fixes mbleigh#396.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants