thiagopradi / octopus

Database Sharding for ActiveRecord
2.53k stars 505 forks source link

rake db:migrate breaks #307

Open schowdhury opened 9 years ago

schowdhury commented 9 years ago

I am on rails 4. v 0.8.5 version of octopus

...
ar-octopus (0.8.5)
    activerecord (>= 3.2.0)
    activesupport (>= 3.2.0)
arel (4.0.2)
...

when I run rake, specifically rake db:migrate, I get the following error

ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:205:in `safe_connection'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:212:in `select_connection'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:273:in `method_missing'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/migration.rb:867:in `initialize'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/migration.rb:768:in `new'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/migration.rb:768:in `up'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/migration.rb:110:in `up_with_octopus'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/migration.rb:746:in `migrate'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/migration.rb:104:in `block in migrate_with_octopus'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:226:in `block (2 levels) in run_queries_on_shard'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:476:in `using_shard'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:225:in `block in run_queries_on_shard'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:462:in `keeping_connection_proxy'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:224:in `run_queries_on_shard'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:233:in `block in send_queries_to_multiple_shards'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:232:in `each'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/proxy.rb:232:in `send_queries_to_multiple_shards'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/ar-octopus-0.8.5/lib/octopus/migration.rb:103:in `migrate_with_octopus'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/gems/activerecord-4.0.10/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/bin/ruby_executable_hooks:15:in `eval'
/Users/sameerchowdhury/.rvm/gems/ruby-2.0.0-p598@icn/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
octopus:
  replicated: true
  fully_replicated: false
  environments:
    - development
    - staging
    - production

  development:
    slave1:
      adapter: postgresql
      host: <%= ENV['DB_HOSTNAME'] %>
      database: <%= ENV['DB_NAME'] %>
      port: <%= ENV['DB_PORT_SLAVE_1'] %>
      username: <%= ENV['DB_USERNAME'] %>
      password: <%= ENV['DB_PASSWORD'] %>
      pool: 5
      timeout: 5000

Any ideas about how I can fix this?

joaofraga commented 9 years ago

+1 Same here.

velles commented 9 years ago

try this

in config/initializers/octopus.rb

add

if Octopus.enabled? 
  Octopus.config[Rails.env.to_s]['master'] = ActiveRecord::Base.connection.config
  ActiveRecord::Base.connection.initialize_shards(Octopus.config)
end
raphi commented 9 years ago

I had the same problem and this fixed it. Thanks @velles Is this compatible with the unicorn config (same lines in the after_fork)?

AdamQuadmon commented 9 years ago

thanks again @velles, It worked for me as well, but I'd like to understand why it happened any clues?

randomm commented 9 years ago

I used this too. I had the same ActiveRecord::ConnectionNotEstablished problem occurring when the first ever query was routed through Octopus. However, if there were regular queries prior to a query through Octopus everything was hunky dory.

ronwong82 commented 8 years ago

I have this problem too. PG::ConnectionBad: could not connect to server: Connection timed out

adesnmi commented 7 years ago

Sorry for the bump on an old thread, but I recently just encountered the same issue on a project I'm working on. Is this simply an issue with rails 4? @velles trick also worked to fix it for me.

pboling commented 7 years ago

I am getting this error in production with Octopus 0.9.0, with Rails 4.2.7.1, on Ruby 2.2.6.

Notably we do have spring installed on the production machine, and we shouldn't. (https://github.com/rails/spring#deployment)

You must not install Spring on your production environment.

The error occurs directly following Spring loading:

Running via Spring preloader in process 33615
rake aborted!
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
.../activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:446:in `checkout_new_connection'

So it seems this issue may be related to, or the same as: https://github.com/thiagopradi/octopus/issues/303