DatabaseCleaner / database_cleaner

Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing.
https://www.rubydoc.info/github/DatabaseCleaner/database_cleaner
MIT License
2.94k stars 484 forks source link

YAML aliases with Ruby 3.1 #690

Closed jacobat closed 1 year ago

jacobat commented 2 years ago

When trying to use DatabaseCleaner 2.0.0 with Ruby 3.1 and a database.yml file containing aliases I get this error:

Psych::BadAlias:
  Unknown alias: default

lib/database_cleaner/active_record/base.rb:49:in `load_config'

The YAML loading in Ruby 3.1 no longer supports aliases in YAML by default.

Further information and examples: https://bugs.ruby-lang.org/issues/17866 https://github.com/rails/rails/commit/179d0a1f474ada02e0030ac3bd062fc653765dbe https://github.com/mperham/sidekiq/pull/5141

cesarjr commented 2 years ago

Same problem over here. It "works" if I remove database cleaner.

@jacobat, did you find some solution? The only way I found was to remove alias at database.yml and duplicate all the config.

jacobat commented 2 years ago

I added a monkey patch in an initializer:

if defined?(DatabaseCleaner)
  module DatabaseCleaner
    module ActiveRecord
      class Base
        private

        def load_config
          if db != :default && db.is_a?(Symbol) && File.file?(DatabaseCleaner::ActiveRecord.config_file_location)
            connection_details = YAML::load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result, aliases: true)
            @connection_hash   = valid_config(connection_details, db.to_s)
          end
        end
      end
    end
  end
end
cesarjr commented 2 years ago

Thanks, man! 🤜🤛

etagwerker commented 1 year ago

@jacobat Thanks for submitting this issue! Would you be interested in submitting a PR so that people no longer have to use that monkey patch?

jvortmann commented 1 year ago

@etagwerker I believe this is fixed on active_record adapter on https://github.com/DatabaseCleaner/database_cleaner-active_record/blob/v2.1.0/lib/database_cleaner/active_record/base.rb#L57 but we need a new version of database-cleaner released with a loosen restriction on https://github.com/DatabaseCleaner/database_cleaner/blob/main/database_cleaner.gemspec#L19 otherwise we can't update the active-record adapter with the fix with an bundle update database_cleaner-active_record.

This is the fix: https://github.com/DatabaseCleaner/database_cleaner-active_record/commit/b750ac73ee38dc5706dcb9a97a0061e18ab5fe06

I suggest using just ~> 2 there to easy future updates to get minor and patch fixes.

etagwerker commented 1 year ago

@jvortmann Sounds like a good idea. Thank you!

jacobat commented 1 year ago

Thank you!

Ggg21659 commented 8 months ago

https://github.com/ruby-geekery/nanoc/tree/bring-back-yaml-aliases