Models can't be used in migrations if config.threadsafe! is set #2662

Closed
schuetzm opened this Issue Aug 23, 2011 · 7 comments

6 participants

@schuetzm

It seems, autoloading is disabled then.

However, Bruce Adams suggested a workaround here:
http://www.builtfromsource.com/2011/02/09/getting-rake-dbseed-and-config-threadsafe-to-play-nice/#comment-4293
which is to put "config.threadsafe! unless $rails_rake_task" into your environment file.

I suggest to insert this into the default files that are created with a new application.

@isaacsanders

@schuetzm Is this still an issue?

@schuetzm

Yes, seems so. Steps to reproduce:
1. create new rails app (3.2.3)
2. insert "config.threadsafe!" in config/environments/development.rb
3. create migration file db/migrate/20120428210253_add_xxx.rb:

Xxx
Blabla

class AddXxx < ActiveRecord::Migration
  def up
  end

  def down
  end
end
  1. create app/model/xxx.rb and app/model/blabla.rb with dummy classes Xxx and Blabla
  2. run rake db:migrate => error "uninitialized constant Blabla"
  3. if you use "config.threadsafe! unless $rails_rake_task" in the environment file instead, the classes will be auto-loaded

(note also that it has already loaded the class Xxx, probably because that's what the migration is named)

@acrispino

Another way to work around this issue is to explicitly require the necessary models

@steveklabnik
Ruby on Rails member

Given @tenderlove's recent interest in making config.threadsafe! the default, this ticket is probably of interest.

It seems, autoloading is disabled then.

That is true. Autoloading isn't threadsafe.

@freerobby

This should at minimum throw a loud warning since it's going to break a lot of people's existing migrations.

@josevalim
Ruby on Rails member

Fixed on master! \o/

@josevalim josevalim closed this Sep 7, 2012
@freerobby

@josevalim Rocking, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment