Sinatra与Postgres和ActiveRecord |错误推送到Heroku是:无效的DATABASE_URL

时间:2014-01-10 22:05:29

标签: ruby postgresql heroku sinatra

我有一个用Sinatra编写的应用程序在本地工作,但我似乎无法推动到heroku。

当我尝试推送到heroku时,我收到以下错误:

  

- - - - >编写config / database.yml以从DATABASE_URL读取         无法检测rake任务         确保您可以针对您的应用运行$ bundle exec rake -P,而不会出现环境变量         并使用Gemfile的生产组。         如果您希望运行rake任务,这可能是有意的         取消构建(CTRL + C)并修复错误然后提交修复:         耙子流产了!         无效的DATABASE_URL

我的Gemfile

source 'https://rubygems.org'
ruby '2.0.0'

gem 'sinatra', require: 'sinatra/base'
gem "pg"
gem "activerecord"
gem "sinatra-activerecord"
gem "rake"

gem 'minitest', require: false
gem 'rack-test', require: false
gem 'faraday'
gem 'json'
gem 'minitest-reporters'

我的database.yml文件

development:
  adapter: postgresql
  encoding: unicode
  database: calendar_development
  host: localhost
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: calendar_test
  pool: 5
  host: localhost
  password:

我的config.ru

require './app'
run Sinatra::Application

ENV['RACK_ENV'] ||= 'development'

Procfile

web: bundle exec rackup config.ru -p $PORT

Rake文件

require "sinatra/activerecord/rake"
require 'rake/testtask'
require "./app"

task :default => :test
env = ENV["SINATRA_ENV"] || "development"

Rake::TestTask.new do |t|
  t.libs << 'test'
  t.test_files = FileList["test/**/*_test.rb"]
  t.verbose = false
end

app.rb

require 'sinatra' 
require 'sinatra/activerecord'
require 'json'
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }

env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
env = env_arg || ENV["SINATRA_ENV"] || "development"

use ActiveRecord::ConnectionAdapters::ConnectionManagement # close connection to the DDBB properly...https://github.com/puma/puma/issues/59
databases = YAML.load_file("config/database.yml")
ActiveRecord::Base.establish_connection(databases[env])

if env == 'test'
  User.destroy_all
end

1 个答案:

答案 0 :(得分:5)

我从app.rb文件中删除了所有环境测试并将其移至环境:

app.rb

require './config/environments'

然后我更改了数据库设置 - 首先,删除了所有的ENV [SINATRA_ENV]

environments.rb

configure :production, :development, :test do
  db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/calendar_development')

  ActiveRecord::Base.establish_connection(
      :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
      :host     => db.host,
      :username => db.user,
      :password => db.password,
      :database => db.path[1..-1],
      :encoding => 'utf8'
  )
end

我改变了我的Rakefile

namespace :db do
  task :seed do
    seed_file = File.join('db/seeds.rb')
    load(seed_file) if File.exist?(seed_file)
  end
end

我按照sybohy的建议,在我的config.ru文件中,我删除了最后一行 - 离开

config.ru

require './app'
run Sinatra::Application

我跑了:

  

heroku addons | grep POSTGRES

抓住了db的名称(HEROKU_POSTGRESQL_JADE)

  

heroku pg:重置HEROKU_POSTGRESQL_JADE

然后迁移

  

heroku run rake db:migrate


到目前为止,这似乎修复了错误!如果我找到更多,我会更新!