stonean

Timestamped Migrations

<update>
As soon as I figure out how, I will be submitting a patch to Rails to make this an option. I would like to use something like:

config.active_record.timestamped_migrations = true

This was suggested in a comment by hardbap referencing the announcement of this post. I agree with idea, but think the default should be false and users can set to true (as in my example) if they want this change. We’ll see how that goes… :)

Even though I botched the process, I have submitted a ticket.
</update>

Alright, so I really don’t like this feature, the anti-collision benefit is outweighed by the annoyance factor.

So, here’s where open source code rocks and why Ruby is my language of choice.

require "rails_generator"

class Rails::Generator::Commands::Base
  protected
  def current_migration_number
    Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path|
      n = File.basename(file_path).split('_', 2).first.to_i
      if n > max then n else max end
    end
  end

  def next_migration_number
    current_migration_number + 1
  end

  def next_migration_string(padding = 3)
    "%.#{padding}d" % next_migration_number
  end
end

I just created a file called classic_migrations.rb in config/initializers and added the above code. I haven’t run it through all the various scenarios yet, but for a simple migration setup, it works fine.

It should work for everything else, after all, there is nothing inherently different between the timestamped version number and the “old” version number. They are both just numbers representing a sequence.

For the collision issue, here’s a simple idea.

Written by stonean

June 17, 2008 at 4:47 pm

Posted in RubyOnRails