Note that there are some explanatory texts on larger screens.

plurals
  1. PORolling back a failed Rails migration
    text
    copied!<p>How do you roll back a failed rails migration? I would expect that <code>rake db:rollback</code> would undo the failed migration, but no, it rolls back the previous migration (the failed migration minus one). And <code>rake db:migrate:down VERSION=myfailedmigration</code> doesn't work either. I've ran into this a few times and it's very frustrating. Here's a simple test I made to duplicate the problem:</p> <pre><code>class SimpleTest &lt; ActiveRecord::Migration def self.up add_column :assets, :test, :integer # the following syntax error will cause the migration to fail add_column :asset, :test2, :integer end def self.down remove_column :assets, :test remove_column :assets, :test2 end end </code></pre> <p>result:</p> <pre> == SimpleTest: migrating ===================================================== -- add_column(:assets, :test, :integer) -> 0.0932s -- add_column(:asset, :error) rake aborted! An error has occurred, all later migrations canceled: wrong number of arguments (2 for 3) </pre> <p>ok, lets roll it back:</p> <pre> $ rake db:rollback == AddLevelsToRoles: reverting =============================================== -- remove_column(:roles, :level) -> 0.0778s == AddLevelsToRoles: reverted (0.0779s) ====================================== </pre> <p>huh? that was my last migration before SimpleTest, not the failed migration. (And oh, it would be nice if the migration output included the version number.)</p> <p>So lets try running the down for the failed migration SimpleTest:</p> <pre> $ rake db:migrate:down VERSION=20090326173033 $ </pre> <p>Nothing happens, and no output either. But maybe it ran the migration anyway? So lets fix the syntax error in the SimpleTest migration, and try to run it again. </p> <pre> $ rake db:migrate:up VERSION=20090326173033 == SimpleTest: migrating ===================================================== -- add_column(:assets, :test, :integer) rake aborted! Mysql::Error: Duplicate column name 'test': ALTER TABLE `assets` ADD `test` int(11) </pre> <p>Nope. Obviously the migrate:down didn't work. It's not failing, it's just not executing.</p> <p>No way to get rid of that duplicate table other than manually going into the database and removing it, and then running the test. There's got to be a better way than that.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload