janko / rodauth-rails

Rails integration for Rodauth authentication framework
https://github.com/jeremyevans/rodauth
MIT License
571 stars 40 forks source link

No ActiveRecord, Still complains about having it... #74

Closed gencer closed 2 years ago

gencer commented 2 years ago

I have an existing project that composed by these:

rails@6.1.4.1
sequel
sequel-rails
rodauth-rails # added this
devise # removed favor of rodauth-rails
devise-sequel # removed favor of rodauth-rails
doorkeeper # removed favor of rodauth-oauth
doorkeeper-sequel # removed favor of rodauth-oauth

So I gone ahead and dumped Devise + Doorkeeper, However, I also have this setup in my application.rb

# require 'rails/all'
# require 'active_model/railtie'
# require 'active_job/railtie'
# require "active_record/railtie"
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
# require "action_cable/engine"
require 'sprockets/railtie'
require 'rails/test_unit/railtie'
require 'rexml/document'

As you can see, I am not using ActiveRecord at all. Instead i'm using sequel-rails to bring Sequel to the Rails...

Problem starts here, I cannot execute rodauth:install. It hangs on ActiveRecord step and says db is unreachable. If I manually dump ActiveRecord via Object.send(:remove_const, :ActiveRecord) - which is not recommended way of doing this - then I get ActiveModel issues.

Basically, either one of;

a. rodauth-rails forcefully requires ActiveRecord b. Does not work with sequel-rails c. Something is wrong with the existing project (still searching, tho.)

Do you have any idea? Should I get rid of sequel-rails and use ActiveRecord as-is? But the whole project is depends on Sequel and has no any ActiveRecord-related content at all.

Some logs:

/home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_handling.rb:323:in `connection_pool': ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activerecord-6.1.3.2/lib/active_record/connection_handling.rb:319:in `connection_db_config'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/migration_helpers.rb:28:in `activerecord_adapter'
        from (erb):1:in `erb_eval'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/3.0.0/erb.rb:905:in `eval'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/3.0.0/erb.rb:905:in `result'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/migration_helpers.rb:61:in `erb_eval'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/migration_helpers.rb:21:in `block in migration_content'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/migration_helpers.rb:21:in `map'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/migration_helpers.rb:21:in `migration_content'
        from (erb):3:in `migration_template'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/3.0.0/erb.rb:905:in `eval'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/3.0.0/erb.rb:905:in `result'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators/migration.rb:67:in `block in migration_template'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/actions/create_file.rb:53:in `render'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators/actions/create_migration.rb:19:in `identical?'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators/actions/create_migration.rb:50:in `on_conflict_behavior'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/actions/empty_directory.rb:115:in `invoke_with_conflict_check'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/actions/create_file.rb:60:in `invoke!'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators/actions/create_migration.rb:23:in `invoke!'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/actions.rb:93:in `action'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators/migration.rb:36:in `create_migration'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators/migration.rb:65:in `migration_template'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/migration_helpers.rb:12:in `migration_template'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/rodauth-rails-0.18.0/lib/generators/rodauth/install_generator.rb:33:in `create_rodauth_migration'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `block in invoke_all'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `each'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `map'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:134:in `invoke_all'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/group.rb:232:in `dispatch'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/base.rb:485:in `start'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/generators.rb:275:in `invoke'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/commands/generate/generate_command.rb:26:in `perform'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/command/base.rb:69:in `perform'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/command.rb:50:in `invoke'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/railties-6.1.3.2/lib/rails/commands.rb:18:in `<top (required)>'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
        from /home/gencer/sources/my-org/my-org/bin/rails:9:in `<top (required)>'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:10:in `block in fork'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:10:in `block in fork'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:8:in `fork'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:8:in `fork'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:26:in `fork'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:8:in `fork'
        from /home/gencer/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/fork_tracker.rb:26:in `fork'
        from <internal:/home/gencer/.rbenv/versions/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from <internal:/home/gencer/.rbenv/versions/3.0.1/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from -e:1:in `<main>'
janko commented 2 years ago

Hi @gencer, thanks for reporting. As a big fan of Sequel, I have every intention of supporting Rails apps that use Sequel instead of Active Record. There are already places that check whether ActiveRecord is defined to make decisions, but honestly I didn't test it with a Sequel-only Rails app, so I might have missed some places. I'll look into this.

janko commented 2 years ago

Hmm, your backtrace shows that the rodauth:install generator attempted to create an Active Record migration, but it should have been skipped when Active Record is not loaded: https://github.com/janko/rodauth-rails/blob/3c97a31fbf8263166075226a089d285f8cc2ed29/lib/generators/rodauth/install_generator.rb#L29-L31

Does defined?(ActiveRecord::Base) return a truthy value in your app?

janko commented 2 years ago

It could be possible that some other gem is loading Active Record as a gem, so it seems safer for rodauth-rails to check whether Active Record is activated by in the Rails app by checking the existence of ActiveRecord::Railtie constant. I'll go ahead and push the change, and we'll re-open the issue if that doesn't fix it.

I would also like to start working on better Sequel integration (migrations, Rodauth::Rails::Model support etc).

gencer commented 2 years ago

@janko You are my hero! I saw your first reply (i was outside) and going to comment here later but you were faster than me 🥳

Tested master branch here. Works perfectly! Thank you. For now I'll stay on master branch but I hope we can get a new release soon 🚀

gencer commented 2 years ago

BTW, Just a question: Is there anything different in rodauth-rails than rodauth itself? I'll going to run Sequel migrations from rodauth. Should this be enough for rodauth-rails? https://github.com/jeremyevans/rodauth#label-Creating+tables

janko commented 2 years ago

For now I'll stay on master branch but I hope we can get a new release soon 🚀

FWIW, this issue only affects the rodauth:install generator, so as soon as you've run it you can switch back to the released version. The next version is going to be 1.0, which could take a while.

I'll going to run Sequel migrations from rodauth. Should this be enough for rodauth-rails?

Default rodauth-rails configuration changes status column values to strings (so that a seprate table is not needed), disables database authentication functions (so that you can just create tables in a single migration), and sets deadline values in Ruby (so that deadline column defaults are not needed).

gencer commented 2 years ago

For now I'll stay on master branch but I hope we can get a new release soon 🚀

FWIW, this issue only affects the rodauth:install generator, so as soon as you've run it you can switch back to the released version. The next version is going to be 1.0, which could take a while.

Done. Thanks for the update!

Default rodauth-rails configuration changes status column values to strings (so that a seprate table is not needed), disables database authentication functions (so that you can just create tables in a single migration), and sets deadline values in Ruby (so that deadline column defaults are not needed).

I'll look what changes made and I'll adapt to it if necessary. 👌🥳