Skip to content

Commit b750ac7

Browse files
authored
Merge pull request #64 from aziflaj/patch-1
Add support for aliased database configs
2 parents c55d0ea + 13f4275 commit b750ac7

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

lib/database_cleaner/active_record/base.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ def connection_class
4646

4747
def load_config
4848
if db != :default && db.is_a?(Symbol) && File.file?(DatabaseCleaner::ActiveRecord.config_file_location)
49-
connection_details = YAML::load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result)
49+
connection_details =
50+
if RUBY_VERSION.match?(/\A2\.5/)
51+
YAML.safe_load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, [], [], true)
52+
else
53+
YAML.safe_load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, aliases: true)
54+
end
5055
@connection_hash = valid_config(connection_details, db.to_s)
5156
end
5257
end

spec/database_cleaner/active_record/base_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ module ActiveRecord
3838
expect(strategy.connection_hash).to eq({ 'database' => 'one' })
3939
end
4040

41+
context 'when the configs are aliased' do
42+
let(:other_db) { :other_db }
43+
let(:config_location) { 'spec/support/aliased-example.database.yml' }
44+
45+
it 'loads configs correctly' do
46+
strategy.db = my_db
47+
expected_hash = { 'database' => 'one',
48+
'encoding' => 'utf8',
49+
'reconnect' => true }
50+
expect(strategy.connection_hash).to eq(expected_hash)
51+
52+
strategy.db = other_db
53+
expected_hash.merge!('database' => 'two')
54+
expect(strategy.connection_hash).to eq(expected_hash)
55+
end
56+
end
57+
4158
context 'when ActiveRecord configuration contains a config for the given db' do
4259
if ::ActiveRecord.version >= Gem::Version.new('6.1')
4360
context 'ActiveRecord >= 6.1' do
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
default: &default
2+
encoding: utf8
3+
reconnect: true
4+
5+
my_db:
6+
<<: *default
7+
database: <%= "ONE".downcase %>
8+
9+
other_db:
10+
<<: *default
11+
database: <%= "TWO".downcase %>

0 commit comments

Comments
 (0)