Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

65 lines (49 sloc) 1.854 kb

Rails migrations in non-Rails (and non Ruby) projects.
For this code to work you need Ruby, Gems, ActiveRecord, Rake and a suitable database driver installed.


Install Ruby, RubyGems then: sudo gem install standalone_migrations

Add to Rakefile in your projects base directory: begin require 'standalone_migrations' StandaloneMigrations.tasks rescue LoadError puts 'gem install standalone_migrations to get db:migrate:* tasks!' end

Add database configuration to config/database.yml in your projects base directory e.g.: development: adapter: mysql encoding: utf8 reconnect: false database: somedatabase_dev pool: 5 username: root password: socket: /var/run/mysqld/mysqld.sock

  ...something similar...

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


This work is based on Lincoln Stoll's blog post:
and David Welton's post


A good source to learn how to use migrations is: or if you're lazy and want to just execute raw SQL

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

def self.down
  execute "delete from foo where field='something';"
Jump to Line
Something went wrong with that request. Please try again.