Heroku部署数据库为空

时间:2014-05-07 17:28:06

标签: ruby-on-rails sqlite postgresql heroku

我遇到问题,我使用sqllite3在我的本地数据库中有数据,但我已经部署到heroku(postgresSQL)并且数据库是空的,我认为这会导致我的某些页面出现问题。我需要更改我的gemfile吗?见下文

`source 'https://rubygems.org'

#ruby-gemset=railstutorial_rails_4_0

#gem 'ruby', '2.0.0'
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'nokogiri'
gem 'mechanize'
gem 'debugger'

group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'

end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
  gem 'factory_girl_rails', '4.2.1'
  gem 'cucumber-rails', '1.4.0', :require => false
  gem 'database_cleaner', github: 'bmabey/database_cleaner'


end

gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end`

我做什么?

1 个答案:

答案 0 :(得分:0)

首先,您的开发和生产数据库应该是相同的。因此,如果您要继续使用Heroku,您也应该进行开发。

heroku run rake db:migrate不会在本地数据库中添加数据。请参阅:https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull,用于从heroku推送和提取数据。

据我所知,你无法将sqlite数据库直接推送到heroku。所以你必须切换到postgres数据库,然后推送到heroku。你可以遵循RailsCast来做到这一点。

现在其他选项是为您的数据播种。在lib/tasks下,您可以使用rake文件:

<强> sample_data.rake

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    make_users
  end
end

def make_users
  admin = User.create!(name:     "foobar",
                       email:    "example#example.com",
                       password: "foobar",
                       password_confirmation: "foobar",
                       admin: true)
  99.times do |n|
    name  = Faker::Name.name
    email = "example-#{n+1}@example.com"
    password  = "password"
    User.create!(name:     name,
                 email:    email,
                 password: password,
                 password_confirmation: password)
  end
end

然后你可以运行

heroku run rake db:seed

为种子数据库播种。我认为这需要重置您的数据库 将销毁所有生产数据 。所以,只有在你重新开始时才这样做。

最后,如果你的制作和本地数据库都是postgres,那么你将会更容易进行。