Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninitialized constant ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig #201

Closed
tilo opened this issue Nov 20, 2015 · 13 comments

Comments

@tilo
Copy link

tilo commented Nov 20, 2015

Rails startup crashes when starting daemon through bundle exec

ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig is not defined

Rails 4.2.4
pg (0.18.4)

happens with activerecord-postgis-adapter 3.0.0 and 3.1.0

# bundle exec pushr -f -c config/pushr/pushr.yml

$HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/core.rb:46:in `configurations=': uninitialized constant ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig (NameError)
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/core.rb:48:in `block in <module:Core>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/concern.rb:120:in `class_eval'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/concern.rb:120:in `append_features'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/base.rb:284:in `include'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/base.rb:284:in `<class:Base>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/base.rb:269:in `<module:ActiveRecord>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/base.rb:26:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb:9:in `<class:PostgreSQLDatabaseTasks>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb:5:in `<module:Tasks>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb:4:in `<module:ActiveRecord>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/tasks/postgresql_database_tasks.rb:3:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb:4:in `<module:PostGIS>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb:3:in `<module:ConnectionAdapters>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb:2:in `<module:ActiveRecord>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis/postgis_database_tasks.rb:1:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis_adapter.rb:18:in `require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/active_record/connection_adapters/postgis_adapter.rb:18:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/activerecord-postgis-adapter.rb:1:in `require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/activerecord-postgis-adapter-3.0.0/lib/activerecord-postgis-adapter.rb:1:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in `require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in `each'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler/runtime.rb:72:in `block in require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in `each'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler/runtime.rb:61:in `require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/bundler-1.10.6/lib/bundler.rb:134:in `require'
  from $HOME/.rvm/gems/ruby-2.2.3/gems/pushr-core-1.0.2/bin/pushr:4:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-2.2.3/bin/pushr:23:in `load'
  from $HOME/.rvm/gems/ruby-2.2.3/bin/pushr:23:in `<main>'
  from $HOME/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
  from $HOME/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'

@tilo
Copy link
Author

tilo commented Nov 20, 2015

when I change the database.yml file to just use the postgres adapter -> no crash..

@teeparham
Copy link
Member

What does the pushr code that loads this look like? Is it calling gem or something different?

@tilo
Copy link
Author

tilo commented Nov 22, 2015

this is the complete script for the pushr daemon.. the last line crashes

$ cat `which pushr`
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'pushr-core' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first
  str = ARGV.first
  str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
  if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
    version = $1
    ARGV.shift
  end
end

gem 'pushr-core', version
load Gem.bin_path('pushr-core', 'pushr', version)

@tilo
Copy link
Author

tilo commented Nov 24, 2015

@teeparham any chance to get this fixed soon? This is blocking our project.

@tilo
Copy link
Author

tilo commented Dec 8, 2015

@teeparham do you have a suggestion on how to circumvent this error?

@tilo
Copy link
Author

tilo commented Jan 7, 2016

@teeparham Hi Tee, did you have a chance to look at this?

@mattdlh
Copy link

mattdlh commented Feb 18, 2016

@teeparham Also running into this issue, it looks like a bug with activerecord-postgis-adapter. Any possible resolution in sight here?

@gbaptista
Copy link

The problem:

When we use ActiveRecord with PostgreSQL, the ActiveRecord uses a ConnectionHandling adapted for PostgreSQL:

postgresql_adapter.rb override the original ConnectionHandling from connection_handling.rb

activerecord-postgis-adapter databases.rake call the method ActiveRecord::Base.configurations:

databases.rake:

ActiveRecord::Base.configurations
        .values_at(*environments)...

ActiveRecord::Base.configurations at active_record/core.rb:

def self.configurations=(config)
   @@configurations = ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig.new(config).resolve
end

But, we don't have MergeAndResolveDefaultUrlConfig in postgresql_adapter.rb.

A very ugly "solution":

In your rails application, create the file config/initializers/postgis_hotfix.rb:

module ActiveRecord
  module ConnectionHandling # :nodoc:
    RAILS_ENV   = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"] || ENV["RACK_ENV"] }
    DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }

    class MergeAndResolveDefaultUrlConfig # :nodoc:
      def initialize(raw_configurations)
        @raw_config = raw_configurations.dup
        @env = DEFAULT_ENV.call.to_s
      end

      # Returns fully resolved connection hashes.
      # Merges connection information from `ENV['DATABASE_URL']` if available.
      def resolve
        ConnectionAdapters::ConnectionSpecification::Resolver.new(config).resolve_all
      end

      private
        def config
          @raw_config.dup.tap do |cfg|
            if url = ENV['DATABASE_URL']
              cfg[@env] ||= {}
              cfg[@env]["url"] ||= url
            end
          end
        end
    end
  end
end

And require the initializer in config/application.rb before the Bundler.require:

require 'rails/all'

# Bad, ugly, horrible solution:
require './config/initializers/postgis_hotfix'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

Real solution:

  • Possible 1:

Maybe it's an ActiveRecord issue...

The postgresql_adapter.rb breaks the ActiveRecord::Base.configurations method. That's not ok, right?

  • Possible 2:

If databases.rake file at activerecord-postgis-adapter can work whitout the ActiveRecord::Base.configurations, maybe this can be changed to fix the problem.

@tilo
Copy link
Author

tilo commented Mar 3, 2016

thank you for the pointers, @gbaptista ! Will try it out this week

@tilo
Copy link
Author

tilo commented Mar 24, 2016

this did not work for us - we got an error from another Gem (attr_encrypted) -- looks like a problem in the order of how the railties are executed

/...../.rvm/gems/ruby-2.2.3/gems/attr_encrypted-1.3.4/lib/attr_encrypted.rb:259:in method_missing': undefined local variable or methodestablish_connection' for ActiveRecord::Base:Class (NameError)

@tilo
Copy link
Author

tilo commented Mar 24, 2016

what is a cleaner way to ensure that ActiveRecord::ConnectionHandling is loaded before ./lib/active_record/connection_adapters/postgis/create_connection.rb ?

@adis-io
Copy link

adis-io commented May 6, 2020

Any update on this?
Tried using Bumbler and got this error

@keithdoggett
Copy link
Member

keithdoggett commented Dec 3, 2020

The MergeAndResolveDefaultUrlConfig class was removed in rails 6. If you're still having this issue, I'd try to upgrade your version of rails/active_record.

If that's not possible, pull requests are welcome to fix this issue and can be merged into an older branch, but it is unlikely that this issue will be fixed by any current maintainers. We will try to maintain all currently supported versions of rails, and and while rails 5 is still supported, it seems like this error is occuring only when using gems or executing tasks that change the default order of railties so it is low priority right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants