Skip to content

Commit 61e4a9e

Browse files
committed
Process database.yml configuration via ERB
1 parent 3914bd6 commit 61e4a9e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

script/jack_hammer

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ require 'optparse'
1515
require 'bundler/setup'
1616
require 'active_record'
1717
require 'database_cleaner'
18+
require 'erb'
19+
require 'yaml'
1820

1921
support = File.expand_path("../../spec/support/", __FILE__)
2022

@@ -25,7 +27,11 @@ if db_engine == 'sqlite'
2527
exit 0
2628
end
2729

28-
ActiveRecord::Base.establish_connection YAML.load_file(File.join(support, "database.yml"))[db_engine]
30+
database_config_file = File.join(__dir__, '..', 'spec', 'support', 'database.yml')
31+
database_config_raw = File.read(database_config_file)
32+
database_config_yaml = ERB.new(database_config_raw).result
33+
database_config = YAML.load(database_config_yaml)
34+
ActiveRecord::Base.establish_connection(database_config[db_engine])
2935
require "#{support}/schema"
3036

3137
lib = File.expand_path('../../lib', __FILE__)

spec/support/database.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
require 'active_record'
22
require 'database_cleaner'
3+
require 'erb'
4+
require 'yaml'
35

46
FileUtils.mkdir_p 'tmp'
57

68
db_engine = ENV['DB'] || 'mysql'
7-
database_config_file = File.expand_path('../database.yml', __FILE__)
9+
database_config_file = File.join(__dir__, 'database.yml')
810

911
raise <<-MSG.strip_heredoc unless File.exist?(database_config_file)
1012
Please configure your spec/support/database.yml file.
1113
See spec/support/database.example.yml'
1214
MSG
1315

1416
ActiveRecord::Base.belongs_to_required_by_default = true if ActiveRecord.version.version >= '5'
15-
ActiveRecord::Base.establish_connection(YAML.load_file(database_config_file)[db_engine])
17+
database_config_raw = File.read(database_config_file)
18+
database_config_yaml = ERB.new(database_config_raw).result
19+
database_config = YAML.load(database_config_yaml)
20+
ActiveRecord::Base.establish_connection(database_config[db_engine])
1621

1722
RSpec.configure do |config|
1823
config.before(:suite) do

0 commit comments

Comments
 (0)