Generating migrations gets easier - Rails 2.0 a feature a day #6

You may not have noticed this yet, but Rails 2.0 has a new convenient syntax for generating ActiveRecord migrations. Go ahead, run the migration generator with script/generate migration. I’ll wait.

Yup, you can now specify the columns you want to add in your migration by passing attribute/type pairs to the migration generator. Ergo,

script/generate migration AddMoreToAnime episodes:integer licensed:boolean

will generate a migration like so:

class AddMoreToAnime < ActiveRecord::Migration
  def self.up
    add_column :animes, :episodes, :integer
    add_column :animes, :licensed, :boolean
  end

  def self.down
    remove_column :animes, :licensed
    remove_column :animes, :episodes
  end
end

This ties in pretty nicely with the syntax for the model generator (script/generate model), which also accepts attribute pairs. Tiny change but “it’s all about consistency”.

For the geeks among you, check out the related changeset and ticket.

About the contributor, Pratik Naik

Pratik Naik (WorkingWithRails profile), probably better known as lifofifo or just lifo, is an active Rails contributor and can be found on #rails-contrib and #rubyonrails nearly 24/7. Almost everyone who’s used Rails has used code that lifo has written. Oh, and lifo also won one of the monthly Rails Hackfests.

He also keeps a pretty good blog where he posts mostly on Rails hacking and tips.

2 Comments & TrackBacks ()

Paper doll icon
sho's Gravatar

Don’t forget the –svn at the end to automatically add to SVN!

I really like this new change - finally generating the whole, complete migration and adding to SVN is just as fast, if not faster, than just making the change directly in the DB - meaning it’s fast enough that I’ll actually use migrations. Fantastic work from Pratik and the Rails team.

Definitely one of my favourite features from 2.0!

Posted by: sho on December 24, 2007 5pm

Paper doll icon
Tecker.LOG » Blog Archive » Generating migrations gets easier?????Rails 2.0?? #6(??)'s Gravatar

[…] ??? Generating migrations gets easier - Rails 2.0 a feature a day #6 […]

Posted by: Tecker.LOG » Blog Archive » Generating migrations gets easier?????Rails 2.0?? #6(??) on June 4, 2008 7pm

You can subscribe to the RSS feed for comments on this post.

Sorry, this entry is no longer accepting comments. If you have something you really want to say, you can write me.