Stephen Celis

Bashfully Yours, Migration Editing 7 Aug

Let’s face it. The migration timestamps, while nice for teams with conflicts, are a bit painful otherwise. You create a migration and want to open it quickly, but the tab-completion is awkward. Too many numbers (shared by too many files) gum up the whole process.

$ script/generate migration add_comments_count_to_blog_entry comments_count:integer
      create  db/migrate
      create  db/migrate/20080807210534_add_comments_count_to_blog_entry.rb
$ mate db→/m→igrate/2→0080→→
20080507201200_[...]
20080522210443_[...]
20080522210513_[...]
20080522211057_[...]
20080527155018_[...]
20080527163414_[...]
20080527194753_[...]
20080530153856_[...]
20080603175922_[...]
20080603181555_[...]
20080605160930_[...]
20080605220209_[...]
20080605220944_[...]
20080606205245_[...]
20080609140835_[...]
20080609203207_[...]
20080610134408_[...]
20080610140232_[...]
20080610155523_[...]
20080610185209_[...]
20080611153645_[...]
20080617151810_[...]
20080617173216_[...]
20080619192702_[...]
20080620153937_[...]
--More--
$ mate db/migrate/20080

And that’s just the beginning. Copy-paste? Sure…but must I take my fingers from the keyboard?

I decided to slightly alleviate the problem with the help of ~/.bash_profile.

sg() {
  local migration=`script/generate $@ | awk ' /db\/migrate\//{print $NF}'`
  if [ $migration ]; then $EDITOR $migration && rake db:migrate; fi
}
alias sgmo="sg model $@"
alias sgmi="sg migration $@"

Let’s try it out.

$ source ~/.bash_profile
$ sgmi add_comments_count_to_blog_entry comments_count:integer

sgm

A quick edit, save, close, and…

(in ~/Code/blog/trunk)
== 20080807210534 AddCommentsCountToBlogEntry: migrating ======================
-- add_column(:blog_entries, :comments_count, :integer, {:default=>0})
   -> 0.0360s
== 20080807210534 AddCommentsCountToBlogEntry: migrated (0.0360s) =============

Beautiful. Migration editing without migraines.

September 24 update

Zubin Henner emailed me to let me know the script didn’t work with the -g or -c flags. I’ve added a space to the beginning of the regular expression above to make sure the script only matches path output that begins with “db/”.