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:
begin
require 'tasks/standalone_migrations'
rescue LoadError => e
puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: #{e})"
end
Add database configuration to db/config.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
test:
...something similar...
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');"
end
def self.down
execute "delete from foo where field='something';"
end
rake db:migrate
rake db:migrate VERSION=20081220234130
rake db:migrate RAILS_ENV=test
rake db:migrate:up VERSION=20081220234130
Possible options MIGRATION_OPTIONS = {:config=>'database/config.yml', ... } gem 'standalone_migrations' require 'tasks/standalone_migrations'
This work is based on Lincoln Stoll's blog post and David Welton's post.