Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.

Commit d5537a2

Browse files
authored
Merge pull request #291 from zendesk/bquorning.backport-master-branch-features
Backport of pull request #280 & make a new release
2 parents dac9086 + 0082394 commit d5537a2

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

Changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## v3.21.0
10+
11+
### Added
12+
13+
Add a global setting to disable marking instances from replicas as read-only. To enable:
14+
15+
`ActiveRecordShards.disable_replica_readonly_records = true`
16+
917
## v3.20.0
1018

1119
### Changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,16 @@ ActiveRecord::Base.on_replica do
148148
end
149149
```
150150

151-
This will perform the query on the replica, and mark the returned instances as read only. There is also a shortcut for this:
151+
This will perform the query on the replica, and mark the returned instances as read-only. There is also a shortcut for this:
152152

153153
```ruby
154154
Account.on_replica.find_by_big_expensive_query
155155
```
156156

157+
If you do not want instances returned from replicas to be marked as read-only, this can be disabled globally:
158+
159+
`ActiveRecordShards.disable_replica_readonly_records = true`
160+
157161
## Debugging
158162

159163
Show if a query went to primary or replica in the logs:

active_record_shards.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Gem::Specification.new "active_record_shards", "3.20.0" do |s|
1+
Gem::Specification.new "active_record_shards", "3.21.0" do |s|
22
s.authors = ["Benjamin Quorning", "Gabe Martin-Dempesy", "Pierre Schambacher", "Mick Staugaard", "Eric Chapweske", "Ben Osheroff"]
33
44
s.homepage = "https://github.com/zendesk/active_record_shards"

lib/active_record_shards.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
require 'active_record_shards/schema_dumper_extension'
1313

1414
module ActiveRecordShards
15+
class << self
16+
attr_accessor :disable_replica_readonly_records
17+
end
18+
1519
def self.app_env
1620
env = Rails.env if defined?(Rails.env)
1721
env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)

lib/active_record_shards/connection_switcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def on_cx_switch_block(which, force: false, construct_ro_scope: nil, &block)
125125

126126
# we avoid_readonly_scope to prevent some stack overflow problems, like when
127127
# .columns calls .with_scope which calls .columns and onward, endlessly.
128-
if self == ActiveRecord::Base || !switch_to_replica || construct_ro_scope == false
128+
if self == ActiveRecord::Base || !switch_to_replica || construct_ro_scope == false || ActiveRecordShards.disable_replica_readonly_records == true
129129
yield
130130
else
131131
readonly.scoping(&block)

test/connection_switching_test.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,42 @@ def spec_name
501501
assert_equal('replica_name', @model.name)
502502
end
503503

504-
it "be marked as read only" do
504+
it "be marked as read-only" do
505505
assert(@model.readonly?)
506506
end
507507

508508
it "be marked as comming from the replica" do
509509
assert(@model.from_replica?)
510510
end
511511

512+
describe "when ActiveRecordShards.disable_replica_readonly_records is true" do
513+
before do
514+
@original_disable_replica_readonly_records = ActiveRecordShards.disable_replica_readonly_records
515+
ActiveRecordShards.disable_replica_readonly_records = true
516+
517+
@model = Account.on_replica.find(1000)
518+
assert(@model)
519+
assert_equal('replica_name', @model.name)
520+
end
521+
522+
it "read from replica on reload" do
523+
@model.reload
524+
assert_equal('replica_name', @model.name)
525+
end
526+
527+
it "not be marked as read-only" do
528+
refute(@model.readonly?)
529+
end
530+
531+
it "be marked as comming from the replica" do
532+
assert(@model.from_replica?)
533+
end
534+
535+
after do
536+
ActiveRecordShards.disable_replica_readonly_records = @original_disable_replica_readonly_records
537+
end
538+
end
539+
512540
after do
513541
Account.on_replica_by_default = false
514542
end
@@ -543,7 +571,7 @@ def spec_name
543571
assert(@model.readonly?)
544572
end
545573

546-
it "not be marked as read only" do
574+
it "not be marked as read-only" do
547575
assert(!@model.readonly?)
548576
end
549577

0 commit comments

Comments
 (0)