Permalink
Browse files

rake db:migrate:status displays status of migrations [#4947 state:res…

…olved]

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
1 parent fb7715b commit 8e3e117dbe58fd4fedbb78a3d8e398ab0e415634 @kevinskoglund kevinskoglund committed with NZKoz Jun 23, 2010
Showing with 25 additions and 0 deletions.
  1. +25 −0 activerecord/lib/active_record/railties/databases.rake
@@ -171,6 +171,31 @@ namespace :db do
ActiveRecord::Migrator.run(:down, "db/migrate/", version)
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
+
+ desc "Display status of migrations"
+ task :status => :environment do
+ config = ActiveRecord::Base.configurations[Rails.env || 'development']
@fxn

fxn Jun 29, 2010

Owner

How could Rails.env be undefined?

+ db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM schema_migrations")
+ file_list = []
+ Dir.foreach(File.join(Rails.root, 'db', 'migrate')) do |file|
+ # only files matching "20091231235959_some_name.rb" pattern
+ if match_data = /(\d{14})_(.+)\.rb/.match(file)
+ status = db_list.delete(match_data[1]) ? 'up' : 'down'
+ file_list << [status, match_data[1], match_data[2]]
+ end
+ end
+ # output
+ puts "\ndatabase: #{config['database']}\n\n"
+ puts "#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration Name"
+ puts "-" * 50
+ file_list.each do |file|
+ puts "#{file[0].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
+ end
+ db_list.each do |version|
+ puts "#{'up'.center(8)} #{version.ljust(14)} *** NO FILE ***"
+ end
+ puts
+ end
end
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'

5 comments on commit 8e3e117

Member

lukaszx0 replied Jun 28, 2010

I think it would be good to mention this in changelog - http://gist.github.com/455624

Contributor

anildigital replied Jun 28, 2010

nice!

Cool! I like it! :)

Contributor

dmathieu replied Jun 29, 2010

Awesome ! :)

Its really cool :)

Please sign in to comment.