Place this in the rails root of your project. And then call
ruby rails_migration_checker.rb <path/to/migration 1> [<path/to/migration 2>...]
#!/usr/bin/env ruby | |
module Wrapper | |
def execute(sql, *args) | |
puts " > " + sql.gsub(/`/, '') + ";" | |
super | |
end | |
end | |
require_relative 'config/environment' | |
ActiveRecord::Base.connection | |
ActiveRecord::Migration.verbose = false | |
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Wrapper) | |
queue = [] | |
while path = ARGV.shift | |
require_relative path | |
filename = File.basename(path, ".rb") | |
timestamp, name = filename.split("_", 2) | |
name = name.camelcase | |
migration = name.constantize | |
schema_change = ActiveRecord::SchemaMigration.new(:version => timestamp.to_s) | |
queue.append([ | |
migration, | |
name, | |
timestamp, | |
schema_change, | |
]) | |
end | |
puts | |
puts "====== UP =======" | |
queue.each do |(migration, name, timestamp, schema_change)| | |
puts "^ #{timestamp}_#{name}" | |
migration.migrate(:up) | |
schema_change.save | |
end | |
puts | |
puts "====== DOWN =======" | |
queue.reverse_each do |(migration, name, timestamp, schema_change)| | |
puts "v #{timestamp}_#{name}" | |
migration.migrate(:down) | |
schema_change.destroy | |
end |