annotaterb icon indicating copy to clipboard operation
annotaterb copied to clipboard

Compatibility with `activerecord-tenanted`

Open chrise86 opened this issue 3 months ago • 2 comments

The naming may be a little misleading here, as the following issue occurs when trying to annotate tenanted models, even though the error comes from the UntenantedConnectionPool class:

Annotating models
/Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/rails-cf6b3107753a/activerecord/lib/active_record/model_schema.rb:540:in 'ActiveRecord::ModelSchema::ClassMethods#reset_column_information': undefined method 'active_connection' for an instance of ActiveRecord::Tenanted::UntenantedConnectionPool (NoMethodError)

        connection_pool.active_connection&.clear_cache!
                       ^^^^^^^^^^^^^^^^^^
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/model_annotator/project_annotator.rb:42:in 'AnnotateRb::ModelAnnotator::ProjectAnnotator#build_instructions_for_file'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/model_annotator/project_annotator.rb:17:in 'block in AnnotateRb::ModelAnnotator::ProjectAnnotator#annotate'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/model_annotator/project_annotator.rb:13:in 'Array#map'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/model_annotator/project_annotator.rb:13:in 'AnnotateRb::ModelAnnotator::ProjectAnnotator#annotate'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/model_annotator/annotator.rb:21:in 'AnnotateRb::ModelAnnotator::Annotator#do_annotations'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/model_annotator/annotator.rb:8:in 'AnnotateRb::ModelAnnotator::Annotator.do_annotations'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/commands/annotate_models.rb:17:in 'AnnotateRb::Commands::AnnotateModels#call'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/runner.rb:38:in 'AnnotateRb::Runner#run'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/lib/annotate_rb/runner.rb:11:in 'AnnotateRb::Runner.run'
	from /Users/chris/.local/share/mise/installs/ruby/3.4.6/lib/ruby/gems/3.4.0/bundler/gems/annotaterb-dd101d201e94/exe/annotaterb:18:in '<top (required)>'
	from bin/annotate:16:in 'Kernel#load'
	from bin/annotate:16:in '<main>'

Calling .reset_column_information on a tenanted model within the Rails console works fine, but a default tenant is set automatically and I think this issue is likely to do with no tenant being loaded.

chrise86 avatar Oct 04 '25 21:10 chrise86

As a workaround for now I'm doing the following:

# app/overrides/annotate_rb/commands/annotate_models_override.rb

AnnotateRb::Commands::AnnotateModels.class_eval do
  alias original_call call

  def call(options)
    ApplicationRecord.with_tenant(Rails.application.config.active_record_tenanted.default_tenant) do
      original_call(options)
    end
  end
end

chrise86 avatar Oct 05 '25 09:10 chrise86

Hi @chrise86 I don't have familiarity with that gem so it's not something that I would be able to help with. However, if you want to submit a patch/PR I would be happy to help review and get it into the gem.

drwl avatar Oct 20 '25 01:10 drwl