Heroku使用默认数据创建数据库

时间:2015-10-23 09:54:32

标签: ruby-on-rails postgresql sqlite heroku

我在rails中的noobie,我有db的问题。我的database.yml:

    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

我成功地用这个

来填充我的数据库
    namespace :db do
      desc "Erase and fill database"
      task :populate => :environment do
        require 'populator'
        require 'faker'

        [Country, Region, City, Turbaza].each(&:delete_all)

        ActiveRecord::Base.transaction do
          Country.populate 5 do |country|
            country.name = Faker::Address.country
            Region.populate 1..2 do |region|
              region.country_id = country.id
              region.name = Faker::Address.state
              City.populate 1..2 do |city|
                city.region_id = region.id
                city.name = Faker::Address.city
                Turbaza.populate 1..2 do |turbaza|
                  turbaza.city_id = city.id
                  turbaza.name = Populator.words(1..3).titleize
                end
              end
            end
          end
        end
      end
    end

所以我的开发数据库已经填满了数据,但我无法理解如何对生产数据库和数据库执行此操作。拜托,有人可以帮我吗?

2 个答案:

答案 0 :(得分:0)

如果您已将应用程序部署到Heroku,则可以通过heroku-cli运行rake任务 https://devcenter.heroku.com/articles/heroku-command

heroku run rake db:populate

P.S Heroku免费提供pg。所以我想上传你的pg我必须做的

将gem'pg'添加到Gemfile

bundle install 

commit you changes

rebuild your project in heroku

答案 1 :(得分:-1)

运行heroku run rake db:populate应该可以解决问题,你试过吗?而且(IMO)更好的解决方案就是使用种子(http://railscasts.com/episodes/179-seed-data)并运行heroku run rake db:seed