Migration and inserting default data

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

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.

On 7/15/06, Brian H. [email protected] wrote:

On 7/14/06, Reynard H. [email protected] 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://GadgetLife.org/

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

  • reynard

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs