Rails migrations in non-Rails (and non Ruby) projects.
In the 1.0 release we have moved to using Rails 3 migrations instead of maintaining our own migration related code. Just about anything you can do with Rails 3 migrations you can now do with Standalone Migrations too! This removed 95% of the code we have to maintain. Big thanks to Michael Grosser for undertaking this major rewrite!
Standalone Migrations relies on the contributions of the open-source community! To submit a fix or an enhancement fork the repository, checkout the develop branch, make your changes, add your name to the Contributors section in README.markdown, and send us a pull request! If you're active and do good work we'll add you as a collaborator!
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: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql
encoding: utf8
reconnect: false
database: somedatabase_dev
pool: 5
username: root
password:
socket: /var/run/mysqld/mysqld.sock
test: &test
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
rake db:new_migration name=FooBarMigration
edit db/migrate/20081220234130_foo_bar_migration.rb
... and fill in the up and down migrations Cheatsheet.
def self.up
execute "insert into foo values (123,'something');"
end
def self.down
execute "delete from foo where field='something';"
end
The general form is:
rake db:generate model="model_name" fields="type:column_name0 type:column_name1 ... type:column_namen"
You can have as many fields as you would like.
An example to create a Person table with 3 columns (and it will automatically add the t.timestamps line)
rake db:generate model="Person" fields="string:first_name string:last_name integer:age"
This will create a migration in db/migrate/
class CreatePerson < ActiveRecord::Migration
def self.up
create_table :Person do |t|
t.string :first_name
t.string :last_name
t.integer :age
t.timestamps
end
end
def self.down
drop_table :Person
end
end
rake db:migrate
rake db:migrate VERSION=20081220234130
rake db:migrate DB=test ... or ...
rake db:migrate RAILS_ENV=test
rake db:migrate:up VERSION=20081220234130
rake db:rollback
rake db:rollback STEP=3
By default, Standalone Migrations will assume there exists a "db/" directory in your project. But if for some reason you need a specific directory structure to work with, you can use a configuration file named .standalone_migrations in the root of your project containing the following:
db:
seeds: db/seeds.rb
migrate: db/migrate
schema: db/schema.rb
config:
database: db/config.yml
These are the configurable options available. You can omit any of the keys and Standalone Migrations will assume the default values.
- Todd Huss
- Two Bit Labs
- Michael Grosser
- Eric Lindvall
- Steve Hodgkiss
- Rich Meyers
- Wes Bailey
- Robert J. Berger
- Federico Builes
- Ricardo Valeriano
This work is originally based on Lincoln Stoll's blog post and David Welton's post.