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

Conversation

fatkodima
Copy link
Contributor

Fixes #947.
From that issue, it is clear that the error is related to parsing yaml with aliases.

Based on the solution from sidekiq/sidekiq#5141.

@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'yaml'
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need it for YAML.safe_load? Is it required somewhere else?

@@ -207,6 +206,14 @@ def database_yaml
nil
end

def load_yaml(src)
if Psych::VERSION > '4.0'
Copy link
Member

Choose a reason for hiding this comment

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

Based on #516 link, So I'm not sure Psych::VERSION > 4.0 is better than respond_to?(:unsafe_load_file).

@@ -207,6 +206,14 @@ def database_yaml
nil
end

def load_yaml(src)
if Psych::VERSION > '4.0'
YAML.safe_load(src, aliases: true)
Copy link
Member

Choose a reason for hiding this comment

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

Can you use YAML.safe_load_file('config/database.yml', aliases: true) instead of YAML.safe_load(src, aliases: true)?

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 checked the sources for psych, and seems like :aliases are supported only by safe_load, not safe_load_file (which is a bit surprising to me).

Copy link
Member

Choose a reason for hiding this comment

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

Hm, it seems that keyword args are delegated. So :aliases will also be delegated from safe_load_file to safe_load.
https://github.com/ruby/psych/blob/v4.0.0/lib/psych.rb#L580-L589

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Made an update.

@fatkodima fatkodima force-pushed the fix-loading-database-yml-with-aliases branch from df1fd92 to ca1bc98 Compare March 13, 2023 11:15
@@ -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.

@fatkodima fatkodima closed this Mar 29, 2023
@fatkodima fatkodima deleted the fix-loading-database-yml-with-aliases branch March 29, 2023 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

An error occurred while Rails/BulkChangeTable cop was inspecting
2 participants