0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails db:migrate がうまく実行されない時の対処法

0
Posted at

Webアプリを開発中にmigrateで手こずったので、その備忘録として。

原因

そもそもの原因として、migrationファイルに問題(作成した順番など)があるためrails db:migrateを実行していく中で整合性が合わなくなり、完了されなかったことが考えられる。

対処法

migrationファイルを直す方法(本当はこちらが良い)もあるが、今回は一時的な解決法として特定のversionのみmigrateし、それを繰り返すというやり方(少々強引※非推奨)を提示していく。

まずは以下のコマンドを実行し、migrateが実行されていないものを確認。

  rails db:migrate:status

すると

Status   Migration ID    Migration Name
--------------------------------------------------
   up     20200530221315  Create members
   up     20200531020945  Create administrators
   up     20200603224605  Create staff posts
  down    20200606215347  Create customers
  down    20200606215411  Remove addresses
   up     20200107092204  Devise create admin users
   up     20200107092210  Create active admin comments
  down    20200107092210  Create active admin comments

以上(例)のように表示されるので、statusがdownになっているMigration IDを把握する。
(upになっているファイルはすでにmigrate済み。)

そして、

  bundle exec rake db:migrate:up VERSION=Migration IDをここにコピペ

を実行してstatusがupになっていることを確認。

これを繰り返して全てのmigrationファイルのstatusがupになれば一番良いのだが、きちんとmigrateされないファイルも出てくる。そのような時は順番を変えて行えばうまくいくケースがあるので、何通りか試してupになることを確かめると良いかもしれない。

Heroku版

  heroku run rails db:migrate:status
  bundle exec heroku run rake db:migrate:up VERSION=Migration IDをここにコピペ
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?