Migration reports success but did not do anything

I observed this a few times, but not always happen, and if I down the
migration and run again, it often works.

My migration is really simple like:

Foo.find_each do |foo|
foo.update_attribute(:bar, “baz”)
end

Occasionally, it reports success, but not actually updated the
attribute for the “bar” column of Foo object. It appears to just skip
the whole thing and report success. And as I said, if I down it and
run again, it has always worked.

Anybody else has seen this problem before? I’m using Rails 3.0.10 but
I had this problem with older 3.0.x versions as well.

So whenever you migrate down and back up it works? Sounds like the
migration is working properly then.

Under what circumstances does it not work?

That’s exactly what I try to figure out. Migrations like this randomly
get “skipped” (reported success but didn’t do anything).

It doesn’t happen consistently (and doesn’t happen most of time).

On Oct 31, 6:48pm, NY [email protected] wrote:

attribute for the “bar” column of Foo object. It appears to just skip
the whole thing and report success. And as I said, if I down it and
run again, it has always worked.

Anybody else has seen this problem before? I’m using Rails 3.0.10 but
I had this problem with older 3.0.x versions as well.

Failed validations? You do have to be a little careful when using
models in migrations. Consider this scenario:

  • The migration above is written
  • A new validation is written and a migration that fixes the data
    already present in the database is written

if you try and run both of these migrations in one go then the updates
in the first migration fail, because the latest validations are used.
if you try and run the migration a second time they succeed because by
now the data fixing migration has run. The way to get around this
particular problem is to use copies of the model in your migration
(the migrations guide on guides.rubyonrails.org discusses this)

Fred