even better rake db:migrate

I normally don’t like hacking rails files directly, but this is the only way I could extend rake db:migrate to support a friendly VERSION.

railities/lib/tasks/database.rake on line 111.

  # was ENV["VERSION"] ? ENV["VERSION"].to_i : nil
  # looks up version based upon migration files
  def version_num
    version=ENV["VERSION"]
    if version.to_i==0 && version.present?
      matches=Dir["db/migrate/*#{ENV["VERSION"]}*"]
      if matches.length > 0
        #prune off directory name
        matches=matches.collect{|n| n.gsub(/^.*\//,'')}

        unless matches.length == 1
          raise "VERSION #{version} matches "+
            "#{matches.join(', ')}"
        end

        #prune off version num
        version=matches.first.gsub(/([^_]*)_.*$/) {$1}
        puts "VERSION=#{version}"
      end
    end
    version ? version.to_i : nil
  end

In 3 places (migrate, up, down), the version code is changed from

ENV["VERSION"] ? ENV["VERSION"].to_i : nil

to

version_num

Now rake db:migrate:down VERSION=create_photo works like a charm.