Doorkeeper: NameError: uninitialized constant Doorkeeper::AccessToken on rake doorkeeper:db:cleanup

Created on 17 Oct 2019  路  7Comments  路  Source: doorkeeper-gem/doorkeeper

Steps to reproduce

What we need to do to see your problem or bug?
On RakeFile:

# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative 'config/application'

Rails.application.load_tasks
Doorkeeper::Rake.load_tasks

Doorkeeper installed and configured with Rails 5.2.3

### Actual behavior
rake doorkeeper:db:cleanup fails with message
rake aborted!
NameError: uninitialized constant Doorkeeper::AccessToken
/usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `block in load_missing_constant'
/usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
/usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `rescue in load_missing_constant'
/usr/local/bundle/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in `load_missing_constant'
/usr/local/bundle/gems/doorkeeper-5.1.0/lib/doorkeeper/rake/db.rake:16:in `block (4 levels) in <main>'
/usr/local/bundle/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'

System configuration

Ruby 2.6.3 with mysql and ActiveRecord

Doorkeeper initializer:
Doorkeeper is working fine in endpoints and even on rails console. But rake cleanup task is not working.

Ruby version: ``
2.6.3

Gemfile.lock:


Gemfile.lock content

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.2.3)
      actionpack (= 5.2.3)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailer (5.2.3)
      actionpack (= 5.2.3)
      actionview (= 5.2.3)
      activejob (= 5.2.3)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (5.2.3)
      actionview (= 5.2.3)
      activesupport (= 5.2.3)
      rack (~> 2.0)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (5.2.3)
      activesupport (= 5.2.3)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.3)
    active_model_serializers (0.10.10)
      actionpack (>= 4.1, < 6.1)
      activemodel (>= 4.1, < 6.1)
      case_transform (>= 0.2)
      jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
    activejob (5.2.3)
      activesupport (= 5.2.3)
      globalid (>= 0.3.6)
    activemodel (5.2.3)
      activesupport (= 5.2.3)
    activerecord (5.2.3)
      activemodel (= 5.2.3)
      activesupport (= 5.2.3)
      arel (>= 9.0)
    activestorage (5.2.3)
      actionpack (= 5.2.3)
      activerecord (= 5.2.3)
      marcel (~> 0.3.1)
    activesupport (5.2.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    ansi (1.5.0)
    arel (9.0.0)
    asciidoctor (2.0.10)
    ast (2.4.0)
    axiom-types (0.1.1)
      descendants_tracker (~> 0.0.4)
      ice_nine (~> 0.11.0)
      thread_safe (~> 0.3, >= 0.3.1)
    bcrypt (3.1.13)
    bootsnap (1.4.5)
      msgpack (~> 1.0)
    builder (3.2.3)
    byebug (11.0.1)
    case_transform (0.2)
      activesupport
    childprocess (3.0.0)
    chronic (0.10.2)
    coderay (1.1.2)
    coercible (1.0.0)
      descendants_tracker (~> 0.0.1)
    concurrent-ruby (1.1.5)
    connection_pool (2.2.2)
    countries (3.0.0)
      i18n_data (~> 0.8.0)
      sixarm_ruby_unaccent (~> 1.1)
      unicode_utils (~> 1.4)
    crass (1.0.4)
    database_cleaner (1.7.0)
    descendants_tracker (0.0.4)
      thread_safe (~> 0.3, >= 0.3.1)
    devise (4.7.1)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0)
      responders
      warden (~> 1.2.3)
    diff-lcs (1.3)
    docile (1.3.2)
    domain_name (0.5.20190701)
      unf (>= 0.0.5, < 1.0.0)
    doorkeeper (5.2.1)
      railties (>= 5)
    dotenv (2.7.5)
    dotenv-rails (2.7.5)
      dotenv (= 2.7.5)
      railties (>= 3.2, < 6.1)
    equalizer (0.0.11)
    erubi (1.9.0)
    factory_bot (5.1.1)
      activesupport (>= 4.2.0)
    factory_bot_rails (5.1.1)
      factory_bot (~> 5.1.0)
      railties (>= 4.2.0)
    faker (2.5.0)
      i18n (~> 1.6.0)
    faraday (0.16.2)
      multipart-post (>= 1.2, < 3)
    fastimage (2.1.7)
    ffi (1.11.1)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    graphiql-rails (1.7.0)
      railties
      sprockets-rails
    graphql (1.9.12)
    http-accept (1.7.0)
    http-cookie (1.0.3)
      domain_name (~> 0.5)
    i18n (1.6.0)
      concurrent-ruby (~> 1.0)
    i18n_data (0.8.0)
    ice_nine (0.11.2)
    iniparse (1.4.4)
    iso-639 (0.2.8)
    iso8601 (0.9.1)
    jaro_winkler (1.5.3)
    jmespath (1.4.0)
    json (2.2.0)
    json-schema (2.8.1)
      addressable (>= 2.4)
    jsonapi-renderer (0.2.2)
    listen (3.2.0)
      rb-fsevent (~> 0.10, >= 0.10.3)
      rb-inotify (~> 0.9, >= 0.9.10)
    loofah (2.3.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (0.9.2)
    mime-types (3.3)
      mime-types-data (~> 3.2015)
    mime-types-data (3.2019.0904)
    mimemagic (0.3.3)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    minitest (5.12.2)
    monetize (1.9.2)
      money (~> 6.12)
    money (6.13.4)
      i18n (>= 0.6.4, <= 2)
    money-rails (1.13.2)
      activesupport (>= 3.0)
      monetize (~> 1.9.0)
      money (~> 6.13.2)
      railties (>= 3.0)
    msgpack (1.3.1)
    multipart-post (2.1.1)
    mysql2 (0.5.2)
    net_http_timeout_errors (0.3.5)
    netrc (0.11.0)
    nio4r (2.5.2)
    nokogiri (1.10.4)
      mini_portile2 (~> 2.4.0)
    oga (2.15)
      ast
      ruby-ll (~> 2.1)
    orm_adapter (0.5.0)
    overcommit (0.51.0)
      childprocess (>= 0.6.3, < 4)
      iniparse (~> 1.4)
    parallel (1.17.0)
    parser (2.6.5.0)
      ast (~> 2.4.0)
    pry (0.12.2)
      coderay (~> 1.1.0)
      method_source (~> 0.9.0)
    pry-rails (0.3.9)
      pry (>= 0.10.4)
    public_suffix (4.0.1)
    puma (3.12.1)
    pundit (2.1.0)
      activesupport (>= 3.0.0)
    rack (2.0.7)
    rack-attack (6.1.0)
      rack (>= 1.0, < 3)
    rack-cors (1.0.3)
    rack-protection (2.0.7)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (5.2.3)
      actioncable (= 5.2.3)
      actionmailer (= 5.2.3)
      actionpack (= 5.2.3)
      actionview (= 5.2.3)
      activejob (= 5.2.3)
      activemodel (= 5.2.3)
      activerecord (= 5.2.3)
      activestorage (= 5.2.3)
      activesupport (= 5.2.3)
      bundler (>= 1.3.0)
      railties (= 5.2.3)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.2.0)
      loofah (~> 2.2, >= 2.2.2)
    railties (5.2.3)
      actionpack (= 5.2.3)
      activesupport (= 5.2.3)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.19.0, < 2.0)
    rainbow (3.0.0)
    rake (13.0.0)
    rake-progressbar (0.0.5)
    rb-fsevent (0.10.3)
    rb-inotify (0.10.0)
      ffi (~> 1.0)
    redis (4.1.3)
    redis-objects (1.5.0)
      redis (~> 4.0)
    responders (3.0.0)
      actionpack (>= 5.0)
      railties (>= 5.0)
    rest-client (2.1.0)
      http-accept (>= 1.7.0, < 2.0)
      http-cookie (>= 1.0.2, < 2.0)
      mime-types (>= 1.16, < 4.0)
      netrc (~> 0.8)
    rspec-core (3.8.2)
      rspec-support (~> 3.8.0)
    rspec-expectations (3.8.5)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.8.0)
    rspec-mocks (3.8.2)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.8.0)
    rspec-rails (3.8.2)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 3.8.0)
      rspec-expectations (~> 3.8.0)
      rspec-mocks (~> 3.8.0)
      rspec-support (~> 3.8.0)
    rspec-support (3.8.3)
    rubocop (0.75.0)
      jaro_winkler (~> 1.5.1)
      parallel (~> 1.10)
      parser (>= 2.6)
      rainbow (>= 2.2.2, < 4.0)
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 1.4.0, < 1.7)
    rubocop-faker (0.2.0)
      rubocop (>= 0.74)
    rubocop-rails (2.3.2)
      rack (>= 1.1)
      rubocop (>= 0.72.0)
    ruby-ll (2.1.2)
      ansi
      ast
    ruby-progressbar (1.10.1)
    ruby-thumbor (3.0.0)
    rubyzip (2.0.0)
    sentry-raven (2.11.3)
      faraday (>= 0.7.6, < 1.0)
    sidekiq (6.0.1)
      connection_pool (>= 2.2.2)
      rack (>= 2.0.0)
      rack-protection (>= 2.0.0)
      redis (>= 4.1.0)
    simplecov (0.17.1)
      docile (~> 1.1)
      json (>= 1.8, < 3)
      simplecov-html (~> 0.10.0)
    simplecov-html (0.10.2)
    sixarm_ruby_unaccent (1.2.0)
    spring (2.1.0)
    spring-watcher-listen (2.0.1)
      listen (>= 2.7, < 4.0)
      spring (>= 1.2, < 3.0)
    sprockets (3.7.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    thor (0.20.3)
    thread_safe (0.3.6)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    unf (0.1.4)
      unf_ext
    unf_ext (0.0.7.6)
    unicode-display_width (1.6.0)
    unicode_utils (1.4.0)
    valid_email2 (3.0.5)
      activemodel (>= 3.2)
      mail (~> 2.5)
    video_info (2.7)
      iso8601 (~> 0.9.1)
      net_http_timeout_errors (~> 0.3.0)
      oga (~> 2.7)
    virtus (1.0.5)
      axiom-types (~> 0.1)
      coercible (~> 1.0)
      descendants_tracker (~> 0.0, >= 0.0.3)
      equalizer (~> 0.0, >= 0.0.9)
    warden (1.2.8)
      rack (>= 2.0.6)
    websocket-driver (0.7.1)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.4)
    whenever (1.0.0)
      chronic (>= 0.6.3)

PLATFORMS
  ruby

DEPENDENCIES
  active_model_serializers (~> 0.10.0)
  addressable
  bootsnap
  byebug
  countries
  database_cleaner
  devise
  doorkeeper
  dotenv-rails
  factory_bot_rails
  faker
  fastimage
  graphiql-rails
  graphql
  iso-639
  json-schema
  listen (~> 3.0)
  mimemagic
  money-rails (~> 1)
  mysql2
  overcommit
  pry-rails
  puma (~> 3.0)
  pundit
  rack-attack
  rack-cors
  rails (~> 5.2.3)
  rake-progressbar
  rapidocum!
  redis-objects
  rest-client
  rspec-rails
  rubocop
  rubocop-faker
  rubocop-rails
  ruby-thumbor
  rubyzip
  sentry-raven
  sidekiq
  simplecov
  spring
  spring-watcher-listen (~> 2.0)
  valid_email2
  video_info
  virtus
  whenever

BUNDLED WITH
   1.17.3

bug?

All 7 comments

Hi @j-arnaiz . Thanks for reporting it!

It seems to be related to Rails auto/lazy-loading of AR models, which is (possibly) is not loaded yet on time when Rake task run. Try to add something like ActiveRecord::Base.logger on top of Doorkeeper::Rake.load_tasks in your Rakefile and see if it works (it must directly call AR and invoke it's hooks)

Hi @j-arnaiz . Any progress here? Did you try my suggestion above?

Got the same issue when trying to modify Doorkeeper::AccessToken in the initializer:

config/initializers/doorkeeper.rb

# ...
Doorkeeper::AccessToken.belongs_to :resource_owner, class_name: 'User'

Adding ActiveRecord::Base.logger works. Not sure about the implications of this or whether it would be possible to have Doorkeeper classes loaded at least during initialization. Seems like this will cause a lot of annoying issues.

@thisismydesign the problem is that ActiveRecord isn't loaded while running initializers, but you're trying to patch model that isn't loaded yet (Doorkeeper do a lazy load of models to respect custom AR options). Try to wrap your patch in a AR hook:

ActiveSupport.on_load(:active_record) do
  Doorkeeper::AccessToken.belongs_to :resource_owner, class_name: 'User'
end

More info: https://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html

Thanks. From my side, this isn't an issue with doorkeeper then.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

So finally I found the issue! It's already loaded AR in time when rake tasks are loading. Fixed and would be released with next version of the gem

Was this page helpful?
0 / 5 - 0 ratings