database_cleaner icon indicating copy to clipboard operation
database_cleaner copied to clipboard

YAML aliases with Ruby 3.1

Open jacobat opened this issue 3 years ago • 3 comments

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

jacobat avatar May 25 '22 15:05 jacobat

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.

cesarjr avatar Sep 03 '22 19:09 cesarjr

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

jacobat avatar Sep 05 '22 06:09 jacobat

Thanks, man! 🤜🤛

cesarjr avatar Sep 05 '22 21:09 cesarjr

@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?

etagwerker avatar Jan 26 '23 17:01 etagwerker

@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.

jvortmann avatar Feb 26 '23 22:02 jvortmann

@jvortmann Sounds like a good idea. Thank you!

etagwerker avatar Mar 10 '23 17:03 etagwerker

Thank you!

jacobat avatar Mar 13 '23 09:03 jacobat

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

Ggg21659 avatar Mar 11 '24 20:03 Ggg21659