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

Commit dac9086

Browse files
authored
Merge pull request #285 from zendesk/bquorning.backport-master-branch-features
Backport master branch features
2 parents 0dbb545 + 3d41a38 commit dac9086

File tree

10 files changed

+93
-16
lines changed

10 files changed

+93
-16
lines changed

Changelog.md

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

77
## [Unreleased]
88

9+
## v3.20.0
10+
11+
### Changed
12+
13+
Rename `ActiveRecordShards.rails_env` to `ActiveRecordShards.app_env`, and include `APP_ENV` and `ENV['APP_ENV']` in the list of places it looks for environment information.
14+
915
## v3.19.3
1016

1117
### Fixed

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.19.3" do |s|
1+
Gem::Specification.new "active_record_shards", "3.20.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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
require 'active_record_shards/schema_dumper_extension'
1313

1414
module ActiveRecordShards
15-
def self.rails_env
15+
def self.app_env
1616
env = Rails.env if defined?(Rails.env)
1717
env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
1818
env ||= ENV['RAILS_ENV']
19+
env ||= APP_ENV if Object.const_defined?(:APP_ENV)
20+
env ||= ENV['APP_ENV']
1921
env || 'development'
2022
end
2123
end

lib/active_record_shards/connection_switcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def switch_connection(options)
191191
end
192192

193193
def shard_env
194-
ActiveRecordShards.rails_env
194+
ActiveRecordShards.app_env
195195
end
196196

197197
# Make these few schema related methods available before having switched to

lib/active_record_shards/shard_selection.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def shard_name(klass = nil, try_replica = true)
2626
the_shard = shard(klass)
2727

2828
@shard_names ||= {}
29-
@shard_names[ActiveRecordShards.rails_env] ||= {}
30-
@shard_names[ActiveRecordShards.rails_env][the_shard] ||= {}
31-
@shard_names[ActiveRecordShards.rails_env][the_shard][try_replica] ||= {}
32-
@shard_names[ActiveRecordShards.rails_env][the_shard][try_replica][@on_replica] ||= begin
33-
s = ActiveRecordShards.rails_env.dup
29+
@shard_names[ActiveRecordShards.app_env] ||= {}
30+
@shard_names[ActiveRecordShards.app_env][the_shard] ||= {}
31+
@shard_names[ActiveRecordShards.app_env][the_shard][try_replica] ||= {}
32+
@shard_names[ActiveRecordShards.app_env][the_shard][try_replica][@on_replica] ||= begin
33+
s = ActiveRecordShards.app_env.dup
3434
s << "_shard_#{the_shard}" if the_shard
3535
s << "_replica" if @on_replica && try_replica
3636
s
@@ -50,7 +50,7 @@ def shard
5050
PRIMARY = "primary"
5151
def resolve_connection_name(sharded:, configurations:)
5252
resolved_shard = sharded ? shard : nil
53-
env = ActiveRecordShards.rails_env
53+
env = ActiveRecordShards.app_env
5454

5555
@connection_names ||= {}
5656
@connection_names[env] ||= {}

lib/active_record_shards/tasks.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
desc 'Drops the database for the current RAILS_ENV including shards'
1111
task drop: :load_config do
1212
ActiveRecord::Base.configurations.to_h.each do |key, conf|
13-
next if !key.start_with?(ActiveRecordShards.rails_env) || key.end_with?("_replica", "_slave")
13+
next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica", "_slave")
1414

1515
begin
1616
ActiveRecordShards::Tasks.root_connection(conf).drop_database(conf['database'])
@@ -31,7 +31,7 @@
3131
desc "Create the database defined in config/database.yml for the current RAILS_ENV including shards"
3232
task create: :load_config do
3333
ActiveRecord::Base.configurations.to_h.each do |key, conf|
34-
next if !key.start_with?(ActiveRecordShards.rails_env) || key.end_with?("_replica", "_slave")
34+
next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica", "_slave")
3535

3636
begin
3737
# MysqlAdapter takes charset instead of encoding in Rails 4.2 or greater
@@ -48,7 +48,7 @@
4848
end
4949
end
5050
end
51-
ActiveRecord::Base.establish_connection(ActiveRecordShards.rails_env.to_sym)
51+
ActiveRecord::Base.establish_connection(ActiveRecordShards.app_env.to_sym)
5252
end
5353

5454
desc "Raises an error if there are pending migrations"

test/active_record_shards_test.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'helper'
4+
5+
describe 'ActiveRecordShards' do
6+
describe '.app_env' do
7+
before do
8+
if defined?(Rails) || ENV['RAILS_ENV'] || defined?(APP_ENV) || ENV['APP_ENV']
9+
raise "Tests in #{__FILE__} will overwrite environment constants, please update them to avoid conflicts"
10+
end
11+
12+
Object.send(:remove_const, 'RAILS_ENV')
13+
end
14+
15+
after { Object.const_set('RAILS_ENV', 'test') }
16+
17+
describe 'Rails.env' do
18+
before do
19+
class Rails
20+
def self.env
21+
'environment from Rails.env'
22+
end
23+
end
24+
end
25+
26+
after { Object.send(:remove_const, 'Rails') }
27+
28+
it 'looks for Rails.env' do
29+
assert_equal 'environment from Rails.env', ActiveRecordShards.app_env
30+
end
31+
end
32+
33+
describe 'RAILS_ENV' do
34+
before { Object.const_set('RAILS_ENV', 'environment from RAILS_ENV') }
35+
after { Object.send(:remove_const, 'RAILS_ENV') }
36+
37+
it 'looks for RAILS_ENV' do
38+
assert_equal 'environment from RAILS_ENV', ActiveRecordShards.app_env
39+
end
40+
end
41+
42+
describe "ENV['RAILS_ENV']" do
43+
before { ENV['RAILS_ENV'] = "environment from ENV['RAILS_ENV']" }
44+
after { ENV.delete('RAILS_ENV') }
45+
46+
it 'looks for RAILS_ENV' do
47+
assert_equal "environment from ENV['RAILS_ENV']", ActiveRecordShards.app_env
48+
end
49+
end
50+
51+
describe 'APP_ENV' do
52+
before { Object.const_set('APP_ENV', 'environment from APP_ENV') }
53+
after { Object.send(:remove_const, 'APP_ENV') }
54+
55+
it 'looks for APP_ENV' do
56+
assert_equal 'environment from APP_ENV', ActiveRecordShards.app_env
57+
end
58+
end
59+
60+
describe "ENV['APP_ENV']" do
61+
before { ENV['APP_ENV'] = "environment from ENV['APP_ENV']" }
62+
after { ENV.delete('APP_ENV') }
63+
64+
it 'looks for APP_ENV' do
65+
assert_equal "environment from ENV['APP_ENV']", ActiveRecordShards.app_env
66+
end
67+
end
68+
end
69+
end

test/connection_switching_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class ShardedModel < ActiveRecord::Base
340340
end
341341

342342
describe "in an environment without replica" do
343-
switch_rails_env('test3')
343+
switch_app_env('test3')
344344
def spec_name
345345
ActiveRecord::Base.connection_pool.spec.name
346346
end
@@ -369,7 +369,7 @@ def spec_name
369369
end
370370

371371
describe "in an unsharded environment" do
372-
switch_rails_env('test2')
372+
switch_app_env('test2')
373373

374374
describe "shard switching" do
375375
it "just stay on the main db" do

test/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def migrator(direction = :up, path = 'migrations', target_version = nil)
183183
Minitest::Spec.include(SpecHelpers)
184184

185185
module RailsEnvSwitch
186-
def switch_rails_env(env)
186+
def switch_app_env(env)
187187
before do
188188
silence_warnings { Object.const_set("RAILS_ENV", env) }
189189
ActiveRecord::Base.establish_connection(::RAILS_ENV.to_sym)

test/migrator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
describe "when DB is empty" do
1111
extend RailsEnvSwitch
1212

13-
switch_rails_env('test3')
13+
switch_app_env('test3')
1414

1515
it "makes meta tables" do
1616
ActiveRecord::Base.on_shard(nil) do

0 commit comments

Comments
 (0)