~cytrogen/masto-fe

ref: d7254bd3327c5ad208b7f74aade749234441b3bc masto-fe/app/helpers/database_helper.rb -rw-r--r-- 514 bytes
d7254bd3 — Laura Hausmann Update default local-settings 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true

module DatabaseHelper
  def replica_enabled?
    ENV['REPLICA_DB_NAME'] || ENV.fetch('REPLICA_DATABASE_URL', nil)
  end
  module_function :replica_enabled?

  def with_read_replica(&block)
    if replica_enabled?
      ApplicationRecord.connected_to(role: :reading, prevent_writes: true, &block)
    else
      yield
    end
  end

  def with_primary(&block)
    if replica_enabled?
      ApplicationRecord.connected_to(role: :writing, &block)
    else
      yield
    end
  end
end