Hard work by INTERNET

ベンチャーで働くひとりぼっちWEB開発者が頑張るブログ

Migrationが2回走る #37225

Migration running twice #37225

Migration running twice · Issue #37225 · rails/rails · GitHub

質問1

railsコマンドを実行すると2回実行してしまう。一体何が起きているんですか? (本文に再現コードあり)

回答1

This file does not have the migration code in an up method.

そのファイルにはマイグレーションファイルの中にupメソッドがない。

I would venture a guess that the file is getting run multiple times and running the defined migrations each time

そのファイルは複数回実行されて、マイグレーションの定義を毎回実行する、と検討をつけるでしょう。

The syntax highlighting is a dead giveaway, watch out for that 😎

シンタックスハイライトは死んでる。注意してね。

質問2

Yeah, that would be it. Why isn't that an error? "Migration has no up or change method"

おおおお、たぶんそれっぽい。なぜマイグレーションにup か changeが無い時にエラーが起きないの?

回答3

Yeah it's just a ruby file and those methods (create_table, add_index, etc) are class methods.

それはただのrubyファイルであり、クラスメソッドです。

It's definitely a gotcha but unfortunately it's valid ruby.

たしかに落とし穴(?)しかし、残念ながら正しいRubyです。

When you require the file, it will perform the method calls just as directed.

そのファイルが必要な時、そのメソッドを直接実行するでしょう。

Also, up, down, and change are all intentionally optional.

また、up, down, changeはすべて意図的にオプションです。

This allows you to do things like comment out all your methods to force a migration to be marked as ran.

あなたのメソッドをコメントアウトするように、マイグレーションを強制的にマークすることができるようになります。

4.2の互換性を断つなら、down, up, change以外でのDSLをブロックできる、ということが書いていた。 おわり。