Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rake Tasks completely screwed up

#4471

I observed the following behaviour in rails3 beta3.

first of all, it is not possible to modify framework raketasks from lib/tasks/myraketasks.rake as in rails 2.3.5 cause rake tasks from gems are loaded after tasks in lib/tasks. i think this sucks.

therefore to modify tasks i had to do this in the Rakefile.
I found some terrifying behaviour:

Rails::Application.load_tasks

Rake::Task['db:schema:load'].enhance do
  puts Rails.env
  puts ActiveRecord::Base.connection.inspect
end

if i invoke spec:models i get this output:

development
<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x7f220048a818 @logger=#<ActiveSupport::BufferedLogger:0x7f2200804598 @guard=#<Mutex:0x7f22007ec3a8>, @log=#<File:/home/myusername/myproject/log/development.log>, @auto_flushing=1, @level=0, @buffer={}>, @config={:host=>"localhost", :database=>"myproject_test", :adapter=>"postgresql", :pool=>5, :username=>"postgres", :password=>"postgres"}, @runtime=76.7457485198975, @query_cache_enabled=false, @connection=#<PGconn:0x7f220048a700>, @connection_parameters=["localhost", 5432, nil, nil, "myproject_test", "postgres", "postgres"], @postgresql_version=80403, @async=nil, @active=nil, @query_cache={}>

WTF?!
Rails.env is development?! log is development too?

Reported by Kane · April 25th, 2010 @ 03:25 PM

State: wontfix
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to José Valim

    April 25th, 2010 @ 08:04 PM

  2. José Valim
    José Valim

    Ok, we have two issues here.

    The first one is about rake tasks in gems being loaded after, point taken. This should be fixed.

    The second one is about the output being development needs further investigation. Is rake spec:models setting the environment to test? When? Maybe your rake task is being executed before rspec has the chance to set the environment to test?

    So could you please investigate the second issue and give more information? I will provide a patch for the former.

    April 25th, 2010 @ 08:12 PM

  3. Matt Jones
    Matt Jones

    Some notes on the second point:

    • the enhance method adds functionality that runs after the specified task. See this for more details.

    • there's a certain amount of setup done in development mode before the test stuff can be loaded, for instance to handle loading the schema from dev into test.

    • the connection is in a somewhat mixed state; I'm guessing the myproject_test DB is in the test environment...

    April 26th, 2010 @ 01:22 AM

  4. José Valim
    José Valim

    Thanks Matt. A few more questions: how does the current behavior compares with Rails 2.3? Do you have any fix in mind?

    April 26th, 2010 @ 07:29 AM

  5. Repository
    Repository
    • State changed from open to resolved

    (from [e461e1bc0ec0a5c365840031309b83143e12955a]) Ensure application rake tasks and generators are loaded after the ones specified in railties/engines/rails. [#4471 Rake Tasks completely screwed up state:resolved] http://github.com/rails/rails/commit/e461e1bc0ec0a5c365840031309b83...

    April 26th, 2010 @ 08:53 AM

  6. Kane
    Kane

    So regarding the second issue:
    I assumed if i run rake db:test:prepare everything would be run in evironment test.
    I didnt looked how deep this goes and how many and which rake tasks are affected.

    So Matt, im not sure if i get you right, but i dont think Rails.env should be dev in any point in the process. Im aware that the process involves loading the dev database, but the env should be definitely test right?

    April 28th, 2010 @ 08:40 PM

  7. José Valim
    José Valim
    • State changed from resolved to open

    April 28th, 2010 @ 08:42 PM

  8. Nicolas Alejandro Santa
    Nicolas Alejandro Santa

    New Bug:

    running rake db:seed executes the db/seeds.rb script twice times:

        puts "Creating Basic Admin"
        u = User.create(:email => 'admin@yeti-media.com' , :password => 'admin123', :password_confirmation => 'admin123')
        u.admin = true
        u.confirmation_sent_at = u.confirmed_at = Time.now.to_s
        u.confirmation_token = nil
        u.save
        puts "Basic Admin Created"
    

    Got this output:

    rake db:seed

    Creating Basic Admin
    Basic Admin Created
    Creating Basic Admin
    Basic Admin Created

    April 29th, 2010 @ 11:21 PM

  9. José Valim
    José Valim

    Which rails version? This happens in rails master? It happens with this task only or all others?

    April 30th, 2010 @ 09:19 AM

  10. José Valim
    José Valim

    Nicolas, I just create a new application from Rails source and put the following in my db/seeds.rb

    puts "OMG"
    

    I invoked "rake db:seed" and it printed "OMG" just once, as expected.

    Kane, regarding the other bug, can you please create a failing test case here:

    http://github.com/rails/rails/blob/master/railties/test/application...

    Please let me know if you have any doubts.

    April 30th, 2010 @ 12:18 PM

  11. Dan Pickett
    Dan Pickett
    • Tag set to bugmash

    Can some bugmashers reproduce this behavior?

    May 15th, 2010 @ 01:50 AM

  12. Gavin Stark
    Gavin Stark

    Unable to reproduce in 3.0.0beta3

    May 15th, 2010 @ 05:48 PM

  13. Rizwan Reza
    Rizwan Reza
    • Tag cleared.
    • State changed from open to invalid

    May 16th, 2010 @ 02:57 AM

  14. Kane
    Kane

    Hey José
    i attached the requested failing test case

    May 26th, 2010 @ 10:06 PM

  15. Rizwan Reza
    Rizwan Reza
    • Tag set to rake, tasks
    • State changed from invalid to open

    June 15th, 2010 @ 03:12 PM

  16. José Valim
  17. José Valim
    José Valim

    Could someone please check what is the behavior in Rails 2.3? I'm almost sure that db:test:prepare needs to run in development as Matt pointed out. In other words, you should not be enhancing it to add custom behavior.

    June 20th, 2010 @ 11:09 AM

  18. José Valim
  19. José Valim
    José Valim
    • State changed from open to wontfix
    • Importance changed from to Low

    Yes, I confirmed this one. When you call rake test, it is actually in development, because you did not pass RAILS_ENV=test. Then it does all setup in development and finally invoke the test command, by shelling out with the proper env configured. There is not much we can do here.

    July 21st, 2010 @ 02:30 PM

  20. Jeremy Kemper
  21. Jeff Rafter
    Jeff Rafter

    I was able to reproduce Nicolas' duplication bug in Rails 3.0.3. Entering puts "OMG!" in db/seeds.rb and calling rake db:seed shows the message twice (meaning the rake task is called twice). I spent some time digging around and found that my rake tasks in my application were being loaded twice (duplicating the task). This was caused by the annotate-models gem and associated tasks. Removing those and the behavior was fine. Ultimately instead of

    ENV['position'] ||= 'after' require 'annotate_models/tasks'

    I now have

    ENV['position'] ||= 'after' desc "Add schema information (as comments) to model and fixture files" task :annotate_models => :environment do

    require 'annotate_models'
    options={}
    options[:position_in_class] = ENV['position_in_class'] || ENV['position']
    options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position']  
    AnnotateModels.do_annotations(options)
    

    end

    desc "Remove schema information from model and fixture files" task :remove_annotation => :environment do

    require 'annotate_models'
    AnnotateModels.remove_annotations
    

    end

    And everything works as expected.

    Cheers,
    Jeff

    November 23rd, 2010 @ 08:26 PM