ankane/blazer

ActiveRecord::ConnectionNotEstablished when using variables

UnderpantsGnome opened this issue · 1 comments

If I have a query with hard coded values the query runs fine, if I add a single variable I get the following error.

ActiveRecord::ConnectionNotEstablished in Blazer::Queries#show 

No connection pool for 'Blazer::Connection::Adapter85180' found for the 'reading' role.

Working query

SELECT COUNT(*) FROM users WHERE confirmed_at >= '2022-01-01T00:00:00Z'

Query throwing error

SELECT COUNT(*) FROM users WHERE confirmed_at >= {start_time}

This happens on 3.1 and master. We do have a primary and primary_replica in the production config, not sure if that's throwing something off.

I added a binding.pry to this method and it works the first 2 times it's called and fails on the third.

I'm pretty sure this has to do with the way they decided to implement primary/replica in Rails since it uses the HTTP verb to choose between the primary and the replica.

Processing by Blazer::QueriesController#show as HTML
  Parameters: {"bow_type"=>"pool", "id"=>"1-water-usage-scores-pools-10-point-buckets"}
  Blazer::Query Load (0.7ms)  SELECT `blazer_queries`.* FROM `blazer_queries` WHERE `blazer_queries`.`id` = 1 LIMIT 1
  Rendering layout ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/app/views/layouts/blazer/application.html.erb
  Rendering ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/app/views/blazer/queries/show.html.erb within layouts/blazer/application
  Rendered ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/app/views/blazer/_nav.html.erb (Duration: 0.2ms | Allocations: 207)
  Rendered ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/app/views/blazer/_variables.html.erb (Duration: 0.1ms | Allocations: 151)

From: ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/lib/blazer/adapters/sql_adapter.rb:207 Blazer::Adapters::SqlAdapter#quoting:

    206: def quoting
 => 207:   ->(value) { binding.pry; connection_model.connection.quote(value) }
    208: end

(development) >>
(development) >> connection_model.connection.quote(value)
=> "'pool'"
(development) >>

From: ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/lib/blazer/adapters/sql_adapter.rb:207 Blazer::Adapters::SqlAdapter#quoting:

    206: def quoting
 => 207:   ->(value) { binding.pry; connection_model.connection.quote(value) }
    208: end

(development) >>
(development) >> connection_model.connection.quote(value)
=> "'pool'"
(development) >>

  Rendered ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/app/views/blazer/queries/show.html.erb within layouts/blazer/application (Duration: 27324.3ms | Allocations: 48486)
  Rendered layout ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/app/views/layouts/blazer/application.html.erb (Duration: 27336.5ms | Allocations: 54120)
Completed 200 OK in 27338ms (Views: 27336.1ms | ActiveRecord: 1.4ms | Allocations: 55557)

Started POST "/admin/blazer/queries/run" for 127.0.0.1 at 2024-12-04 13:48:04 -0600
Processing by Blazer::QueriesController#run as HTML
  Parameters: {"statement"=>"...", "query_id"=>"1", "data_source"=>"main", "variables"=>{"bow_type"=>"pool"}, "run_id"=>"c79a18b2-9af3-4ee3-a27e-9922f321f0a7"}
  Blazer::Query Load (0.3ms)  SELECT `blazer_queries`.* FROM `blazer_queries` WHERE `blazer_queries`.`id` = 1 LIMIT 1

From: ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/blazer-3.1.0/lib/blazer/adapters/sql_adapter.rb:207 Blazer::Adapters::SqlAdapter#quoting:

    206: def quoting
 => 207:   ->(value) { binding.pry; connection_model.connection.quote(value) }
    208: end

(development) >> connection_model.connection.quote(value)
ActiveRecord::ConnectionNotEstablished: No connection pool for 'Blazer::Connection::Adapter84840' found.
from ~/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.5/lib/active_record/connection_adapters/abstract/connection_handler.rb:243:in `retrieve_connection'
ankane commented

Hi @UnderpantsGnome, it looks like your application is configured to use automatic role switching, which isn't supported.