diff --git a/app/controllers/api/v1/bookable_slots_controller.rb b/app/controllers/api/v1/bookable_slots_controller.rb index 66a8f3fa..d59cbf5d 100644 --- a/app/controllers/api/v1/bookable_slots_controller.rb +++ b/app/controllers/api/v1/bookable_slots_controller.rb @@ -8,11 +8,13 @@ class InvalidScheduleType < StandardError; end end def index - @slots = BookableSlot.grouped(filtered_provider_ids, schedule_type, day) + ActiveRecord::Base.connected_to(role: :bookable_slots) do + @slots = BookableSlot.grouped(filtered_provider_ids, schedule_type, day) - Rails.logger.info("Empty times returned for #{day}") if day.present? && @slots.empty? + Rails.logger.info("Empty times returned for #{day}") if day.present? && @slots.empty? - render json: @slots + render json: @slots + end end private diff --git a/app/controllers/bookable_slots_controller.rb b/app/controllers/bookable_slots_controller.rb index b618c6e5..597e9a9a 100644 --- a/app/controllers/bookable_slots_controller.rb +++ b/app/controllers/bookable_slots_controller.rb @@ -1,4 +1,6 @@ class BookableSlotsController < ApplicationController + around_action :read_from_replica + def index @bookable_slots = bookable_slots @@ -51,6 +53,10 @@ def scoped_to_me? private + def read_from_replica(&block) + ActiveRecord::Base.connected_to(role: :bookable_slots, &block) + end + def schedule_type params[:schedule_type] end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 10a4cba8..6a505104 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,3 +1,5 @@ class ApplicationRecord < ActiveRecord::Base self.abstract_class = true + + connects_to database: { writing: :primary, reading: :primary, bookable_slots: :secondary } end diff --git a/config/database.yml b/config/database.yml index 6bbac3b5..e4c82f81 100644 --- a/config/database.yml +++ b/config/database.yml @@ -22,8 +22,13 @@ default: &default pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: - <<: *default - database: telephone_appointment_planner_development + primary: + <<: *default + database: telephone_appointment_planner_development + secondary: + <<: *default + database: telephone_appointment_planner_development # just use the primary as a stand in here + replica: true # The specified database role being used to connect to postgres. # To create additional roles in postgres see `$ createuser --help`. @@ -56,8 +61,22 @@ development: # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: - <<: *default - database: telephone_appointment_planner_test<%= ENV['TEST_ENV_NUMBER'] %> + primary: + <<: *default + database: telephone_appointment_planner_test + secondary: + <<: *default + database: telephone_appointment_planner_test + replica: true + +staging: + primary: + <<: *default + url: <%= ENV['DATABASE_URL'] %> + secondary: + <<: *default + url: <%= ENV['DATABASE_URL'] %> # use this as a stand-in for now + replica: true # As with config/secrets.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is @@ -79,5 +98,10 @@ test: # url: <%= ENV['DATABASE_URL'] %> # production: - <<: *default - url: <%= ENV['DATABASE_URL'] %> + primary: + <<: *default + url: <%= ENV['DATABASE_URL'] %> + secondary: + <<: *default + url: <%= Rails.env.production? ? ENV['SECONDARY_DATABASE_URL'] : ENV['DATABASE_URL'] %> + replica: true