22 Sep 2009

Ruby on Rails Plugin: data_migration

data_migration allows you to separate data you need to load from your normal database migrations in a minimal way. While developing the new version of our flagship site, Knetwit, we decided we needed to separate our data migrations (initial settings and the like) from our structural migrations. We decided the easiest way to do this was to modify the existing Rails migration to allow for a new data migration. So we did.

Install Plugin

 
  script/plugin install git://github.com/handerson/data_migration.git

Generate Migration

 
  script/generate data_migration BlockedDomains 
  exists  db/data
  create  db/data/20090915161242_settings.rb[/sourcecode]

db/data/20090915161242_settings.rb:

 
  class BlockedDomains < ActiveRecord::Migration
    def self.up
    end
  end

Add your data:

 
  def self.up
    BlockedEmailDomain.create(:domain => "mailinator.com")
    BlockedEmailDomain.create(:domain => "spamherelots.com")
    BlockedEmailDomain.create(:domain => "disposeamail.com")
  end

Run Migration

  rake db:data:migrate
 
  ==  BlockedDomains: migrating ===========================================================
  ==  BlockedDomains: migrated (0.0020s) ==================================================

db:data:migrate adds the data migration version number to the 'schema_migrations' table so it will not be ran again.

Code at Github:
http://github.com/handerson/data_migration