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.

[BUG] rake db:create and observers

#63

If you have an observer, then doing rake db:create in some circumstances (one is described below) causes rail to die with 'unknown database'.

Obviously a circular dependency problem.

I've been poking around the test suite, and can't seem to find the right place for testing this, can anyone offer any pointers?

The problem can be reproduced with (fresh rails app, with dbs wiped)

config/environment.rb:
  config.active_record.observers = :user_observer

app/models/user.rb
  class User < ActiveRecord::Base
    has_many :posts, :conditions => ['posts.published = ?', true], :order => 'updated_at DESC', :dependent => :destroy
  end

app/models/user_observer.rb
  class UserObserver < ActiveRecord::Observer
  end

Reported by Ian White · April 29th, 2008 @ 07:29 PM

State: committed
Milestone: none
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski

    Can you post a stack trace by using rake --trace.

    May 4th, 2008 @ 01:47 AM

  2. Michael Koziarski
    Michael Koziarski
    • Assigned user set to Michael Koziarski

    May 4th, 2008 @ 01:47 AM

  3. Ian White
    Ian White

    Stack trace below

    I've attached a git diff of what I had to add to a fresh rails app (6a6b439 Sun Apr 20 12:57:36 2008 +0800) to get the problem to occur.

    I'd be happy to investigate, but couldn't find the obvious place to start adding tests.

    
    ~/dev/sandbox/ticket63(master)$ rake db:create --trace
    (in /Users/ian/dev/sandbox/ticket63)
    ** Invoke db:create (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    rake aborted!
    Unknown database 'ticket63_development'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:505:in `real_connect'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:505:in `connect'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:183:in `initialize'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:88:in `new'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:88:in `mysql_connection'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:292:in `send'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:292:in `connection='
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:260:in `retrieve_connection'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/base.rb:2067:in `quote_bound_value'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/base.rb:2035:in `replace_bind_variables'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/base.rb:2035:in `gsub'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/base.rb:2035:in `replace_bind_variables'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/base.rb:2024:in `sanitize_sql_array'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/base.rb:1921:in `sanitize_sql'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/associations.rb:1249:in `configure_dependency_for_has_many'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/associations.rb:700:in `has_many'
    /Users/ian/dev/sandbox/ticket63/app/models/user.rb:2
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_without_new_constant_marking'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_file'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:353:in `new_constants_in'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:213:in `load_file'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:94:in `require_or_load'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:259:in `load_missing_constant'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:466:in `const_missing'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:478:in `const_missing'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/inflector.rb:271:in `constantize'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb:143:in `constantize'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/observer.rb:157:in `observed_class'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/observer.rb:183:in `observed_classes'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/observer.rb:166:in `initialize'
    /opt/local/lib/ruby/1.8/singleton.rb:95:in `new'
    /opt/local/lib/ruby/1.8/singleton.rb:95:in `instance'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/observer.rb:38:in `instantiate_observers'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/observer.rb:36:in `each'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activerecord/lib/active_record/observer.rb:36:in `instantiate_observers'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:26:in `define_dispatcher_callbacks'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:177:in `call'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:177:in `evaluate_method'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:161:in `call'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:90:in `run'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:90:in `each'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:90:in `send'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:90:in `run'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/callbacks.rb:272:in `run_callbacks'
    /Users/ian/dev/sandbox/ticket63/config/../vendor/rails/railties/lib/initializer.rb:460:in `send'
    /Users/ian/dev/sandbox/ticket63/config/../vendor/rails/railties/lib/initializer.rb:460:in `prepare_dispatcher'
    /Users/ian/dev/sandbox/ticket63/config/../vendor/rails/railties/lib/initializer.rb:146:in `process'
    /Users/ian/dev/sandbox/ticket63/config/../vendor/rails/railties/lib/initializer.rb:89:in `send'
    /Users/ian/dev/sandbox/ticket63/config/../vendor/rails/railties/lib/initializer.rb:89:in `run'
    /Users/ian/dev/sandbox/ticket63/config/environment.rb:13
    /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    /opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:27:in `require'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:508:in `require'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:353:in `new_constants_in'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/activesupport/lib/active_support/dependencies.rb:508:in `require'
    /Users/ian/dev/sandbox/ticket63/vendor/rails/railties/lib/tasks/misc.rake:3
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:546:in `call'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:546:in `execute'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:541:in `each'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:541:in `execute'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:508:in `invoke_with_call_chain'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `synchronize'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `invoke_with_call_chain'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:518:in `invoke_prerequisites'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1183:in `each'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1183:in `send'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1183:in `each'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:515:in `invoke_prerequisites'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:507:in `invoke_with_call_chain'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `synchronize'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:501:in `invoke_with_call_chain'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:494:in `invoke'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1931:in `invoke_task'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `top_level'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `each'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1909:in `top_level'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1948:in `standard_exception_handling'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1903:in `top_level'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1881:in `run'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1948:in `standard_exception_handling'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake.rb:1878:in `run'
    /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.1/bin/rake:31
    /opt/local/bin/rake:16:in `load'
    /opt/local/bin/rake:16
    
    

    May 4th, 2008 @ 09:39 AM

  4. Michael Koziarski
    Michael Koziarski

    So it's trying to generate the finder sql for your has_many association, and needs the database connection in order to quote the sql,

    I'm not sure how easy this will be to patch, don't worry about the tests initially, try to figure out the solution ;)

    May 4th, 2008 @ 09:44 AM

  5. Ian White
    Ian White

    Well, a really trivial 'fix' is to just rescue observer instantiation, and log the error (patch given on right)

    Given that this is a bootstrapping dependency problem, this might be appropraite. It's a bit icky though.

    A more sophisticated version of this approach would be to have a config setting 'allow_observer_instatiation_failure', which is set by various bootstrapping tasks (like db:create, or db:migrate). Also, being more discriminating on error catching would be useful.

    Could you advise on whether this general approach (ignore snafus when bootstrapping) is likely to get traction? It could hide sins if used badly. It would be in effect saying 'bootstrapping is different to running the app'

    May 4th, 2008 @ 10:09 AM

  6. Ian White
    Ian White

    Even simpler: rescue the bootstrapping tasks' db loading

    Given that this is an edge case (trying to load up framework when db

    doesn't yet exist), it makes sense to allow for snafus that result

    from this.

    May 4th, 2008 @ 10:20 AM

  7. josh
    josh
    • State changed from new to open
    • Tag set to activerecord, bug

    Koz, you going apply this?

    July 17th, 2008 @ 01:08 AM

  8. Gerrit Kaiser
    Gerrit Kaiser

    This one trips me up a lot aswell. Pretty much makes db:create and co unusable for me (I’m heavily using branch-specific databases so have a need to create them even after the initial app generation).

    I don’t really see why db:create would depend on the whole environment at all… Couldn’t it just load the database.yml and use ActiveRecord independent of the app? That would reduce the startup time for those tasks aswell.

    October 6th, 2008 @ 01:52 AM

  9. Michael Koziarski
    Michael Koziarski

    That sounds like a much nicer fix, if you can make those tasks just read the yml file, (and some how know the RAILS_ENV) then we solve most of these issues.

    October 6th, 2008 @ 01:05 PM

  10. Gerrit Kaiser
    Gerrit Kaiser
    • Tag changed from activerecord, bug to activerecord, bug, create_database, databases.rake, patch, rake

    whipped up a patch against a03e2b356c66ddc8809fa2b23a2a7d652f173b8b that won’t load the full environment for db:create:* and db:drop but instead just performs the minium config loading necessary.

    successfully manually tested on a skeleton rails app (didn’t see any examples of how to automatically test rake tasks)

    October 22nd, 2008 @ 05:22 AM

  11. Aliaksey Kandratsenka
    Aliaksey Kandratsenka

    After Gerrit's patch db:drop stopped working for mysql. That's because mysql connection wasn't established before call to #drop_database. I'm attaching a trivial fix.

    November 28th, 2008 @ 03:41 PM

  12. Aliaksey Kandratsenka
    Aliaksey Kandratsenka

    Ah. I forgot to mention that Gerrit's patch fixed the problem for us.

    November 28th, 2008 @ 03:42 PM

  13. Repository
    Repository
    • State changed from open to committed

    (from [a0bc480e1ddcaa015927c2d941e7b96a8ce4f6fa]) establish mysql connection before dropping database

    Signed-off-by: Michael Koziarski michael@koziarski.com [#63 [BUG] rake db:create and observers state:committed] http://github.com/rails/rails/co...

    December 1st, 2008 @ 08:40 PM