views:

203

answers:

1
rake db:schema:dump

This command gives you the schema of a legacy database and you can build a migration for that database off the generated schema.

But if that database had data in it, it would be nice if there was a rake command to retrieve the data in a migration file generated by Rails.

Perhaps I'm dreaming - it's probably asking too much to think that Rails could look at the data in the legacy database and construct a migration for you from the existing data - something like this:

class LoadDefaultData < ActiveRecord::Migration
  def self.up
    bopeep = User.find_by_username 'bopeep'
    BlogPost.create(:title => 'test', :content => 'test', :author_id => bopeep.id, :status => 'ok')
  end

  def self.down
  end
end

Or is there a way?

+4  A: 

Tobias Lütke needed to migrate a database from one architecture to another but needed to copy the data across intact. To ensure that the format would be architecture agnostic, he's created a plugin that dumps the data to YAML and then reloads it back into the database at the other end. It's a simple rake task, and Tobi gives quick and easy instructions.

http://blog.leetsoft.com/2006/5/29/easy-migration-between-databases

This may help.

You may also like to look at YamlDB plugin http://opensource.heroku.com/ which can back up from one db type and restore to another. Here are some instructions

Corban Brook