Last active
August 29, 2015 14:09
-
-
Save duderman/fb1e7b4d670d5c33f807 to your computer and use it in GitHub Desktop.
Loads all migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
files = Dir.glob("db/migrate/*") | |
timestamps = files.collect{|f| f.split("/").last.split("_").first} | |
timestamps.count | |
migrations = ActiveRecord::Base.connection.execute("SELECT version FROM schema_migrations", as: :array).to_a.flatten | |
needs = timestamps - migrations | |
files_needs = files.select { |f| needs.include? f.split("/").last.split("_").first } | |
files_needs.sort.each do |f| | |
begin | |
require "#{Rails.root}/#{f}" | |
f.split('/').last.split('_')[1..-1].join('_').split('.').first.camelize.constantize.new.up | |
rescue => e | |
end | |
end | |
sql = "INSERT INTO `schema_migrations` (`version`) VALUES #{needs.map{|n|"('#{n}')"}.join(',')} ON DUPLICATE KEY UPDATE `version`" | |
ActiveRecord::Base.connection.execute(sql) rescue nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment