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

Rake task's config method is globally accessible, enabling unexpected execution and causing undesirable side effects #134

Open
lukinski opened this issue May 9, 2024 · 0 comments · Fixed by #143

Comments

@lukinski
Copy link
Contributor

lukinski commented May 9, 2024

In my project I use clickhouse-activerecord together with ActiveAdmin 3.2.0. I've encountered an issue with the config method defined in the clickhouse.rake file, which seems to be conflicting with the config method in the ActiveAdmin::Views::Pages::Show class.

In the ActiveAdmin::Views::Pages::Show class, the config method tries to get a page presenter using active_admin_config.get_page_presenter(:show). If this returns nil, it calls super, which should invoke the config method in the parent class.

However, it appears that the config method in clickhouse.rake is being called instead. This results in an error, as the config method in clickhouse.rake is not compatible with the expectations of the config method in ActiveAdmin::Views::Pages::Show.

Looks like causing change has been introduced here 535ca30#diff-258ec640a5a16784999ad47ad0fbe3819f003c7d8975589fb493c84b812bb1e0R89.

Below is an example illustrating the described issue:

[4, 13] in /Users/(...)/gems/activeadmin-3.2.0/lib/active_admin/views/pages/show.rb
    4:     module Pages
    5:       class Show < Base
    6:
    7:         def config
    8:           byebug
=>  9:           active_admin_config.get_page_presenter(:show) || super
   10:         end
   11:
   12:         def title
   13:           if config[:title]
(byebug) active_admin_config.get_page_presenter(:show)
nil
(byebug) super
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x000000010970bea8 @env_name="test", @name="clickhouse", @configuration_hash={:adapter=>"clickhouse", :database=>"clickhouse_test", :username=>nil, :password=>nil, :host=>nil, :port=>8123, :pool=>5, :read_timeout=>10, :migrations_paths=>"db/clickhouse/migrate", :debug=>true}>
(byebug) super[:title]
*** NoMethodError Exception: undefined method '[]' for #<ActiveRecord::DatabaseConfigurations::HashConfig:0x000000010970bea8 @env_name="test", @name="clickhouse", @configuration_hash={:adapter=>"clickhouse", :database=>"clickhouse_test", :username=>nil, :password=>nil, :host=>nil, :port=>8123, :pool=>5, :read_timeout=>10, :migrations_paths=>"db/clickhouse/migrate", :debug=>true}>

nil
(byebug)

This could potentially impact any other pieces of code. As a proposed solution, I suggest the following change in clickhouse.rake:

class ClickhouseActiverecord::Tasks::Configuration
  def self.config
    ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: 'clickhouse')
  end
end

And then referring to the config method like this:

task dump: ['db:check_protected_environments'] do
  ClickhouseActiverecord::Tasks.new(
    ClickhouseActiverecord::Tasks::Configuration.config
  ).structure_dump(Rails.root.join('db/clickhouse_structure.sql'))
end

Thanks for your time.

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 a pull request may close this issue.

2 participants