# Migration and inserting default data

**URL:** <https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472>\
**Category:** Rails\
**Created:** [July 14, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472 "2006-07-14T23:17:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [July 14, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472/1 "2006-07-14T23:17:55Z")

</div>

Hi,  
I’m wondering is there a way to load default data in the migration  
script?  
So, for example I’m creating new table to store the order status, I also  
want to pre-populate the table with some data from a sql file.  
I have done a quick search on the list but cannot find anything.  
thanks,

- reynard

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [July 14, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472/2 "2006-07-14T23:40:33Z")

</div>

well actually this is just as simple as executing a command in the up  
method  
`mysql -u dev -psecret mydb_dev < db/data.sql`

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [July 15, 2006, 6:29am UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472/3 "2006-07-15T06:29:55Z")

</div>

You should actually consider using a fixture to do this so that it’s  
database-independent. There are plugins that will do this for you.

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [July 15, 2006, 6:33am UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472/4 "2006-07-15T06:33:20Z")

</div>

On 7/15/06, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On 7/14/06, Reynard H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > well actually this is just as simple as executing a command in the up method  
> > `mysql -u dev -psecret mydb_dev < db/data.sql`
> 
> You should actually consider using a fixture to do this so that it’s  
> database-independent. There are plugins that will do this for you.

I agree. I made a mistake once of putting sample data into a migration  
instead of a fixture, and the first time I reloaded the fixtures with  
rake, all the data went away. The moral? If the data is sample, put it  
in fixtures. If the data is necessary for the site, put it in both.

Sincerely,

Tom L.  
[http://AllTom.com/](http://AllTom.com/)  
[http://GadgetLife.org/](http://GadgetLife.org/)

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [July 15, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472/5 "2006-07-15T17:13:19Z")

</div>

The AR Fixture is a great plugin.  
Thanks for the suggestions.

- reynard

---

<div class="post-metadata">

**Author:** ![reynardmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@reynardmh](https://www.ruby-forum.com/u/reynardmh)\
**Post date:** [January 16, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/migration-and-inserting-default-data/98472/6 "2007-01-16T20:16:12Z")

</div>

Reynard List wrote:

> The AR Fixture is a great plugin.  
> Thanks for the suggestions.
> 
> - reynard

For what it is worth, I recently created a small helper method to load  
initialization data from fixtures into new table migrations. The code  
resides in ./lib and is invoked in the migration using

require migration\_fixture

class MyMigration \< ActiveRecord::Migration

extend MigrationFixture

def self.up  
create\_table :my\_migrations do |t|  
t.column …

```
  end

load_migration_data :my_migrations

```

end

By default the migration data is considered to reside in  
“./db/migrate/fixtures/my\_migrations.yml” but you can pass an optional  
directory parameter is you wish to have a different name or nested  
subdirectories. For example:

load\_migration\_data “my\_migations” “data/001\_create\_my\_migrations”

Here is the code:

module MigrationFixtures # Load initialization data from migration

def load\_fixture\_data(from\_fixture\_name, from\_fixture\_directory=nil)

```
# This helper module takes the fixture name and location as
# arguments and loads the data contained therein. To use from within
# a migration add 'require "migration_fixtures"' to the top of
# the migration file (before the "class" extension of ActiveRecord
# and add the line 'extend MigrationFixtures' below the "class"
# statement. You can then call the helper using:
#
# load_fixture_data(:fixture_name, :fixture_directory)
# or
# load_fixture_data(:fixture_name)
#
# The fixture name is the table name to be loaded. The
# fixture directory is relative to the migration directory and,
# if absent, the default ./fixtures is used. The fixture file
# always takes the form "table_name.yml"
#

if from_fixture_directory != nil then
  @mf_dir = File.join("../db/migrate", from_fixture_directory.to_s)
else
  @mf_dir = "../db/migrate/fixtures/"
end

directory = File.join(File.dirname( __FILE__ ),@mf_dir)
fixture_name = File.join(directory, (from_fixture_name.to_s + 

```

“.yml”))

```
# Test that fixture exists and if so, load it; otherwise skip it.
if File.exist?(fixture_name) then
  Fixtures.create_fixtures(directory, from_fixture_name)
else
  puts "No fixture #{fixture_name} exists."
end

```

end

end
