« Decimal Support in Rails | Main | Rails and the Legacy World »

July 18, 2006

Migrations Outside Rails

I’m about 3 weeks into the rewrite of the Active Record chapters for the new Rails book. In the book, I try to demonstrate Active Record with real, live code. At the same time, I don’t want to run every single piece of code in the context of a Web application. So, I use Active Record stand-alone, without having the rest of Rails loaded. All my demonstration files start:
    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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2226312/7670398

Listed below are links to weblogs that reference Migrations Outside Rails:

Comments

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?

That's exactly what migrations do. Check our any of the online videos, or the Agile Web Development with Rails book.

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

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

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

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.

Post a comment

If you have a TypeKey or TypePad account, please Sign In

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now in beta
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave