Doorkeeper: Default migration fails in index 'index_oauth_access_tokens_on_resource_owner_id'

Created on 24 Oct 2018  路  3Comments  路  Source: doorkeeper-gem/doorkeeper

Steps to reproduce

Just installed Doorkeeper gem to work with Devise.

Did rails g doorkeeper:install, then customized the initializer (flow is password as in https://scotch.io/@jiggs/rails-api-doorkeeper-devise).

Then did rails generate doorkeeper:migration

Then rails db:migrate

Expected behavior

Should execute the migration without errors.

Actual behavior

== 20181024144635 CreateDoorkeeperTables: migrating ===========================
-- create_table(:oauth_applications)
   -> 0.0008s
-- add_index(:oauth_applications, :uid, {:unique=>true})
   -> 0.0004s
-- create_table(:oauth_access_grants)
   -> 0.0012s
-- add_index(:oauth_access_grants, :token, {:unique=>true})
   -> 0.0005s
-- add_foreign_key(:oauth_access_grants, :oauth_applications, {:column=>:application_id})
   -> 0.0000s
-- create_table(:oauth_access_tokens)
   -> 0.0011s
-- add_index(:oauth_access_tokens, :token, {:unique=>true})
   -> 0.0006s
-- add_index(:oauth_access_tokens, :resource_owner_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Index name 'index_oauth_access_tokens_on_resource_owner_id' on table 'oauth_access_tokens' already exists
/home/mic/dev/articule/product/api/db/migrate/20181024144635_create_doorkeeper_tables.rb:61:in `change'
/home/mic/dev/articule/product/api/bin/rails:9:in `<top (required)>'
/home/mic/dev/articule/product/api/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'

Caused by:
ArgumentError: Index name 'index_oauth_access_tokens_on_resource_owner_id' on table 'oauth_access_tokens' already exists
/home/mic/dev/articule/product/api/db/migrate/20181024144635_create_doorkeeper_tables.rb:61:in `change'
/home/mic/dev/articule/product/api/bin/rails:9:in `<top (required)>'
/home/mic/dev/articule/product/api/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

System configuration

Rails 5.2.1.
doorkeeper 5.0.1

Doorkeeper initializer:

# frozen_string_literal: true

Doorkeeper.configure do
  # Change the ORM that doorkeeper will use (needs plugins)
  orm :active_record

  # This block will be called to check whether the resource owner is authenticated or not.
  resource_owner_authenticator do
    # raise "Please configure doorkeeper resource_owner_authenticator block located in #{__FILE__}"
  end

  # Mic: Taken from https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Resource-Owner-Password-Credentials-flow
  resource_owner_from_credentials do |_routes|
    user = User.find_for_database_authentication(email: params[:username])
    if user&.valid_for_authentication? \
        { user.valid_password?(params[:password]) }
      user
    end
  end

  api_only
  access_token_expires_in 2.hours
  base_controller 'ApplicationController'
  force_ssl_in_redirect_uri !Rails.env.development?
  grant_flows %w[password]

  skip_authorization do |resource_owner, client|
    true
  end

Ruby version:
2.5.1
Gemfile.lock:

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.2.1)
      actionpack (= 5.2.1)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailer (5.2.1)
      actionpack (= 5.2.1)
      actionview (= 5.2.1)
      activejob (= 5.2.1)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (5.2.1)
      actionview (= 5.2.1)
      activesupport (= 5.2.1)
      rack (~> 2.0)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (5.2.1)
      activesupport (= 5.2.1)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.3)
    activejob (5.2.1)
      activesupport (= 5.2.1)
      globalid (>= 0.3.6)
    activemodel (5.2.1)
      activesupport (= 5.2.1)
    activerecord (5.2.1)
      activemodel (= 5.2.1)
      activesupport (= 5.2.1)
      arel (>= 9.0)
    activestorage (5.2.1)
      actionpack (= 5.2.1)
      activerecord (= 5.2.1)
      marcel (~> 0.3.1)
    activesupport (5.2.1)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    arel (9.0.0)
    bcrypt (3.1.12)
    bindex (0.5.0)
    bootsnap (1.3.2)
      msgpack (~> 1.0)
    builder (3.2.3)
    concurrent-ruby (1.0.5)
    crass (1.0.4)
    database_cleaner (1.7.0)
    devise (4.5.0)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0, < 6.0)
      responders
      warden (~> 1.2.3)
    diff-lcs (1.3)
    doorkeeper (5.0.1)
      railties (>= 4.2)
    erubi (1.7.1)
    factory_bot (4.11.1)
      activesupport (>= 3.0.0)
    factory_bot_rails (4.11.1)
      factory_bot (~> 4.11.1)
      railties (>= 3.0.0)
    faker (1.9.1)
      i18n (>= 0.7)
    ffi (1.9.25)
    globalid (0.4.1)
      activesupport (>= 4.2.0)
    i18n (1.1.1)
      concurrent-ruby (~> 1.0)
    listen (3.1.5)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
      ruby_dep (~> 1.2)
    loofah (2.2.2)
      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.0)
    mimemagic (0.3.2)
    mini_mime (1.0.1)
    mini_portile2 (2.3.0)
    minitest (5.11.3)
    msgpack (1.2.4)
    nio4r (2.3.1)
    nokogiri (1.8.5)
      mini_portile2 (~> 2.3.0)
    orm_adapter (0.5.0)
    pg (1.1.3)
    puma (3.12.0)
    rack (2.0.5)
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (5.2.1)
      actioncable (= 5.2.1)
      actionmailer (= 5.2.1)
      actionpack (= 5.2.1)
      actionview (= 5.2.1)
      activejob (= 5.2.1)
      activemodel (= 5.2.1)
      activerecord (= 5.2.1)
      activestorage (= 5.2.1)
      activesupport (= 5.2.1)
      bundler (>= 1.3.0)
      railties (= 5.2.1)
      sprockets-rails (>= 2.0.0)
    rails-controller-testing (1.0.2)
      actionpack (~> 5.x, >= 5.0.1)
      actionview (~> 5.x, >= 5.0.1)
      activesupport (~> 5.x)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.0.4)
      loofah (~> 2.2, >= 2.2.2)
    railties (5.2.1)
      actionpack (= 5.2.1)
      activesupport (= 5.2.1)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.19.0, < 2.0)
    rake (12.3.1)
    rb-fsevent (0.10.3)
    rb-inotify (0.9.10)
      ffi (>= 0.5.0, < 2)
    responders (2.4.0)
      actionpack (>= 4.2.0, < 5.3)
      railties (>= 4.2.0, < 5.3)
    rspec-core (3.8.0)
      rspec-support (~> 3.8.0)
    rspec-expectations (3.8.2)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.8.0)
    rspec-mocks (3.8.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.8.0)
    rspec-rails (3.8.0)
      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.0)
    ruby_dep (1.5.0)
    shoulda-matchers (3.1.2)
      activesupport (>= 4.0.0)
    spring (2.0.2)
      activesupport (>= 4.2)
    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)
    sqlite3 (1.3.13)
    thor (0.20.0)
    thread_safe (0.3.6)
    tzinfo (1.2.5)
      thread_safe (~> 0.1)
    warden (1.2.7)
      rack (>= 1.0)
    web-console (3.7.0)
      actionview (>= 5.0)
      activemodel (>= 5.0)
      bindex (>= 0.4.0)
      railties (>= 5.0)
    websocket-driver (0.7.0)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.3)

PLATFORMS
  ruby

DEPENDENCIES
  bootsnap (>= 1.1.0)
  database_cleaner
  devise (~> 4.5.0)
  doorkeeper (~> 5.0.1)
  factory_bot_rails
  faker
  listen (>= 3.0.5, < 3.2)
  pg
  puma (~> 3.11)
  rails (~> 5.2.1)
  rails-controller-testing
  rspec-rails
  shoulda-matchers
  spring
  spring-watcher-listen (~> 2.0.0)
  sqlite3
  tzinfo-data
  web-console (>= 3.3.0)

RUBY VERSION
   ruby 2.5.1p57

BUNDLED WITH
   1.16.6
bug

Most helpful comment

Hi @MicMicMon . Thanks for creating this issue! It is a problem of forgotten line in migration template that must be removed. I'll release a fix tomorrow, you can just drop the line with add_index :oauth_access_tokens, :resource_owner_id from migration template

All 3 comments

Hi @MicMicMon . Thanks for creating this issue! It is a problem of forgotten line in migration template that must be removed. I'll release a fix tomorrow, you can just drop the line with add_index :oauth_access_tokens, :resource_owner_id from migration template

Awesome, thanks for the quick reply & fix!

Hi @MicMicMon . Thanks for creating this issue! It is a problem of forgotten line in migration template that must be removed. I'll release a fix tomorrow, you can just drop the line with add_index :oauth_access_tokens, :resource_owner_id from migration template

Tested on Rails 5.1. This fix is also work on Rails 5.1. Thank you for the prompt response!

Was this page helpful?
0 / 5 - 0 ratings