As title, too bad I don’t have experience on this one, anyone could
help?
Here is my patch:
desc 'Runs the "up" for a given migration VERSION.'
task :up => :environment do
version=
if ENV["NAME"]
get_migrate_task_version(ENV["NAME"])
else
ENV["VERSION"] ? ENV["VERSION"].to_i : nil
end
raise "VERSION or NAME is required" unless version
ActiveRecord::Migrator.run(:up, "db/migrate/", version)
Rake::Task["db:schema:dump"].invoke if
ActiveRecord::Base.schema_format == :ruby
end
desc 'Runs the "down" for a given migration VERSION.'
task :down => :environment do
version= ""
if ENV["NAME"]
version= get_migrate_task_version(ENV["NAME"])
else
ENV["VERSION"] ? ENV["VERSION"].to_i : nil
end
raise "VERSION or NAME is required" unless version
ActiveRecord::Migrator.run(:down, "db/migrate/", version)
Rake::Task["db:schema:dump"].invoke if
ActiveRecord::Base.schema_format == :ruby
end
def get_migrate_task_version(task_name)
files=Dir["#{RAILS_ROOT}/db/migrate/*_#{task_name.underscore}.rb"]
raise “More than one task found” if files.length>1
raise “No task found” if files.length==0
files[0] =~ /[[:digit:]]+/
$~[0].to_i
end
What it does is to allow you use rake db:migrate:down/up
NAME=YourMigrationTaskName instead of trying to dig your version with
grep etc… it would only run when a precise migration can be found so
to avoid mis-executed migration.