29

I typed in rails db:migrate and received the following error. I googled and someone said something like changing the Migration version from [4.2] to [5.1] but it still has not worked.

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 CreateCkeditorAssets < ActiveRecord::Migration[4.2]

Tachyons
  • 2,131
  • 1
  • 21
  • 35
jose tanaka
  • 489
  • 1
  • 4
  • 9

2 Answers2

57

Aditya already wrote the answer, Changing all migrations by hand is a hectic task, So I wrote a one liner script to do that

In linux (Gnu sed)

grep -rl "ActiveRecord::Migration$" db | xargs sed -i 's/ActiveRecord::Migration/ActiveRecord::Migration[4.2]/g'

In Mac (BSD sed)

grep -rl "ActiveRecord::Migration$" db | xargs sed -i "" "s/ActiveRecord::Migration/ActiveRecord::Migration[4.2]/g"

Note, you can replace 4.2 with the rails from which you are upgrading to 5.1

Tachyons
  • 2,131
  • 1
  • 21
  • 35
  • 3
    ^ This is awesome and saved me a bunch of time. – Matthew Rathbone Apr 15 '18 at 21:23
  • 2
    I run this command `grep -rl "ActiveRecord::Migration$" db | xargs sed -i 's/ActiveRecord::Migration/ActiveRecord::Migration[5.2]/g'` but its not working i am getting this output `sed: no input files` – Nency Apr 05 '19 at 13:30
  • @Nency Which OS ? – Tachyons May 10 '19 at 06:26
  • On macOS Mojave the BSD sed script provided doesn't work, use the Gnu sed one. – Mauro Nidola Oct 31 '19 at 09:48
  • Is this really advisable? Let's say I'm updating from 4.2. Could it cause issues if some of the migrations were written prior to 4.2? – Daniel Dec 05 '19 at 21:57
  • @Daniel I did this for multiple projects, this worked without any issues – Tachyons Dec 06 '19 at 07:40
  • https://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html explains why it's safe. – Daniel Dec 07 '19 at 01:26
  • I get this error when I run the command on ubuntu ```sed: can't read s/ActiveRecord::Migration/ActiveRecord::Migration[5.1]/g: No such file or directory ``` – chrisgeeq Nov 20 '20 at 10:56
  • @chrisgeeq hi, probably too late, but you need to remove space sign between `-i` and `""` as mentioned here https://stackoverflow.com/a/4247319/6860421 – Roman Alekseiev Jan 28 '21 at 15:18
39

Rails 5 changed the way migrations are created. You'll have to specify the Rails release starting Rails 5 like this (assuming you're using Rails 5.1):

class CreateCkeditorAssets < ActiveRecord::Migration[5.1]

Alternately, you can try creating a test migration and see how your version of Rails generates a migration and then take it from there:

rails g model Test name:string
Aditya
  • 1,693
  • 19
  • 27
  • Thanks! I wasnt able to fix this issue until I found out that I was looking at the files within the ckeditor folder instead of the migration file!! – jose tanaka Jun 08 '17 at 01:40
  • Is the value inside the brackets the version of Rails that you are running the migration in, or the version of Rails where the migration was created? That is, if I wrote a migration under 4.2 that I am applying to a Rails 5 environment, should I use 4.2 or 5.1 inside the braces? – Phil DD Mar 28 '18 at 18:49
  • @PhilDD It should be 4.2 – Tachyons Jul 17 '18 at 14:59
  • Thank you, the error occurred for me after installing and configuring Paperclip/ImageMagick combo. I added my [5.1] to the end of the first line of the related migration file like so... class AddAttachmentImageToPosts < ActiveRecord::Migration[5.1] – domdaviesdev Feb 03 '19 at 23:07