Skip to content

Handle apps without ActiveRecord #263

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

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Feb 20, 2024

Fixes #262

I tried to add a test which undefines ActiveRecord:

      test "#model returns nil if app doesn't use ActiveRecord" do
        Temporary = ActiveRecord
        Object.send(:remove_const, :ActiveRecord)
        assert_nil(@client.model("User"))
      ensure
        ActiveRecord = Temporary
      end

but it fails because of the ActiveSupport internals:

/Users/andyw8/.gem/ruby/3.2.1/gems/activerecord-7.1.3/lib/active_record/query_cache.rb:35:in `complete': uninitialized constant ActiveRecord::QueryCache::ActiveRecord (NameError)

      ActiveRecord::Base.connection_handler.each_connection_pool do |pool|
                  ^^^^^^
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/execution_wrapper.rb:37:in `before'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:426:in `block in make_lambda'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:202:in `block (2 levels) in halting'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:707:in `block (2 levels) in default_terminator'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:706:in `catch'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:706:in `block in default_terminator'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:203:in `block in halting'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:598:in `block in invoke_before'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:598:in `each'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:598:in `invoke_before'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/callbacks.rb:109:in `run_callbacks'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/execution_wrapper.rb:143:in `complete'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/execution_wrapper.rb:107:in `perform'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/activesupport-7.1.3/lib/active_support/executor/test_helper.rb:5:in `run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:1126:in `run_one_method'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:378:in `run_one_method'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:365:in `block (2 levels) in run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:364:in `each'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:364:in `block in run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:412:in `on_signal'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:399:in `with_info_handler'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:363:in `run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/railties-7.1.3/lib/rails/test_unit/line_filtering.rb:10:in `run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:185:in `block in __run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:185:in `map'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:185:in `__run'
        from /Users/andyw8/.gem/ruby/3.2.1/gems/minitest-5.21.2/lib/minitest.rb:162:in `run'

@andyw8 andyw8 requested a review from a team as a code owner February 20, 2024 16:25
@andyw8 andyw8 requested review from Morriar and vinistock February 20, 2024 16:25
@andyw8 andyw8 added the bugfix This PR fixes an existing bug label Feb 20, 2024
@@ -29,7 +29,7 @@ class Server
sig { params(model_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
def resolve_database_info_from_model(model_name)
const = ActiveSupport::Inflector.safe_constantize(model_name)
unless const && const < ActiveRecord::Base && !const.abstract_class?
unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class?
Copy link
Member

Choose a reason for hiding this comment

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

We might be able to support other backends (like Mongoid) if we shift this to check for features rather than inheritance. Would something like this work?

Suggested change
unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class?
unless const && const.respond_to?(:columns) && !const.abstract_class?

Copy link
Contributor Author

@andyw8 andyw8 Feb 20, 2024

Choose a reason for hiding this comment

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

It looks like we can't assume the existence of those methods:

irb(main):003* class Post
irb(main):004*   include Mongoid::Document
irb(main):005*   field :title, type: String
irb(main):006*   field :body, type: String
irb(main):007*   has_many :comments
irb(main):008> end
=> 
#<Mongoid::Association::Referenced::HasMany:0x0000000116e39c18
 @extension=nil,
 @module_path="",
 @name=:comments,
 @options={},
 @owner_class=Post,
 @polymorphic=false,
 @validate=true>
irb(main):009> Post.columns
(irb):9:in `<main>': undefined method `columns' for Post:Class (NoMethodError)

Post.columns
    ^^^^^^^^
irb(main):010> Post.abstract_class?
(irb):10:in `<main>': undefined method `abstract_class?' for Post:Class (NoMethodError)

Post.abstract_class?
    ^^^^^^^^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The closest match would be fields:

irb(main):011> Post.fields
=> 
{"_id"=>
  #<Mongoid::Fields::Standard:0x000000011783f820
   @default_name="___id_default__",
   @default_val=#<Proc:0x0000000116fbe160 /Users/andyw8/.gem/ruby/3.2.1/gems/mongoid-8.1.4/lib/mongoid/fields.rb:137 (lambda)>,
   @label=nil,
   @name="_id",
   @options=
    {:default=>#<Proc:0x0000000116fbe160 /Users/andyw8/.gem/ruby/3.2.1/gems/mongoid-8.1.4/lib/mongoid/fields.rb:137 (lambda)>,
     :pre_processed=>true,
     :type=>BSON::ObjectId,
     :klass=>Post},
   @pre_processed=true>,
 "title"=>#<Mongoid::Fields::Standard:0x0000000117838fc0 @default_val=nil, @label=nil, @name="title", @options={:type=>String, :klass=>Post}>,
 "body"=>#<Mongoid::Fields::Standard:0x00000001178387a0 @default_val=nil, @label=nil, @name="body", @options={:type=>String, :klass=>Post}>}

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's too bad. I thought there'd be a common interface for these. Thanks for looking into it.

Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

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

Let's move forward with the fix and consider support for other ORMs later

@andyw8 andyw8 merged commit 3df6a98 into main Feb 20, 2024
@andyw8 andyw8 deleted the andyw8/handle-apps-without-activerecord branch February 20, 2024 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR fixes an existing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ruby LSP Rails error: uninitialized constant RubyLsp::Rails::Server::ActiveRecord
2 participants