rake db:migrate:status displays status of migrations [#4947 state:resolved]

Signed-off-by: Michael Koziarski <michael@koziarski.com>
This commit is contained in:
Kevin Skoglund 2010-06-23 12:45:23 -04:00 committed by Michael Koziarski
parent fb7715b249
commit 8e3e117dbe
1 changed files with 25 additions and 0 deletions

View File

@ -171,6 +171,31 @@ namespace :db do
ActiveRecord::Migrator.run(:down, "db/migrate/", version) ActiveRecord::Migrator.run(:down, "db/migrate/", version)
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end end
desc "Display status of migrations"
task :status => :environment do
config = ActiveRecord::Base.configurations[Rails.env || 'development']
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 end
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).' desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'