Skip to content

Instantly share code, notes, and snippets.

@conorh
Created April 19, 2016 19:40
Show Gist options
  • Save conorh/2fda6817b8559883ca4f482f2bb94bf4 to your computer and use it in GitHub Desktop.
Save conorh/2fda6817b8559883ca4f482f2bb94bf4 to your computer and use it in GitHub Desktop.
Rake task to archive old migrations
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment