Skip to content

Doorkeeper is loading ActiveRecord too early #1703

Open
@ngan

Description

@ngan

Steps to reproduce

We found a weird behavior as we were upgrading Rails (7.0 => 7.1). When upgrading Rails, it generates a file called config/initializers/new_framework_defaults.rb where it sets a bunch of Rails.application.config.x.y = :something in preparation for changing Rails defaults to the latest version.

We noticed that some of these configs, especially the ones in config.active_record.X did not take hold. Meaning, they did not change the behavior of ActiveRecord. We dug into this further and found out that this happens when gem loads ActiveRecord too early. Here's a Rails issue describing the problem: rails/rails#50133. There's even work (in Rails) towards warning when this happens: rails/rails#46047

For Doorkeeper, specifically, we traced it down to here:

def access_token_model
@access_token_model ||= access_token_class.constantize
end

Calling access_token_model will constantize the configured class and essentially loads an autoloaded model that lives inside app/models/.. within the application. The problem is that this method is invoked very early in the setup phase of the doorkeeper gem:

=> https://github.com/doorkeeper-gem/doorkeeper/blob/main/lib/doorkeeper/orm/active_record.rb#L41
from https://github.com/doorkeeper-gem/doorkeeper/blob/main/lib/doorkeeper.rb#L160-L172
from https://github.com/doorkeeper-gem/doorkeeper/blob/main/lib/doorkeeper/engine.rb#L23-L25

I've created a repo to help illustrate the problem. The README has the information.
https://github.com/ngan/doorkeeper-activerecord-load-issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions