LoginSignup
42
24

More than 3 years have passed since last update.

rails4 で利用していたmigrationファイルを rails5 用に移行するワンライナー

Last updated at Posted at 2017-09-19

rails5からどのrailsバージョンでmigrationを作成したかという情報が追加されました。
それにともないrails4で作成していたファイル全てにバージョン情報を付与するワンライナーで対応しました。

rails4で作成したmigrationファイルのまま bundle exec rails db:migrate すると以下のようにエラーとこんな感じで書いてなというメッセージが表示されます。

$ be rails db:migrate
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class CreateUsers < ActiveRecord::Migration[4.2]

以下のコマンドで ActiveRecord::Migration の後ろにバージョン( [4.2] )をつけてあげます。

$ find db/migrate -name '*.rb' | xargs sed -i "" 's/ActiveRecord::Migration$/ActiveRecord::Migration[4.2]/g'

2019/6/21: @creativivalさんのコメントを受けてコマンドを修正しました

これでmigrationも通るようになります。

この記事は以下の記事の焼き直しです。

42
24
4

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
42
24