#!/usr/bin/env ruby

# occasionally i need to run a single migration.  there are few ways to do
# that, but this script, save as ./script/migrate is a very simple way to do
# that.
#
  USAGE = 'migrate path_to_migration [up|down]'

# bootstrap
#
  require File.dirname(__FILE__) + '/../config/environment'

# parse args
#
  path = ARGV.shift
  direction = ARGV.shift || 'up'

  abort('migrate path_to_migration') unless
    path and %w[ up down ].include?(direction)

# track loaded migrations classes and load the migration path 
#
  migrations = []

  singleton_class =
    class << ActiveRecord::Migration
      self
    end

  singleton_class.module_eval do
    define_method(:inherited) do |other|
      migrations << other
      super if defined? super
    end
  end

  Kernel.load path

# run any appropriate migrations the right direction
#
  migrations.each{|migration| migration.migrate(direction.to_sym)}