bellycard / napa

A simple framework for building APIs with Grape
Other
329 stars 72 forks source link

ActiveRecord::AdapterNotSpecified - Heroku #233

Closed MOPineyro closed 8 years ago

MOPineyro commented 8 years ago

The app runs on heroku, but when attempting to run napa console I receive the following error:

/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.13/lib/active_record/connection_adapters/connection_specification.rb:29:in `spec': ActiveRecord::AdapterNotSpecified (ActiveRecord::AdapterNotSpecified)
    from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.0.13/lib/active_record/connection_handling.rb:39:in `establish_connection'
    from /app/config/initializers/active_record.rb:4:in `<top (required)>'
    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:229:in `require'
    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:229:in `block in require'
    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:214:in `load_dependency'
    from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.0.13/lib/active_support/dependencies.rb:229:in `require'
    from /app/app.rb:12:in `block in <top (required)>'
    from /app/app.rb:12:in `map'
    from /app/app.rb:12:in `<top (required)>'
    from config.ru:1:in `require'
    from config.ru:1:in `block in <main>'
    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in `instance_eval'
    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in `initialize'
    from config.ru:in `new'
    from config.ru:in `<main>'
    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.5.5/lib/rack/builder.rb:49:in `eval'
    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.5.5/lib/rack/builder.rb:49:in `new_from_string'
    from /app/vendor/bundle/ruby/2.2.0/gems/rack-1.5.5/lib/rack/builder.rb:40:in `parse_file'
    from /app/vendor/bundle/ruby/2.2.0/gems/racksh-1.0.0/lib/racksh/init.rb:23:in `init'
    from /app/vendor/bundle/ruby/2.2.0/gems/napa-0.5.0/lib/napa/cli/base/console.rb:24:in `console'
    from /app/vendor/bundle/ruby/2.2.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /app/vendor/bundle/ruby/2.2.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /app/vendor/bundle/ruby/2.2.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /app/vendor/bundle/ruby/2.2.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /app/vendor/bundle/ruby/2.2.0/gems/napa-0.5.0/bin/napa:5:in `<top (required)>'
    from /app/vendor/bundle/ruby/2.2.0/bin/napa:23:in `load'
    from /app/vendor/bundle/ruby/2.2.0/bin/napa:23:in `<main>'
shaqq commented 8 years ago

@MOPineyro It sounds like something is wrong with your database configuration. Do you have an idea of how you're setting up your database.yml?

Also, what ActiveRecord version are you using?

MOPineyro commented 8 years ago

My database.yml is the default

defaults: &defaults
  encoding: unicode
  adapter: postgresql
  host: <%= ENV['DATABASE_HOST'] %>
  username: <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  database: <%= ENV['DATABASE_NAME'] %>

production:
  <<: *defaults

development:
  <<: *defaults

test:
  <<: *defaults

staging:
  <<: *defaults

activerecord (4.0.13)

I started a fresh napa app and pushed it to heroku and it throws the same error.

shaqq commented 8 years ago

Ah, if you have anything below ActiveRecord 4.1, Heroku will rewrite your database.yml:

https://github.com/heroku/heroku-buildpack-ruby/blob/8db0a60e1d0148611cd6dd075c339548ee8ae2e3/lib/language_pack/ruby.rb#L651

https://devcenter.heroku.com/articles/rails-database-connection-behavior#prior-to-rails-4-1

That means you need to have your DATABASE_URL env set, and you'll have to change your ActiveRecord setup like so:

# in your config/initializers/active_record.rb

db_config =  ENV['DATABASE_URL'] || YAML.load(ERB.new(File.read('./config/database.yml')).result)[Napa.env]
db_connection = ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Base.configurations["db"] = db_connection.spec.config

This should get you on your way, assuming you have the pg gem installed

Though, I would qualify this as a bug, since it's not working correctly out of the box. We'll figure out a way to fix the scaffolding.

MOPineyro commented 8 years ago

Updated active record then modified the initializer like you mentioned. All good now. Thank you!

shaqq commented 8 years ago

should be fixed by https://github.com/bellycard/napa/pull/234