A while ago, PragDave wrote about using Rails migrations outside of Rails.
I wanted to be able to have things work ‘just like in rails’, so I hacked up a quick script that runs all the migrations in the files/
directory:
require 'rubygems'
require_gem 'activerecord'
if ARGV[0] =~ /VERSION=d+/
version = ARGV[0].split('=')[1].to_i
else
version = nil
end
@logger = Logger.new $stderr
ActiveRecord::Base.logger = @logger
ActiveRecord::Base.colorize_logging = false
ActiveRecord::Base.establish_connection(:adapter => "mysql",
:username => "dbuser",
:host => "localhost",
:password => "xxx",
:database => "yyy")
ActiveRecord::Migrator.migrate("files/", version)
It would be even nicer to have a config file, but that should be very easy to do with YML.
I run it like so: ruby domigrations.rb
. I believe that the Migrator.migrate
call isn’t documented, so I don’t know how safe it is to count on it, but for the moment things seem to work quite nicely…