Using models in migrations

Hi team
I came across an issue with migrations when I was adding a validation to
partition tables recently. One of the migrations used partition tables
model directly for creating/updating them. This broke the migration when
one wanted to do it from scratch after my validation update.

The problem is migrations stay the same while using the most recent
version of models.
I think the recommended solution is to redefine models locally in the
migration. Something like:

class ConfigTemplate < ActiveRecord::Base
has_and_belongs_to_many :operatingsystems
#more necessary definitions here
end

We should keep this in mind when adding new migrations. Overwhelming
majority of our migrations is ok but still I found few that aren't safe.
I wanted to point this out for less experienced RoR devs like me:)

Regards
Tomas

Tomas,

we solve that by turning off validations for that particular migration.
There are couple of examples in the codebase, there is a scope or
something to do that.

LZ

··· On Tue, Jul 23, 2013 at 02:33:57PM +0200, Tomas Strachota wrote: > Hi team > I came across an issue with migrations when I was adding a > validation to partition tables recently. One of the migrations used > partition tables model directly for creating/updating them. This > broke the migration when one wanted to do it from scratch after my > validation update. > > The problem is migrations stay the same while using the most recent > version of models. > I think the recommended solution is to redefine models locally in > the migration. Something like: > > class ConfigTemplate < ActiveRecord::Base > has_and_belongs_to_many :operatingsystems > #more necessary definitions here > end > > We should keep this in mind when adding new migrations. Overwhelming > majority of our migrations is ok but still I found few that aren't > safe. I wanted to point this out for less experienced RoR devs like > me:) > > > Regards > Tomas > > -- > You received this message because you are subscribed to the Google Groups "foreman-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to foreman-dev+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > >


Later,

Lukas “lzap” Zapletal
irc: lzap #theforeman

Thanks Tomas for opening this topic. It's really something that can (and
certainly will) cause problems in future.

> we solve that by turning off validations for that particular migration.
> There are couple of examples in the codebase, there is a scope or
> something to do that.

There are other problems you can encounter when you use models in validations.
For example callbacks can use attributes that do not exist yet. I prefer
either defining your own models in migrations or executing plain SQL (as long
as you make it DB agnostic).

If you want to define your own models I would recommend giving them a special
name so they do not collide with existing classes that may already be loaded
(e.g. Setting). Also note that they can collide within several migrations, so
maybe dynamic classes would be appropriate.

··· -- Marek

On Tuesday 23 of July 2013 14:44:57 Lukas Zapletal wrote:

Tomas,

we solve that by turning off validations for that particular migration.
There are couple of examples in the codebase, there is a scope or
something to do that.

LZ

On Tue, Jul 23, 2013 at 02:33:57PM +0200, Tomas Strachota wrote:

Hi team
I came across an issue with migrations when I was adding a
validation to partition tables recently. One of the migrations used
partition tables model directly for creating/updating them. This
broke the migration when one wanted to do it from scratch after my
validation update.

The problem is migrations stay the same while using the most recent
version of models.
I think the recommended solution is to redefine models locally in

the migration. Something like:
class ConfigTemplate < ActiveRecord::Base

has_and_belongs_to_many :operatingsystems
#more necessary definitions here

end

We should keep this in mind when adding new migrations. Overwhelming
majority of our migrations is ok but still I found few that aren’t
safe. I wanted to point this out for less experienced RoR devs like
me:)

Regards
Tomas

Marek