• Home
  • About

stonean

Feeds:
Posts
Comments

Generators and Timestamped Migrations

June 2, 2008 by stonean

I’ve never liked the idea of timestamped migrations, still don’t. So, not to my surprise, this was the first (and only) thing that bit me when testing the Lockdown generators against the latest release of Rails (2.1).

What happened? Well, the Lockdown generator generates 5 migrations, and contrary to all the speed talk surrounding Ruby, it does this in less than a second. (Yes – I know Ruby isn’t super fast, but it gets the job done.)

What does this mean? Well, all migrations have the same “number” and therefore don’t work. Lovely. Just lovely.

So here’s what you need to add to your Rails::Generator to get around this issue:

if Rails::VERSION::MAJOR >= 2 && Rails::VERSION::MINOR >= 1  class Rails::Generator::Commands::Base    protected    def next_migration_string(padding = 3)      sleep(1)      Time.now.utc.strftime("%Y%m%d%H%M%S")     end  enden

I’m grateful for Rails and to the Rails community. They have done a lot of really good stuff.

I only have one question, couldn’t timestamped migrations have been an option? I’m not the only one who disliked the concept from the beginning.

Note: I’m not expecting anyone from the Rails team to have even heard of this blog so it’s a rhetorical question.

Posted in RubyOnRails | 1 Comment

One Response

  1. on October 13, 2008 at 8:19 pm Evgeny Myasishchev

    I got the same problem as you does. Proposed solution is nice but I have more than 10 migrations so this means 10 seconds delay…

    Here what I have used to make it work:
    class Rails::Generator::Commands::Base
    protected
    def next_migration_string(padding = 3)
    next_unique_migration_num(Time.now.utc.strftime("%Y%m%d%H%M%S").to_i)
    end

    def next_unique_migration_num(num)
    if @prev_num && @prev_num >= num
    next_unique_migration_num(num += 1)
    else
    @prev_num = num
    end
    end
    end



Comments are closed.

  • Open Source


    Lockdown (GitHub)
    RuHL (GitHub)

  • Recommend Me

  • twitter: stonean

    • Yet another way to waste time in the guise of keeping up to date: http://vurl.me/EIR 2 days ago
    • So I submitted my RailsConf proposal, called it Beyond MVC. We shall see. I do such a bad job talking about myself, the bio sucked. 2 days ago
    • @ubermuda that's good to know, thank you! 4 days ago
    • @chanmix51 cool, thanks! 4 days ago
    • In vi, :set list to show hidden characters, to revert, :set nolist Very handy. 4 days ago
  • Categories

    classy-inheritance Code Style DataMapper Fusion git Haml Interview Questions jQuery lockdown Merb MysqlUtils Process Quick Tips REST RSpec Ruby RubyOnRails rubytrends Stage thoughts Uncategorized


  • Archives

    • November 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007

Blog at WordPress.com.

Theme: Mistylook by Sadish.