Thursday, July 30, 2009

Seed data into development database using Bootstrapper and Factory Girl

There's only so much development an application using unit tests and cucumber before we need to fire up the application on the browser in development mode. Trouble is we've written so many associated models and there did not contain any data in development database.

So comes seeding of data into my development database. I could do one of the following:

- Manually do SQL insertions of records into database
- Manually create and save objects in "script/console"
- Setup rake task to create records
- Use migration to insert seed data
- Use a SystemSettings model to create objects
- Use plugins like Seed_Fu and object creations
- Use plugins like Seed_Fu and fixtures
- Use Bootstrapper plugin with Factory Girl

Since I'm already using Factory Girl heavily with shoulda and cucumber, it makes sense to use them for my database seeding.

Bootstrapper gives nice convenience methods like

rake db:bootstrap
rake db:bootstrap:reset

You can specify different environments (test, development, staging, production, etc.) and run seeding modularly within the db/bootstrap.rb file

Bootstrapper.for :development do |b|
b.truncate_tables :addresses
b.run :users

Factory(:us_address, :state => "ME")
Factory(:us_address, :state => "IL")
Factory(:us_address, :state => "CA")
end

Bootstrapper.for :production do |b|
end

Bootstrapper.for :test do |b|
end

Bootstrapper.for :staging do |b|
end

Bootstrapper.for :users do |b|
3.times{ Factory(:user) }
Factory(:user, :login => "admin",
:password => "sekret",
:password_confirmation => "sekret")
end

You can then specify the bootstrapping that you wanna make

rake db:bootstrap BOOTSTRAP=users
rake db:bootstrap RAILS_ENV=production
rake db:bootstrap BOOTSTRAP=users RAILS_ENV=production

No comments:

Blog Archive