Dragon Arrow written by Tatsuya Nakaji, all rights reserved animated-dragon-image-0164

Rails5.2 ローカルのDBデータをHerokuに渡す

updated on 2019-06-14

イメージ

トラブル
herokuにデプロイしたあと、テーブルにデータがなく空状態である
(After deploy to heroku, there are no data in tables)

実現したいこと
localのデータを本番環境(heroku)に移したい
(I would like to dump local data to heroku)


手順1 yaml_dbを導入

# Gemfile
gem 'yaml_db'
$ bundle install


手順2 コピーしたいデータをダンプ

$ rake db:dump RAILS_ENV="development"
(config/db/data.yml will be created!)

$ rake db:data:load RAILS_ENV="production"
(production.sqlite3 will be created!)


手順3 HerokuのPostgreSQLでダンプデータを読み込む

$ heroku run pg:reset
$ heroku run rake db:create RAILS_ENV="production"
$ heroku run rake db:schema:load RAILS_ENV="production"
$ heroku run rake db:migrate RAILS_ENV="production"
$ heorku run rake db:data:load RAILS_ENV="production"
(content of data.yml has been dumped to Heroku now!)