File tree 5 files changed +34
-1
lines changed
5 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 1
1
## Unreleased
2
2
3
+ - Allow setting ` Knockoff.default_target ` to set the default target other than ` :primary ` (https://github.com/joinhandshake/knockoff/pull/11 )
3
4
- Drop Ruby 2.2 support
4
5
- Add Ruby 2.5 support
Original file line number Diff line number Diff line change 10
10
module Knockoff
11
11
class << self
12
12
attr_accessor :enabled
13
+ attr_reader :default_target
13
14
14
15
def on_replica ( check_transaction : true , &block )
15
16
Base . new ( :replica , check_transaction : check_transaction ) . run ( &block )
@@ -19,6 +20,10 @@ def on_primary(&block)
19
20
Base . new ( :primary ) . run ( &block )
20
21
end
21
22
23
+ def default_target = ( target )
24
+ @default_target = Base . new ( target ) . target
25
+ end
26
+
22
27
def replica_pool
23
28
@replica_pool ||= ReplicaConnectionPool . new ( config . replica_database_keys )
24
29
end
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ class << self
4
4
alias_method :original_connection , :connection
5
5
6
6
def connection
7
- case Thread . current [ :knockoff ]
7
+ target = Thread . current [ :knockoff ] || Knockoff . default_target
8
+ case target
8
9
when :replica
9
10
# Attempts to use a random replica connection, but otherwise falls back to primary
10
11
Knockoff . replica_pool . random_replica_connection . original_connection
Original file line number Diff line number Diff line change 1
1
module Knockoff
2
2
class Base
3
+ attr_reader :target
4
+
3
5
def initialize ( target , check_transaction : true )
4
6
@target = decide_with ( target , check_transaction )
5
7
end
Original file line number Diff line number Diff line change @@ -61,6 +61,30 @@ def on_replica?
61
61
end
62
62
end
63
63
64
+ context 'allows for setting the default target' do
65
+ after ( :each ) { Knockoff . instance_variable_set ( "@default_target" , nil ) }
66
+
67
+ it 'sets the target' do
68
+ expect ( Knockoff . default_target ) . to be nil
69
+ Knockoff . default_target = :replica
70
+ expect ( Knockoff . default_target ) . to eq :replica
71
+ Knockoff . on_primary { expect ( on_replica? ) . to be false }
72
+ expect ( Knockoff . default_target ) . to eq :replica
73
+ Knockoff . default_target = :primary
74
+ expect ( Knockoff . default_target ) . to eq :primary
75
+ end
76
+
77
+ it 'returns the correct connection' do
78
+ expect ( ActiveRecord ::Base . connection ) . to eq ActiveRecord ::Base . original_connection
79
+ Knockoff . default_target = :replica
80
+ expect ( ActiveRecord ::Base . connection ) . to eq Knockoff ::KnockoffReplica0 . connection
81
+ Knockoff . on_primary { expect ( ActiveRecord ::Base . connection ) . to eq ActiveRecord ::Base . original_connection }
82
+ expect ( ActiveRecord ::Base . connection ) . to_not eq ActiveRecord ::Base . original_connection
83
+ Knockoff . default_target = :primary
84
+ expect ( ActiveRecord ::Base . connection ) . to eq ActiveRecord ::Base . original_connection
85
+ end
86
+ end
87
+
64
88
context 'in transaction' do
65
89
it 'raises error in transaction if replica is attempted' do
66
90
User . transaction do
You can’t perform that action at this time.
0 commit comments