/standalone-migrations

A thin wrapper to use Rails Migrations in non Rails projects

Primary LanguageRuby

STANDALONE MIGRATIONS
---------------------
This project allows you to use Rails migrations in non-Rails (and non Ruby) projects. I created it because not all of my projects are on Rails but I still think Rails migrations are the best way to manage a database schema. For this code to work you need Ruby, Gems, ActiveRecord, Rake and a suitable database driver installed.

Email me at todd dot huss dot work at gmail dot com if you have a patch that adds functionality or fixes a bug. Or post a comment here:
http://gabrito.com/post/standalone-migrations-using-rails-migrations-in-non-rails-projects

TODO
----
Turn into a gem

USAGE
-----
To create a new database migration run:

rake db:new_migration name=FooBarMigration
edit migrations/20081220234130_foo_bar_migration.rb

and fill in the up and down migrations. To apply your newest migration

rake db:migrate

To migrate to a specific version (for example to rollback)

rake db:migrate VERSION=20081220234130

To migrate a specific database (for example your "testing" database)

rake db:migrate RAILS_ENV=test

CREDIT
------
This work is based on Lincoln Stoll's blog post: http://lstoll.net/2008/04/stand-alone-activerecord-migrations/
and David Welton's post http://journal.dedasys.com/2007/01/28/using-migrations-outside-of-rails

INSTALLATION
------------
Many systems have pre-existing packages for Ruby and Ruby gems so in most cases that should be pretty straightforward. Once Ruby and Gem is installed, as root run:
gem install -y activerecord rake mysql

cd standalone-migrations/config
cp database_sample.yml database.yml
edit database.yml (and change settings for your database, the development database is the default)
cd ../
rake db:migrate (this will just create the schema_migrations table)
(If you're familiar with rails there are also the tasks db:schema:dump and db:schema:load)

DOCUMENTATION
-------------
A good source to learn how to use migrations is:
http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations
or if you're lazy and want to just execute raw SQL

def self.up
  execute "insert into foo values (123,'something');"
end

def self.down
  execute "delete from foo where field='something';"
end