Blog of Heath

Ruby on Rails Plugin: data_migration

Posted in Knetwit, Ruby on Rails by heathanderson on September 22, 2009

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

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

2 Responses

Subscribe to comments with RSS.

  1. Kyle Lau said, on September 22, 2009 at 3:23 am

    Yeah,very nice! There is the same mind in rails 2.3.4 ,you can try it , add data in the file db/seed.rb , then run rake db:seed .

  2. heathanderson said, on September 26, 2009 at 9:29 pm

    Thanks. I noticed the seed feature in 2.3.4 as I was converting this to a plugin. I should have thought to mention it in the post–and maybe I’ll edit the post with the info soon. Thanks again for bringing it up and for being the first commenter on my blog!


Leave a comment