diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2019-03-06 13:02:48 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-06 13:02:48 -0500 |
commit | a2bc42f1e78fbc48f7810abf5970268ccefaf021 (patch) | |
tree | e4f10993ef691b4be4ec6f8908132423a6577ef2 /railties/lib/rails/application | |
parent | 1818c4e8b49d053ca7a220288dd6823984bc0328 (diff) | |
parent | 37d1429ab1da78a3b8afcfcec8a135ac85cd897a (diff) | |
download | rails-a2bc42f1e78fbc48f7810abf5970268ccefaf021.tar.gz rails-a2bc42f1e78fbc48f7810abf5970268ccefaf021.tar.bz2 rails-a2bc42f1e78fbc48f7810abf5970268ccefaf021.zip |
Merge pull request #35497 from eileencodes/parse-yaml-without-erb-for-rake-tasks
Load YAML for rake tasks without parsing ERB
Diffstat (limited to 'railties/lib/rails/application')
-rw-r--r-- | railties/lib/rails/application/configuration.rb | 20 | ||||
-rw-r--r-- | railties/lib/rails/application/dummy_erb_compiler.rb | 16 |
2 files changed, 36 insertions, 0 deletions
diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index af3ec36064..83a7b6cf01 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -184,6 +184,26 @@ module Rails end end + # Load the database YAML without evaluating ERB. This allows us to + # create the rake tasks for multiple databases without filling in the + # configuration values or loading the environment. Do not use this + # method. + # + # This uses a DummyERB custom compiler so YAML can ignore the ERB + # tags and load the database.yml for the rake tasks. + def load_database_yaml # :nodoc: + if path = paths["config/database"].existent.first + require "rails/application/dummy_erb_compiler" + + yaml = Pathname.new(path) + erb = DummyERB.new(yaml.read) + + YAML.load(erb.result) + else + {} + end + end + # Loads and returns the entire raw configuration of database from # values stored in <tt>config/database.yml</tt>. def database_configuration diff --git a/railties/lib/rails/application/dummy_erb_compiler.rb b/railties/lib/rails/application/dummy_erb_compiler.rb new file mode 100644 index 0000000000..4e92526969 --- /dev/null +++ b/railties/lib/rails/application/dummy_erb_compiler.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# These classes are used to strip out the ERB configuration +# values so we can evaluate the database.yml without evaluating +# the ERB values. +class DummyERB < ERB # :nodoc: + def make_compiler(trim_mode) + DummyCompiler.new trim_mode + end +end + +class DummyCompiler < ERB::Compiler # :nodoc: + def compile_content(stag, out) + out.push "_erbout << 'dummy_compiler'" + end +end |