ActiveRecordを使って複数DBにアクセスする(その1)

Posted on December 03, 2017 23:35
Share on:

この記事は、株式会社ケーシーエスキャロット アドベントカレンダーの2日目の記事です。

ちょっと公開が遅くなってしまいましたが、やっていきます。

1日目に準備をしたので、今日はいよいよプログラムを書いていきましょう。
以下のような構成で作成していきます。


config/database.yml

own(development) / other(other_development) のKEYで接続情報を記載します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock

development:
  <<: *default
  database: multi_connect_development

other_development:
  <<: *default
  database: other_multi_connect_development

lib/common/user.rb

own /other のUserクラスの親クラスとなるクラスです。

同じ構造のテーブルにアクセスするので、共通のクラスを作って、
このクラスは、AciveRecord::Baseを継承します。

1
2
class Common::User < ActiveRecord::Base
end

lib/own/user.rb, lib/other/user.rb のそれぞれのデータベースにアクセスするためのクラスとしています。

lib/own.rb

lib/own.rb には、autoload でUserクラスの読み込みと、DBの情報を返す connectメソッドを定義しておきます。

1
2
3
4
5
6
7
8
module Own
  autoload :User, 'own/user'

  def connect
    MultiConnectConfig.config["development"]
  end
  module_function :connect
end

DB接続は、establish_connection をモデルクラスに記載するとできるとの情報を見つけたので、
lib/own/user.rb には、establish_connection を記載します。

lib/own/user.rb

1
2
3
class Own::User < Common::User
  establish_connection Own::connect
end

また、other 側も同様に記載しておきます。

bin/multi_connect.rb

実際の呼び出すプログラムは、以下のように書きました。
config/database.ymlを管理するクラス(MultiConnectConfig)を記載しています。
(別ファイルにすることが望ましいです)

そして、Own側とOther側のUserの1件目をそれぞれ呼び出すプログラムです。

1
2
3
4
5
6
7
8
9
10
11
12
class MultiConnectConfig
  file_path = "./config/database.yml"
  @@config = YAML.load_file(file_path)

  def self.config
    @@config
  end
end

p Own::User.first
p Other::User.first

結果

以下のエラーになりました。
(2.5からbacktraceの出力が逆になる予定で、この2.5-devのバージョンも逆になっています

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[vagrant@rg batch]$ bundle exec ruby bin/multi_connect.rb
Traceback (most recent call last):
        14: from bin/multi_connect.rb:19:in `<main>'
        13: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/querying.rb:3:in `first'
        12: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/relation/finder_methods.rb:122:in `first'
        11: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/relation/finder_methods.rb:531:in `find_nth'
        10: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/relation/finder_methods.rb:538:in `find_nth_with_limit'
         9: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/relation/delegation.rb:44:in `primary_key'
         8: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/attribute_methods/primary_key.rb:74:in `primary_key'
         7: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/attribute_methods/primary_key.rb:88:in `reset_primary_key'
         6: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/attribute_methods/primary_key.rb:74:in `primary_key'
         5: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/attribute_methods/primary_key.rb:86:in `reset_primary_key'
         4: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/attribute_methods/primary_key.rb:98:in `get_primary_key'
         3: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/model_schema.rb:331:in `table_exists?'
         2: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:88:in `connection'
         1: from /vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
/vagrant/adventcalendar/batch/vendor/ruby/2.5.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:930:in `retrieve_connection': No connection pool with 'primary' found. (ActiveRecord::ConnectionNotEstablished)

No connection pool with ‘primary’ found.
primary って何でしょう? 次はこのエラーを見ながら、進めていきます。

そして3日目は、naokishi の 「資格取得のススメ(仮)」です。

naokishi と一緒に青物横丁で仕事していたのは…もう17年くらい前!?
チームが変わって、同じプロジェクトで仕事することはなくなったけれど、
いい意味で「サラリーマン気質」で、いつもみんなを気遣ってくれるのは、
あの頃も今も変わらずです。

最近は、お互い日本酒党になったので、また美味しいお酒飲み行きましょう~