Rather sticky migrate problem

Here’s the problem… Typo has ~35 migrate scripts. Script 3 makes a
minor schema change, then migrates data to the new schema. For
instance:

add_column :articles, :user_id, :integer
Article.find(:all).each do |a|
  a.user=User.find_by_name(a.author)
  a.save
end

This makes sense… except that a revision 35 model has been
instantiated on a revision 3 schema! This, of course, barfs all sorts
of errors about missing tables, missing rows, etc.

How can this be fixed? Do I need to rewrite the models so that they
support ALL schemas this database has ever had? That sounds tedious.

Without this, however, upgrading typo should require one checkout for
each schema change! Otherwise, models and schemas will not necessarily
match. You’ll be relying on luck for your migration to succeed
(assuming you don’t do a careful analysis first, which I’m betting none
of you do… :).

This problem seems to make migration scripts only useful for minor
upgrades (Typo 2.5.5 -> 2.6.0)! Am I missing something here? Thanks,

- Scott

Yeah. This is an issue.

I don’t know how to say it more clearly than you.

I deal with this by not using models in most migrate scripts. I’m not
saying this is an absolute rule. If you have a single rails application
that
stays in lock step with development, then you only have to be careful
about using models in migrations. I think most applications fall in this
category.

However, if you have an application that will be deployed in multiple
places
– where you can’t ensure that all deployments will be at a certain
migrate
level, then you probably need to remove models from the migrate scripts.

The schema migrations don’t require models. When migrating the data,
don’t
use models unless you are confident that someone will not be attempting
to
migrate an old schema when the application has moved forward in some
incompatible way. Instead use execute and insert statements…in
other
words, use sql.

In one case I renamed and modified the model. I kept the old version of
the
model and had the migrate script use this. I don’t recommend this.

-Kelly