@ledsun blog

Hのキーがhellで、Sのキーがslaveだ、と彼は思った。そしてYのキーがyouだ。

既存のRailsプロジェクトからカラム情報を取得する(原因)

既存のRailsプロジェクトなので、rails consoleで使用しているデータベースへの接続情報を見れば原因がわかるはずです。

~ bin/rails console
Running via Spring preloader in process 24178
Loading development environment (Rails 6.1.3.2)
irb(main):001:0> Computer.connection.instance_variable_get('@config')
   (0.5ms)  SELECT sqlite_version(*)
=> {:adapter=>"sqlite3", :pool=>5, :timeout=>5000, :database=>"/Users/shigerunakajima/pc_management_desk/db/development.sqlite3"}
~ irb -r 'yaml'
irb(main):001:0> YAML.load_file('config/database.yml')['development']
=> {"adapter"=>"sqlite3", "pool"=>"<%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %>", "timeout"=>5000, "database"=>"db/development.sqlite3"}

違いは

  1. pool
  2. database

あとは接続してみればわかります。

ActiveRecord::Base.establish_connection({:adapter=>"sqlite3", :pool=>5000, :timeout=>5000, :database=>"/Users/shigerunakajima/pc_management_desk/db/development.sqlite3"})
ActiveRecord::Base.connection

は成功します。

ActiveRecord::Base.establish_connection({:adapter=>"sqlite3", :pool=>"<%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %>", :timeout=>5000, :database=>"db/development.sqlite3"})
ActiveRecord::Base.connection

は失敗します。エラーメッセージも同じです。

/Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:210:in `block in wait_poll': could not obtain a connection from the pool within 5.000 seconds (waited 5.003 seconds); all pooled connections were in use (ActiveRecord::ConnectionTimeoutError)
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:199:in `loop'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:199:in `wait_poll'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:160:in `internal_poll'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:286:in `internal_poll'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:155:in `block in poll'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/3.0.0/monitor.rb:202:in `synchronize'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/3.0.0/monitor.rb:202:in `mon_synchronize'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:164:in `synchronize'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:155:in `poll'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:870:in `acquire_connection'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:1128:in `retrieve_connection'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_handling.rb:327:in `retrieve_connection'
    from /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_handling.rb:283:in `connection'
    from (irb):5:in `<main>'
    ... 3 levels...

poolの文字列が問題であることがわかりました。

参考