Forum: Ruby on Rails Rather sticky migrate problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Scott B. (Guest)
on 2006-01-24 07:32
(Received via mailing list)
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
Kelly Dwight F. (Guest)
on 2006-01-24 07:54
(Received via mailing list)
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
This topic is locked and can not be replied to.