Browse files

Improve migration handling

  • Loading branch information...
1 parent 0188add commit 1bfb16eec8b4172ee209a6733b01a1bca6301e10 @tpope committed May 9, 2011
Showing with 19 additions and 13 deletions.
  1. +19 −13 lib/hookup.rb
View
32 lib/hookup.rb
@@ -85,22 +85,28 @@ def migrate(old, new, *args)
end
migrations = %x{git diff --name-status #{old} #{new} -- db/migrate}.scan(/.+/).map {|l| l.split(/\t/) }
-
- migrations.select {|(t,f)| %w(D M).include?(t)}.reverse.each do |type, file|
- begin
- system 'git', 'checkout', old, '--', file
- system 'rake', 'db:migrate:down', "VERSION=#{File.basename(file)}"
- ensure
- if type == 'D'
- system 'git', 'rm', '--force', '--quiet', '--', file
- else
- system 'git', 'checkout', new, '--', file
+ begin
+ migrations.select {|(t,f)| %w(D M).include?(t)}.reverse.each do |type, file|
+ begin
+ system 'git', 'checkout', old, '--', file
+ unless system 'rake', 'db:migrate:down', "VERSION=#{File.basename(file)}"
+ raise Error, "Failed to rollback #{File.basename(file)}. Consider rake db:setup"
+ end
+ ensure
+ if type == 'D'
+ system 'git', 'rm', '--force', '--quiet', '--', file
+ else
+ system 'git', 'checkout', new, '--', file
+ end
end
end
- end
- if migrations.any? {|(t,f)| %w(A M).include?(t)}
- system 'rake', 'db:migrate'
+ if migrations.any? {|(t,f)| %w(A M).include?(t)}
+ system 'rake', 'db:migrate'
+ end
+
+ ensure
+ system 'git', 'checkout', '--', 'db/schema.rb' if migrations.any?
end
end

0 comments on commit 1bfb16e

Please sign in to comment.