Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
114 lines (83 sloc) 3.08 KB

Rails migrations in non-Rails (and non Ruby) projects.


Install Ruby, RubyGems and a ruby-database driver (e.g. gem install mysql) then:

sudo gem install standalone_migrations

Add to Rakefile in your projects base directory:

  require 'tasks/standalone_migrations' do |t|
    # t.migrations = "db/migrations"
    # t.config = "db/config.yml"
    # t.schema = "db/schema.rb"
    # t.sub_namespace = "dbname"
    # t.env = "DB"
    # t.verbose = true
    # t.log_level = Logger::ERROR
rescue LoadError => e
  puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: #{e})"

Add database configuration to db/config.yml in your projects base directory e.g.:

  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

  adapter: mysql
  encoding: utf8
  reconnect: false
  database: somedatabase_dev
  pool: 5
  username: root
  socket: /var/run/mysqld/mysqld.sock

test: &test
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

To create a new database migration:

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

... and fill in the up and down migrations Cheatsheet.

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';"

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 DB=test

To execute a specific up/down of one single migration

rake db:migrate:up VERSION=20081220234130


When working with multiple databases in a single application it is convenient to have separate sets of tasks for each database. This is accomplished with sub_namespace parameter - for example, given the following declaration: do |t|
  t.migrations = "db/migrate/widgets"
  t.sub_namespace = "widgets"

The following tasks will be created:


And migrations for this database would be created in db/migrate/widgets subdirectory.


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

Something went wrong with that request. Please try again.