-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Unexpected rake task behavior change in Rails 5.2 #32870
Comments
Your point is right. I think it is good to add documents. Can you create a PR? |
Sure! I will |
@y-yagi I can't find a proper place for adding it, any suggestion? |
How about adding doc to Configuring Guide? |
@y-yagi I was thinking there as well. but there's already documents for this
So the problem is actually that the old documents don't provide correct infos (not all rake tasks will run the block) |
Hey! I just ran into this and found this issue. What is the recommended way to solve this? In our codebase there's something like What is the correct construct, I'd love to have that in the docs as well. :) |
Oh yeah, I also run into #32994, maybe it's related or just coincidence?
|
Thanks for your report. # config/application.rb
class Application < Rails::Application
config.after_initialize do
next if ARGV.include?("db:create")
p User.table_exists?
end
end I would like to think about a more appropriate approach for this. |
I think the question here should be whether the use of models is not allowed in |
I took another look, it's a very old initializer. I think it's correct to assume that this is not the right way to do it. I think it was placed there to ensure some data exists. I'll have a look at our code, I think a DB seed or even a PORO model would be a better place for it. |
We also encountered this change of behavior and I wanted to share our situation. Running The way we solved this for now was to avoid our factories from being called in |
Hey team, we've also hit this problem as we have a few cases where we reference constants in our initialisers and our factories (which are being loaded as a part of |
Should quickly add we've managed to solve our issue by guarding the areas where classes were being loaded with |
Then loading it where we needed it explicitly, which, for us, was in our spec helper:
|
Yes we have – we now lazily load/create this record on demand. It's the (There's also now a |
This fixes rake db:create task for rails 5.2 Related Information rails/rails#32870 (comment)
rails/rails#32870 Rescue on `table_exists?` call Without this rescue in place you can’t run `rails db:create`. It’ll fail spectacularly with the following error: rails db:create rake aborted! ActiveRecord::NoDatabaseError: FATAL: database "headway_rails_template_development" does not exist /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:688:in `rescue in connect' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:683:in `connect' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:215:in `initialize' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:40:in `new' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:40:in `postgresql_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:809:in `new_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `checkout_new_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:832:in `try_to_checkout_new_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:793:in `acquire_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:521:in `checkout' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:380:in `connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1008:in `retrieve_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/connection_handling.rb:90:in `connection' /Users/jon/.rvm/gems/ruby-2.5.1@headway_rails_template/gems/activerecord-5.2.1/lib/active_record/model_schema.rb:324:in `table_exists?' /Users/jon/Sites/Rails/github/canard/lib/canard/adapters/active_record.rb:31:in `active_record_table?' /Users/jon/Sites/Rails/github/canard/lib/canard/adapters/active_record.rb:36:in `has_roles_mask_accessors?' /Users/jon/Sites/Rails/github/canard/lib/canard/user_model.rb:74:in `acts_as_user' /Users/jon/Sites/Rails/headway/headway_rails_template/app/models/user.rb:31:in `<class:User>' /Users/jon/Sites/Rails/headway/headway_rails_template/app/models/user.rb:1:in `<main>'
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
There was a behaviour change on rake tasks that avoided the creation of the test database. All initializers are now called from rake tasks, so those initializers with code accessing the database will fail when the database isn't created. See: rails/rails#32870 Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>
The 'preload_all_models' initializer is now run even when db:create is called, so it avoids the database to be created on arvbox, for example, because it attempts to access the database before being created. This initializer was added 7 years ago and it now seems to not be needed anymore, so I think it's the cleaner way to resolve this issue. See: rails/rails#32870 Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>
In case anyone lands here w/ the same issue, I adapted the NullDB fallback fix referenced by @ulferts in opf/openproject#7114 when updating an app to Rails v5.2. Worked well! |
I have some initializers that are needed for my app, but not for my rake db tasks, has anyone find a solution other than avoiding values form |
Steps to reproduce
config.after_initialize { raise "foo" }
rake db:create
Expected behavior
Before 5.2, the
after_initialize
block will not be executed so the database can be created successfullyActual behavior
The app crashed because the
after_initialize
blockSystem configuration
Rails version: 5.2
Ruby version: 2.4.1
Notes
I think this is an issue because before 5.2 we might add some database related logics in
after_initialize
block. And since running tasks likedb:create
won't initialize the whole app this can work. Like:But after upgrade to 5.2 the code won't work because the logic will hit the database first before actually execute the rake task (
db:create
). And we can't find this behavior change in changelog or documents.Also, I found this issue is cause by this commit. Before this commit some of the rake tasks will only run
load_config
as prerequisite. But nowload_config
has theenvironment
prerequisite so the app will be fully initialized.So I think either we change this behavior back, or we should document about it.
The text was updated successfully, but these errors were encountered: