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?

Rails: db:rollbackがエラーになったが、なんとかロールバックしたい。

Posted at

環境

Docker Desktop 4.35.1 (173168)
Debian GNU/Linux 12 (bookworm)
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
Rails 7.1.5

事象

今回は2つのテーブルのカラムを追加したり削除したりしてデータパッチするmigrationを実行後、rollbackしようとしたらエラーになった。

# rails db:rollback
== 20241124220824 XXXXXXXXXXXXXXXXXX: reverting ===========================
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

このマイグレーション(20241124220824)をなかったことにしたい。

対応

  1. 手動でSQLを実行し、カラム追加・削除、データパッチを戻す。(ここは頑張る)
  2. データベースのschema_migrationsテーブルから、20241124220824のレコードを削除する。
  3. db/schema.rbのversionを1つ前のマイグレーションファイルに戻す。
    書き換え前:
    ActiveRecord::Schema[7.1].define(version: 2024_11_24_220824)
    
    書き換え後:
    ActiveRecord::Schema[7.1].define(version: 2024_11_24_004258) 
    
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?