trilogy-libraries / activerecord-trilogy-adapter

Active Record adapter for the Trilogy database client for Rails v6.0 - v7.0.
https://github.com/trilogy-libraries/trilogy
MIT License
172 stars 17 forks source link

Trilogy adapter not working with `DATABASE_URL` config in GitHub actions #64

Open colinframe opened 1 year ago

colinframe commented 1 year ago

Hi there,

I'm in the process of moving a Rails 7 app over to Trilogy and whilst locally everything Just Worked using database.yml or DATABASE_URL as a config I'm getting an error when using the DATABASE_URL env variable in Github actions for CI. Previously I had the mysql2 adapter working fine on GA using DATABASE_URL.

The error I'm seeing is Trilogy::QueryError: trilogy_auth_recv: TRILOGY_UNEXPECTED_PACKET.

I'm not using any specific config options on the CI mysql service but locally I am starting up the docker compose service with the following, in case that makes a difference command: [ "--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci", "--innodb_monitor_enable=all" ]

I'm using the latest versions of trilogy, active record-trilogy-adapter and rails.

    rails (7.0.8)
      actioncable (= 7.0.8)
      actionmailbox (= 7.0.8)
      actionmailer (= 7.0.8)
      actionpack (= 7.0.8)
      actiontext (= 7.0.8)
      actionview (= 7.0.8)
      activejob (= 7.0.8)
      activemodel (= 7.0.8)
      activerecord (= 7.0.8)
      activestorage (= 7.0.8)
      activesupport (= 7.0.8)
      bundler (>= 1.15.0)
      railties (= 7.0.8)
    activerecord-trilogy-adapter (3.1.2)
      activerecord (>= 6.0.a, < 7.1.a)
      trilogy (>= 2.4.0)
    trilogy (2.5.0)

The output I get from the GA CI run is as follows;

bin/rails db:schema:load
  shell: /usr/bin/bash -e {0}
  env:
    RAILS_ENV: test
    MYSQL_ROOT_PASSWORD: ****
    MYSQL_DATABASE: ****
    DATABASE_URL: ***127.0.0.1:3306/****
rails aborted!
Trilogy::QueryError: trilogy_auth_recv: TRILOGY_UNEXPECTED_PACKET
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/trilogy-2.5.0/lib/trilogy.rb:16:in `_initialize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/trilogy-2.5.0/lib/trilogy.rb:16:in `initialize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:82:in `new'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:82:in `new_client'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:300:in `connect'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:205:in `block in reconnect!'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:203:in `reconnect!'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:579:in `verify!'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:211:in `block in with_trilogy_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:210:in `with_trilogy_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-trilogy-adapter-3.1.2/lib/active_record/connection_adapters/trilogy_adapter.rb:330:in `get_full_version'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:59:in `get_database_version'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/schema_cache.rb:145:in `database_version'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:646:in `database_version'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:554:in `check_version'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:657:in `block in new_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `new_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:700:in `checkout_new_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:679:in `try_to_checkout_new_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:640:in `acquire_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:341:in `checkout'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_handler.rb:211:in `retrieve_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_handling.rb:313:in `retrieve_connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_handling.rb:280:in `connection'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/tasks/database_tasks.rb:65:in `check_protected_environments!'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/railties/databases.rake:18:in `block (2 levels) in <main>'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/command.rb:51:in `invoke'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails/commands.rb:18:in `<main>'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
/home/runner/work/dailyintention/dailyintention/vendor/bundle/ruby/3.2.0/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'

Any help would be greatly appreciated!

gstark commented 1 year ago

I speculate the problem is related to an issue in the trilogy gem where it cannot support caching_sha2_password - see this issue

I had to make these changes to use the adapter on GitHub CI. Essentially you need to ensure the user is IDENTIFIED WITH mysql_native_password

         run: |
           sudo service mysql start
           mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
+          mysql -e "CREATE USER 'test'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test';" -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
+          mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' WITH GRANT OPTION;" -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
+          mysql -e "FLUSH PRIVILEGES;" -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
jon-sully commented 1 year ago

@gstark thanks for the snippet! You're creating a new user (test@localhost) by using the authority of the default/root user (your DB_USER + DB_PASSWORD) — are you telling Trilogy somewhere to use this new user (test@localhost) rather than the DB_USER / DB_PASSWORD env vars?

gstark commented 1 year ago

Yes, in my setup, I am manually setting DATABASE_URL to use this new test user. I think you could modify the existing {{ env.DB_USER }} to use mysql_native_password - though I have not checked to see how to change that.

jon-sully commented 1 year ago

I didn't end up getting this working (had to move on to other things) but hope I can come back at some point and get it going!