Reputation: 32355
I'm using jruby-1.5.3 with a rails 2.3.5 app. I've just started playing around with thread safety using:
config.threadsafe!
config.eager_load_paths << "#{RAILS_ROOT}/lib"
Which works fine, I just noticed however on deployment to my staging environment (which has the same config as production) that I get undefined constants. For instance, a migration that adds another role to a Role table:
class AddSuperAdminRole < ActiveRecord::Migration
def self.up
Role.create :rolename => 'super_admin'
end
end
throws a:
uninitialized constant AddSuperAdminRole::Role
It works fine in dev environment because i'm not running that multithreaded so I know that's the issue. I've tried eager loading the app/models path also but that didn't work. How do I get migrations running with threadsafety?
Upvotes: 0
Views: 465
Reputation: 21497
From ticket #2506 on the Rails Lighthouse site. Below i linked the threadsafe method in Rails. You'll see that config.dependency_loading
is set to false because it's not thread safe and therefore the migrations are getting their dependencies auto-loaded.
# Enable threaded mode. Allows concurrent requests to controller actions and
# multiple database connections. Also disables automatic dependency loading
# after boot, and disables reloading code on every request, as these are
# fundamentally incompatible with thread safety.
def threadsafe!
self.preload_frameworks = true
self.cache_classes = true
self.dependency_loading = false
self.action_controller.allow_concurrency = true
self
end
Here's Joshua Peek's response to the problem in comments to the ticket:
I would suggest requiring the models the you need in the migration, or better including the model definition again so the migration doesn't depend on its existence.
Upvotes: 0