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

July 18, 2006

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e200d834de665d53ef

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

Comments

Jacques Marneweck

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?

Dave Thomas

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

Robert J Berger

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

Dave Thomas

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

Robert J Berger

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

Andrew Beacock

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.

Heldopslippers

Ehm just a quick comment:

you mist a semicolon in the code examples. I had to change children to :children.
So the code would be:
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

The comments to this entry are closed.

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
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.