Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Rakefile to examples

  • Loading branch information...
commit 14d76ac4ae72acc467830fac361e096c1e9e63af 1 parent 3409e9f
@ericgj ericgj authored
Showing with 53 additions and 0 deletions.
  1. +53 −0 examples/Rakefile
View
53 examples/Rakefile
@@ -0,0 +1,53 @@
+# Sample tasks for automating DM migrations
+# Based roughly on Rails conventions
+# Assumes migrations are saved to db/migrate
+
+## DataMapper setup, load models, finalize here
+# require File.expand_path('boot', File.dirname(__FILE__))
+
+require 'rake'
+
+namespace :db do
+
+ desc "Run all pending migrations, or up to migration position TO=x"
+ task :migrate => [:load_migrations] do
+ if vers = ENV['TO']
+ migrate_up!(vers)
+ else
+ migrate_up!
+ end
+ end
+
+ desc "Rollback last STEP=x migrations (default 1), or down to migration position TO=y"
+ task :rollback => [:load_migrations] do
+ if vers = ENV['TO']
+ warn "Rolling back down to migration #{vers}..."
+ migrate_down!(vers)
+ else
+ step = ENV['STEP'] || 1
+ applied = migrations.delete_if {|m| m.needs_up?}.sort # note this is N queries as currently implemented
+ target = applied[-1 * step] || applied[0]
+ if target
+ warn "Rolling back #{step} step(s)..."
+ migrate_down!(target.position - 1)
+ else
+ warn "No migrations to rollback: #{step} step(s)"
+ end
+ end
+ end
+
+ desc "List migrations descending, showing which have been applied"
+ task :migrations => [:load_migrations] do
+ puts migrations.sort.reverse.map {|m| "#{m.position} #{m.name} #{m.needs_up? ? '' : 'APPLIED'}"}
+ end
+
+ task :load_migrations do
+ require 'dm-migrations/migration_runner'
+ Dir[File.expand_path('db/migrate/*', File.dirname(__FILE__))].each do |f|
+ require f
+ end
+ end
+
+end
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.