@ledsun blog

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

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

一般的なRailsアプリケーションはconfig/database.ymlにデータベースへの接続情報があります。 これをつかってすでに存在しているデータベースからカラム情報を取得してみましょう。

必要なライブラリを読み込んでirbを起動します。

~ irb -r 'active_record' -r 'yaml'

データベースに接続します。

irb(main):001:0> ActiveRecord::Base.establish_connection YAML.load_file('config/database.yml')['development']
=>
#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fa3830aeb68
 @automatic_reconnect=true,
 @available=
  #<ActiveRecord::ConnectionAdapters::ConnectionPool::ConnectionLeasingQueue:0x00007fa3830acd18
   @cond=#<MonitorMixin::ConditionVariable:0x00007fa3830acb10 @cond=#<Thread::ConditionVariable:0x00007fa3830ac9d0>, @monitor=#<Monitor:0x00007fa3830adb00>>,
   @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fa3830aeb68 ...>,
   @num_waiting=0,
   @queue=[]>,
 @checkout_timeout=5.0,
 @connection_klass=ActiveRecord::Base,
 @connections=[],
 @db_config=
  #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fa3a3444fc8
   @configuration_hash={:adapter=>"sqlite3", :pool=>"<%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %>", :timeout=>5000, :database=>"db/development.sqlite3"},
   @env_name="default_env",
   @name="primary">,
 @idle_timeout=300.0,
 @lock_thread=false,
 @mon_data=#<Monitor:0x00007fa3830adb00>,
 @mon_data_owner_object_id=380,
 @now_connecting=0,
 @pool_config=
  #<ActiveRecord::ConnectionAdapters::PoolConfig:0x00007fa3830bf5d0
   @_mutex=#<Thread::Mutex:0x00007fa3830bf490>,
   @connection_klass=ActiveRecord::Base,
   @db_config=
    #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fa3a3444fc8
     @configuration_hash={:adapter=>"sqlite3", :pool=>"<%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %>", :timeout=>5000, :database=>"db/development.sqlite3"},
     @env_name="default_env",
     @name="primary">,
   @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fa3830aeb68 ...>>,
 @query_cache_enabled=
  #<Concurrent::Map:0x00007fa3830ad9e8 entries=0 default_proc=#<Proc:0x00007fa3830ad0b0 /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/query_cache.rb:32>>,
 @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x00007fa3830ac728 @frequency=60.0, @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00007fa3830aeb68 ...>>,
 @size=0,
 @thread_cached_conns=#<Concurrent::Map:0x00007fa3830aced0 entries=0 default_proc=nil>,
 @threads_blocking_new_connections=0>
irb(main):002:0>

適当なクラスを定義します。

irb(main):002:0> class Computer < ActiveRecord::Base; end

カラム情報を取得しようとするのデータベースへの接続エラーが起きます。

irb(main):003:0> Computer.columns
/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.001 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 /Users/shigerunakajima/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/model_schema.rb:553:in `load_schema!'
  ... 9 levels...

データベースへの接続情報が正しく取得できていないようです。

参考