Skip to content

Instantly share code, notes, and snippets.

@duderman
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duderman/fb1e7b4d670d5c33f807 to your computer and use it in GitHub Desktop.
Save duderman/fb1e7b4d670d5c33f807 to your computer and use it in GitHub Desktop.
Loads all migrations
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