Rails database configuration does not specify adapter. What do I do?

16,317

Solution 1

I solved a similar problem in a somewhat intricate project. Not sure it's directly related, but I'm posting this as the way of debugging the problem may be helpful.

In my case, I had the following scenario:

  • The situation only occurred when RAILS_ENV=production. When I did RAILS_ENV=development it worked. Weirdly enough, when I changed the production entry in database.yml to production2 and ran the command with RAILS_ENV=production2, it worked.
  • In the project I was connecting to multiple database connections through various models and libraries.

Here is what I did to detect the issue:

vim /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb

(or wherever the backtrace is telling you the issue is).

Then, I found the place in the code that has these lines:

def resolve_hash_connection(spec) # :nodoc:
  spec = spec.symbolize_keys

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

And changed it to the following:

def resolve_hash_connection(spec) # :nodoc:
  spec = spec.symbolize_keys

  # Debug printing
  puts "*" * 80, spec.inspect, "*" * 80

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

Then I reran the command, in my case bundle exec rails c production.

By doing this I realized that Rails wasn't looking for the production entry as I was thinking it was. It was looking for a different entry called abc_production, which was required in my project due to the multiple database connections I was referring to earlier. On that particular server someone forgot to add that abc_production entry to database.yml. Adding the entry solved the issue.

I believe it happened only when RAILS_ENV=production because in environments/production.rb I have config.eager_load = true, which means Rails will eager-load the application and classes to memory, and probably attempt to establish all database connections defined in those classes (one of them being abc_production).

Hope this helps someone in a similar situation... If you're not using multiple connections, try and debug the problem by changing connection_specification.rb and see if it gives you any lead..

Solution 2

i don't now exactly what you tried to do but.

currently i got the same error as i tried to run < rails c -e production > with ruby-2.10.

`resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

as i run < rails c production > everything works.

maybe this helps someone

Solution 3

I had a similar issue with staging environment and I did:

  1. added devise secret to devise.rb initializer file
  2. configured staging 'secret_key_base' in secrets.yml

It worked fine me there after.

Share:
16,317
NW0428
Author by

NW0428

Updated on June 30, 2022

Comments

  • NW0428
    NW0428 almost 2 years

    The following is the error message:

    /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:47:in `resolve_hash_connection'
        /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:41:in `resolve_string_connection'
        ...
        /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
        /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
        /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in `eval'
        /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in `<main>'
    

    My database.yml file:

    development:
      adapter: sqlite3
      database: db/development.sqlite3
      pool: 5
      timeout: 5000
    
    test:
      adapter: sqlite3
      database: db/test.sqlite3
      pool: 5
      timeout: 5000
    
    production:
      adapter: sqlite3
      database: db/production.sqlite3
      pool: 5
      timeout: 5000
    

    My Gemfile:

    source 'https://rubygems.org'
    
    gem "therubyracer"
    gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
    gem "twitter-bootstrap-rails"
    gem 'jquery-rails'
    gem 'devise'
    
    gem 'rails', '3.2.13'
    
    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'
    
    group :development, :test do
        gem 'sqlite3'
    end
    
    group :production do
        gem 'pg'
    end
    
    gem 'mini_magick'
    gem "rmagick"
    gem "carrierwave"
    
    
    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
      gem 'sass-rails',   '~> 3.2.3'
      gem 'coffee-rails', '~> 3.2.1'
    
      # See https://github.com/sstephenson/execjs#readme for more supported runtimes
      # gem 'therubyracer', :platforms => :ruby
    
      gem 'uglifier', '>= 1.0.3'
    end
    
    gem 'jquery-rails'
    
    # To use ActiveModel has_secure_password
    # gem 'bcrypt-ruby', '~> 3.0.0'
    
    # To use Jbuilder templates for JSON
    # gem 'jbuilder'
    
    # Use unicorn as the app server
    # gem 'unicorn'
    
    # Deploy with Capistrano
    # gem 'capistrano'
    
    # To use debugger
    # gem 'debugger'