Migrations Outside Rails
require "rubygems"
require_gem "activerecord"
and then include a call to establish_connection to connect to the database.
At this point, I’m up and running, and I can play with all the Active Record functionality. But… I still wanted to create tables in the underlying database. In the first edition, I used DDL to do this, but in the second I wanted to use migrations.
My first hack was to use the fact that the various schema definition methods are defined both for migrations and in every database connection object. That let me use the following in my code:
ActiveRecord::Base.connection.instance_eval do
create_table children, :force => true do |t|
t.column :parent_id, :integer
t.column :name, :string
t.column :position, :integer
end
end
I was pretty chuffed with this until Jamis Buck (who else) pointed out a more elegant way:
ActiveRecord::Schema.define do
create_table children, :force => true do |t|
t.column :parent_id, :integer
t.column :name, :string
t.column :position, :integer
end
end
As I see more and more people start to use Ruby (and Active Record) as enterprise glue, being able to bring these kinds of Rails goodies to non-Rails applications is a win all around.




Dave, I've been planning on moving over to using ruby and activerecord for maintaining database schemas for work. The question I have is how do you use the migrations for adding tables instead of having everything in one script?
Posted by: Jacques Marneweck | January 29, 2007 at 04:50 PM
That's exactly what migrations do. Check our any of the online videos, or the Agile Web Development with Rails book.
Posted by: Dave Thomas | January 29, 2007 at 05:26 PM
I looked in Agile Web Development v2 and could not find any examples of using Migrations without Rails. I've been googling and so far having found any concrete descriptions on how to do it. But I may be looking in the wrong places...
So do you have any pointers how to do Migrations without Rails? Is there an example Rakefile? What else would be needed and what kind of file layout would be required? I presume a database.yml?
Thanks!
Rob
Posted by: Robert J Berger | February 18, 2007 at 04:15 PM
Robert:
That's what this blog post covers.
You just type this stuff into a file, along with the establish_connection call, and it executes to create your schema. You don't get all the 001_... versioning stuff, but that's easy to implement if you'd like.
Dave
Posted by: Dave Thomas | February 18, 2007 at 06:00 PM
Yeah it was the 001_.. versioning stuff I don't know how to do off hand. I presume it would mainly be rakefile hacking?
I did get basic activerecord migration working outside of Rails (ie standalone ruby/ActiveRecord) for my application which is creating a table for an activerecord object OdiStatusDb::Status
require 'rubygems'
require 'active_record'
require 'odi_status_db'
include OdiStatusDb
def setup_database
@dbs = YAML::load(ERB.new(IO.read("database.yml")).result)
# to slurp records into production db, change this line to production.
curr_db = @dbs["development"]
puts "curr_db: #{curr_db.inspect}"
ActiveRecord::Base.establish_connection(:adapter => curr_db["adapter"],
:host => curr_db["host"],
:database => curr_db["database"],
:username => curr_db["username"],
:password => curr_db["password"])
end
# Defines the schema for the statuses table (OdiStatusDb::Status
# ActiveRecord object)
class CreateStatusTable false
t.column :src_uuid, :string, :null => false
t.column :status_type, :string, :null => false
t.column :message, :text
end
end
# Drops the table
def self.down
drop_table :statuses
end
end
Posted by: Robert J Berger | February 19, 2007 at 02:27 AM
Dave,
Thanks so much for posting this, I found you when searching google for migration script help. I took your idea above a little further so that it can use the database.yml file if one exists - if you are interested take a look over at:
http://blog.andrewbeacock.com/2007/12/how-to-access-activerecord-migrations.html
You comments would be most welcome! :)
Andy.
Posted by: Andrew Beacock | January 02, 2008 at 02:39 AM