ORM refactors

Working together with @jasl (see source PR in https://github.com/doorkeeper-gem/doorkeeper/pull/478).