0
0

More than 1 year has passed since last update.

[Rails] migrationファイルが無く、RDBMSに変更が残ってしまった時の解消方法

Posted at

ブランチを移ってmigrationしたが、それを戻さずに違うブランチに移ってしまい、ファイルが行方不明になってしまった下記のような場合の解決法。

$ rake db:migrate:status
...
   up     20210709062913  ********** NO FILE **********

「rails migration no file」と検索するとマイグレーション管理のtableに変更を入れるだけ記事がいくつかヒットするが、本質的解決ではない。マイグレーションの冪等性を保つためにRDBMSに残っている変更も同時に消すことが望ましい。

https://qiita.com/sakatan_1/items/9bf321f81d3b84042694
https://note.com/yofupro/n/nd2ed39123066

解決策

前提:gitで管理されているリポジトリであること

手順

  1. gitのコミットに残っていないか検索する
  2. 検索結果からcherry-pickなり、コピペなりで、同名のmigrationファイルを復元する
  3. rake db:migrate:down VERSION=xxxx を実行する
  4. 2.で作成したmigrationファイルを削除する

1. gitのコミットに残っていないか検索する

$ git log --all --full-history db/migrate/20210709062913*

参考

あとはよしなに。

他の解決策

rails migration no file で検索するとstack overflowがヒットした。
What is the best way to resolve Rails orphaned migrations? - Stack Overflow

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