Skip to content

Latest commit

 

History

History
58 lines (35 loc) · 2.38 KB

README.markdown

File metadata and controls

58 lines (35 loc) · 2.38 KB

What?

Seed Migrations allow you treat seed data like migration data:

  • each seed is imported only once
  • seeds are imported in a specific order

A rake db:seed command is provided for pre-2.3.4 Rails users.

Why?

db-populate and seed-fu seemed overkill (though create_or_update is neat). Seed-fu didn't appear to support ordering the imports, which makes handling FK relations complicated (i.e. enabling and disabling all constraints.)

While I was flip flopping like Al Gore, I found myself in a situation where the code had to be custom. I saw a post by Mike Gunderloy on A Fresh Cup referring to seed data support in Rails 3. A quick look revealed it to not be much of a feature, but a basic rake task to run a seeds.rb file which just happens to contain seed data (or not.)

Anything to see here?

The only vaguely cool feature is a migration-style record of applied seed data imports. This allows you to run the db:seed as many times as you like without repeating inserts.

Installation

  • script/plugin git://github.com/n3bulous/seed_migrations.git
  • Copy db/seeds.rb to your Rails' db directory.

Obviously, if you already have a db/seeds.rb you'll have to migrate. The most simple way to achieve this is:

  • script/generate seed original_seeds
  • copy the current seeds.rb file into it
  • prepend the seed file name (sans the .rb extension) to db/seeds/seed_order.yaml
  • if the existing seeds can't be re-run, insert the seed filename into the seedings table before running rake db:seed for the first time.

Usage

  • ./script/generate seed <a-descriptive-name>
  • Add Ruby and Rails code to the generated file in the db/seeds directory, e.g.:
  cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
  Mayor.create(:name => 'Daley', :city => cities.first)
  • rake db:seed

The entries in seed_order.yaml are used as the key in the seedings table that keeps track of which ones have been inserted.

Future

  • db:seed:reset/up/down but this might be overkill.
  • automate migrating from an pre-existing seeds.rb file.

Thanks

License

Caveat Emptor