3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

rails6にアップデートしたので `switch_point` gemを外す

Last updated at Posted at 2021-09-28

やりたいこと

  • 複数DBに接続する際にお世話になっていたgem switch_point がrails6.1はサポートしないということなのでで外す

対応したこと

switch_pointの削除

gem 'switch_point' # 削除

bundle install 実行

database.ymlの変更

変更前

default: &default
  adapter: mysql2
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: <%= ENV['DATABASE_HOST'] %>
  database: <%= ENV['DATABASE_NAME'] %>
  socket:  <%= ENV['DATABASE_SOCKET'] %>

default_readonly: &default_readonly
  <<: *default
  host: <%= ENV['DATABASE_HOST_READONLY'] %>

development:
  <<: *default
development_readonly:
  <<: *default_readonly

production:
  <<: *default
production_readonly:
  <<: *default_readonly

変更後

default: &default
  adapter: mysql2
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: <%= ENV['DATABASE_HOST'] %>
  database: <%= ENV['DATABASE_NAME'] %>
  socket:  <%= ENV['DATABASE_SOCKET'] %>

default_readonly: &default_readonly
  <<: *default
  host: <%= ENV['DATABASE_HOST_READONLY'] %>

development:
  primary:
    <<: *default
  primary_replica:
    <<: *default_readonly

production:
  primary:
    <<: *default
  primary_replica:
    <<: *default_readonly

ApplicationRecordの変更

変更前

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  use_switch_point :master
end

変更後

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  connects_to database: { writing: :primary, reading: :primary_replica }
end

ログ出力設定

MultipleDatabaseConnectionLoggerクラスの作成

class MultipleDatabaseConnectionLogger < Arproxy::Base
  def execute(sql, name = nil)
    role = ActiveRecord::Base.current_role
    name = "#{name} [#{role}]"
    super(sql, name)
  end
end

arproxy.rbの変更

変更前

if Rails.env.development? || Rails.env.test?
  require 'switch_point_logger_enhancement'
  Arproxy.configure do |config|
    config.adapter = 'mysql2'
    config.use SwitchPointLoggerEnhancement
  end
  Arproxy.enable!
end

変更後

if Rails.env.development? || Rails.env.test?
  require 'multiple_database_connection_logger'
  Arproxy.configure do |config|
    config.adapter = 'mysql2'
    config.use MultipleDatabaseConnectionLogger
  end
  Arproxy.enable!
end

不要なファイルの削除

  • config/initializer/switch_point.rb
  • lib/switch_point_logger_enhancement.rb

アプリケーションの変更

変更前

ApplicationRecord.with_readonly do
  ...
end

変更後

ApplicationRecord.connected_to(role: :reading) do
  ...
end

おまけ

私の場合ApplicationRecordwith_readonlyというメソッドを作って、今まで通りの書き方ができるようにしました。

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary_replica }

  def self.with_readonly(&block)
    ActiveRecord::Base.connected_to(role: :reading, &block)
  end
end

参考リンク

いつもQiitaにはお世話になってます。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?