London Calling →← DHH on PHP

Import initial data into database during rails migration May 2nd, 2008

Let’s say you’re migrating a country names “static” table. In this case it makes sense to execute the 250-ish INSERT INTO country... queries to populate the database during the migration.

Here’s how to do, and it’s quite simple. In your migration file:

def self.up
    create_table :countries do |t|
      t.column :iso,                   :string, :limit => 2
      t.column :name,                  :string
      t.column :printable_name,        :string
      t.column :iso3,                  :string, :limit => 3
      t.column :numcode,               :string, :limit => 3
    end    
    result = `sudo mysql webgal_development < db/sql_countries.sql`   
  end
/ Tags: Ruby on Rails, Code, Web Trackback