Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading database.yml file with aliases #950

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_loading_database_yml_with_aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#947](https://github.com/rubocop/rubocop-rails/issues/947): Fix loading `database.yml` file with aliases. ([@fatkodima][])
5 changes: 3 additions & 2 deletions lib/rubocop/cop/rails/bulk_change_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,12 @@ def database_from_yaml
def database_yaml
return nil unless File.exist?('config/database.yml')

yaml = if YAML.respond_to?(:unsafe_load_file)
YAML.unsafe_load_file('config/database.yml')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I wondered if the issue would be fixed with this change. Because YAML.unsafe_load_file can accept aliases:

$ cat example.yaml
default: &default
  item: 42

foo:
  <<: *default
$ ruby -v
ruby 3.3.0dev (2023-03-17T00:50:41Z master f29c9d6d36) [x86_64-darwin19]

$ ruby -ryaml -e "p YAML.unsafe_load_file('./example.yaml')"
{"default"=>{"item"=>42}, "foo"=>{"item"=>42}}
$ ruby -ryaml -e "p YAML.load_file('./example.yaml')"
/Users/koic/.rbenv/versions/3.3.0-dev/lib/ruby/3.3.0+0/psych/visitors/to_ruby.rb:432:in `visit_Psych_Nodes_Alias': Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`. (Psych::AliasesNotEnabled)
        from /Users/koic/.rbenv/versions/3.3.0-dev/lib/ruby/3.3.0+0/psych/visitors/visitor.rb:30:in `visit'

I know that YAML.safe_load_file with aliases: true option can accept aliases as well, but YAML.unsafe_load_file should suffice:

$ ruby -ryaml -e "p YAML.safe_load_file('./example.yaml', aliases: true)"
{"default"=>{"item"=>42}, "foo"=>{"item"=>42}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not remember how I reproduced it before, but reverted this PR changes and it works.
I think, the user just forgot default: &default section in the database.yml, because in this case it raises the exact same error as in the issue.

yaml = if YAML.respond_to?(:safe_load_file)
YAML.safe_load_file('config/database.yml', aliases: true)
else
YAML.load_file('config/database.yml')
end

return nil unless yaml.is_a? Hash

config = yaml['development']
Expand Down
5 changes: 3 additions & 2 deletions spec/rubocop/cop/rails/bulk_change_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ def change

before do
allow(File).to receive(:exist?).with('config/database.yml').and_return(true)
if YAML.respond_to?(:unsafe_load_file)
allow(YAML).to receive(:unsafe_load_file).with('config/database.yml').and_return(yaml)
if YAML.respond_to?(:safe_load_file)
allow(YAML).to receive(:safe_load_file).with('config/database.yml',
hash_including(aliases: true)).and_return(yaml)
else
allow(YAML).to receive(:load_file).with('config/database.yml').and_return(yaml)
end
Expand Down