1

I started out using Rolify in a Rails app and created a migration to set up its tables about 15 migrations ago. I've now decided to replace it with my own code and want to revert that one migration without touching all of the later migrations. The database is in use now, so reverting 15, removing the one I don't want to add and then applying the subsequent 14 would destroy data.

Section 3.11 of the Rails Guide on migrations suggests that this can be done by creating a new migration which reverts the specific old migration by name:

class FixupExampleMigration < ActiveRecord::Migration
  def change
    revert ExampleMigration

    create_table(:apples) do |t|
      t.string :variety
    end
  end
end

I tried to customise this to my context, which would look like this:

class RolifyDestroyRoles < ActiveRecord::Migration
  def change
    revert RolifyCreateRoles
  end
end

(The first line of my original Rolify migration was class RolifyCreateRoles < ActiveRecord::Migration). However, I get a namespace error:

StandardError: An error has occurred, this and all later migrations canceled:

uninitialized constant RolifyDestroyRoles::RolifyCreateRoles/home/slack/rails/tracker/db/migrate/20150127093921_rolify_destroy_roles.rb:3:in `change'

Maybe something has changed in Rails 4. Does anyone know how I should refer to RolifyCreateRoles so that Rails can find it?

Dan
  • 778
  • 11
  • 18

1 Answers1

2

Reverting a specific migration in rails :

Let's say we have a migration :

db/migrate/20150127071749_create_users.rb

revert: 
rake db:migrate:down VERSION=20150127071749

setup again:
rake db:migrate:up VERSION=20150127071749

Hope that helps :)

Ajay
  • 4,199
  • 4
  • 27
  • 47
  • 1
    Will this not also revert all migrations that came after 20150127071749? – Dan Jan 27 '15 at 08:22
  • 1
    No, this won't revert all migrations. this will be targeting to a specific migration(20150127071749). – Ajay Jan 27 '15 at 08:30
  • 1
    Great! I was confused by [this answer](http://stackoverflow.com/a/7694500/1857164) which states that `rake db:migrate:down VERSION=nnn` will roll down **to** a specific version -- I assumed that meant rolling back everything in between, but clearly not. – Dan Jan 27 '15 at 09:11
  • 1
    To clarify, `rake db:migrate:down VERSION=nnn` will just take down the specified version, whereas `rake db:migrate VERSION=nnn` will roll back **to** the specified version (taking down any intermediary migrations as necessary). – Dan Apr 09 '15 at 14:14