From 5a682135f2e9955e16a87009985fcb4f5e0ff5f6 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Tue, 7 May 2024 16:37:41 +1000 Subject: [PATCH] feat: configure auto db switching --- config/environments/production.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/config/environments/production.rb b/config/environments/production.rb index a9504fc5c..c08645b4c 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -102,5 +102,28 @@ Rails.application.configure do # :sendgrid for Sendgrid config.action_mailbox.ingress = ENV.fetch('RAILS_INBOUND_EMAIL_SERVICE', 'relay').to_sym + # Enable Database Selector + # + # Inserts middleware to perform automatic connection switching. + # The `database_selector` hash is used to pass options to the DatabaseSelector + # middleware. The `delay` is used to determine how long to wait after a write + # to send a subsequent read to the primary. + # + # The `database_resolver` class is used by the middleware to determine which + # database is appropriate to use based on the time delay. + # + # The `database_resolver_context` class is used by the middleware to set + # timestamps for the last write to the primary. The resolver uses the context + # class timestamps to determine how long to wait before reading from the + # replica. + # + # By default Rails will store a last write timestamp in the session. The + # DatabaseSelector middleware is designed as such you can define your own + # strategy for connection switching and pass that into the middleware through + # these configuration options. + config.active_record.database_selector = { delay: 2.seconds } + config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver + config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session + Rails.application.routes.default_url_options = { host: ENV['FRONTEND_URL'] } end