LoginSignup
41
30

More than 3 years have passed since last update.

[Rails5] heroku db:resetからmigrateまで

Posted at

rubyをherokuでデプロイをしている。
DBはMySQL2を使用。

ローカル環境のようにrails db:resetはできないので、
マイグレーションファイルを初めからやり直したい際の情報が古く、また断片的だったためにメモ。

問題点

Rails5から、productionモードでは、db:dropなどのDBを破壊する系のコマンド実行を防止する機能が追加された。

よって下記の参考にさせていただいた方法ではロールバックできなかった。
[参考記事]
Herokuにデプロイ済みのMySQLのロールバック

解決に役立った参考記事

[参考記事]
Rails5のproductionでrake db:dropはできない、普通には

:point_right:下記より実際の手順

1. ロールバックを試す
$ heroku rake db:migrate:redo STEP=1

下記エラー文

rake aborted!
StandardError: An error has occurred, all later migrations canceled:
ActiveRecord::IrreversibleMigration
2. 次の3のステップに移るためにあえて実行。

[参考記事]
Herokuにデプロイ済みのMySQLのロールバック

コマンド

$ heroku run rake db:migrate:reset

エラー画面

rails aborted!
ActiveRecord::ProtectedEnvironmentError: You are attempting to run a destructive action against your 'production' database.
If you are sure you want to continue, run the same command with the environment variable:
DISABLE_DATABASE_ENVIRONMENT_CHECK=1
3. DBをdropさせる

2で表示された”DISABLE_DATABASE_ENVIRONMENT_CHECK=1”を使用する。
下記がコマンド

$ heroku run RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1 bundle exec rake db:drop

:point_down:やっとdropしてくれた

Dropped database ‘heroku_3b36eff50cb58e6'
4. DB生成

次のステップはheroku run rails db:migrateとの記事が多いができない。
railsコマンドいけるかなと思って試してみる。

$ heroku run rails db:create

無事に作られた

Created database 'heroku_3b36eff50cb58e6'
5. マイグレーション
$ heroku run rake db:migrate

以上、カラム名変更などマイグレーションがゴチャゴチャしてきてまとめたい際にロールバックができずに結構時間を要した。
heroku上でエラーの際などのMySQLコマンドがまとまった記事が見つけられずにあれこれ試した。
他に良い方法あれば是非教えてください:pray:

41
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
30