Intial data in Migrations


#1

One thing I found annoying about Migrations, besides that you have a
very limited ways to define your data, is that it will overwrite
EVERYTHING in your schema file. So if you spend hours customizing your
initial schema file, to get it work, exactly like you’ll need it, that
work immediately becomes for nothing once you run a migration. Your
entire schema will get rewritten, not appended to.

In that case, with migrations, how is one suppose to handle initial
data, such a look up table (such as state or country). Do those get
put into a separate file? something like an initialize_db.rb?

How have others been able to handle this with migrations?


Sean W.
master nerd of
i heart squares, Co.

3711 N. Ravenswood Ave. #147 Chicago, IL 60613
Ph. (773) 531-6301 Fx. (773) 529-7041
http://www.iheartsquares.com


#2

db/migrate:
001_create_schema.rb
002_load_units.rb
003_load_settlement_states.rb


– Tom M.


#3

On 11 Feb 2006, at 1:03 am, Tom M. wrote:

entire schema will get rewritten, not appended to.
003_load_settlement_states.rb
Or, in other words, don’t edit schema.rb directly. Everything that
you were putting in schema.rb by hand, put into your first migration
instead. It should be your first migration that sets up the database
from scratch.

As it says at the top of schema.rb:

This file is autogenerated. Instead of editing this file, please

use the

migrations feature of ActiveRecord to incrementally modify your

database, and

then regenerate this schema definition.

Chris


#4

Chris M. <chris@…> writes:

How have others been able to handle this with migrations?
See below for a plugin I’ve been working on.

Or, in other words, don’t edit schema.rb directly.
Agreed.

It isn’t fully baked yet, but I have a plugin that does this. It’s
called the
“first_migration” plugin.

Here’s how you install it:

script/plugin source http://damonclinkscales.com/svn/plugins/
script/plugin install first_migration

Here’s how you run it:
script/generate first_migration

USAGE: generate first_migration (equal to generate first_migration
InitialSchema ruby)
generate first_migration ClassName sql (runs
db_structure_dump)
generate first_migration ClassName sql [file1.sql] (just uses
db/file1.sql instead)
generate first_migration ClassName ruby (runs db_schema_dump)
generate first_migration ClassName ruby [file1.rb] (just uses
db/file1.rb instead)

What it does:

It builds a migration file from one of four sources:

  1. your db as a Rails structure dump
  2. your db as a Rails schema dump
  3. your db as a sql file (custom)
  4. your db as a ruby schema file (custom)

Please send me feedback, as I am still improving it to handle all
scenarios.

Back up your existing data because it doesn’t quite handle all
situations with
existing data quite yet.

Best,

-damon
http://damonclinkscales.com/