namespace :db do namespace :migrations do task :archive do considered_old = 60 # how many days ago is considered old enough puts "Looking for migrations older than #{considered_old} days" db_path = File.join(Rails.root, "db/") migrations_path = File.join(db_path, "migrate/") old_migrations_path = File.join(db_path, "old_migrations/") files = Dir[File.join(migrations_path, "/*.rb")] moved = [] files.each do |m| timestamp = m.match(%r{/(\d{14})_})[1] rescue nil next unless timestamp date = DateTime.strptime(timestamp, "%Y%m%d%H%M%S") if date <= Date.today - considered_old filename = File.basename(m) puts "Archiving #{filename}" FileUtils.mkdir_p(old_migrations_path) FileUtils.mv(m, File.join(old_migrations_path, filename)) moved << m end end if moved.count > 0 puts "Moved #{moved.count} migrations to #{old_migrations_path}" else puts "No migrations older than #{considered_old} days found" end end end end