Using Migrations Outside of Rails

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
version = nil

@logger = $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…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s