Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read bookable slots from the replica #1017

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/controllers/api/v1/bookable_slots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/bookable_slots_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class BookableSlotsController < ApplicationController
around_action :read_from_replica

def index
@bookable_slots = bookable_slots

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

connects_to database: { writing: :primary, reading: :primary, bookable_slots: :secondary }
end
36 changes: 30 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand All @@ -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