Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading seed data for a rails migration

I have an existing database in which I am converting a formerly 'NULL' column to one that has a default value (and populating that with said default value). However, that value is an ID of a record I need to create. If I put this record in db/seeds.rb, it won't run because db/seeds.rb runs after migrations -- but the migration needs seed data. If I leave the record creation in the migration, then I don't get the record if I make a fresh database with db:load. Is there a better way other than duplicating this in both db/seeds.rb and the migration?

Thanks!

like image 604
wickedchicken Avatar asked Aug 17 '11 23:08

wickedchicken


People also ask

How do I move seeds to rails?

Adding seed_migrations to an existing appClean your local database, and seed it, that can be done with rake db:reset. register all the models that were created in the original seeds file. run rake seed:migrate. At this point, your seeds file will be rewritten with all the create statements.

What is seeding in migration?

Seed migration is a rails gem similar to schema migrations but for data instead. Many projects rely on some kind of initial data, a list of products for an e-commerce shop, a list of post categories in a blog, or a set of user roles, for instance. A classic pattern is to keep that data in Rails' db/seeds.

What is data seeding in rails?

Rails seed files are a useful way of populating a database with the initial data needed for a Rails project. The Rails db/seeds. rb file contains plain Ruby code and can be run with the Rails-default rails db:seed task.


2 Answers

While I can understand your desire to stay DRY and not have to write this in both the migration and seeds.rb, I think you should write it in both places. Not just to make it work, but to accomplish different requirements related to your problem.

  1. You need to ensure that your migration can execute properly regardless of external processes. That means you should put any code required within that specific migration. This isn't to accomplish anything besides making sure your migration executes properly. Suppose someone else tries to migrate without knowing you put part of the code in seeds.rb, it would be very difficult for them to figure out what's going on.

  2. You can make db:load work properly by including similar code in seeds.rb. However, you should be evaluating the current state of your database in seeds.rb due to the fact that it runs after the migrations. So you can check to see if the column exists, and what the default value is etc. This means that if the migration ran and took care of everything, seeds.rb doesn't repeat work or modify values inappropriately. However, if the migration did not set these variables as expected, it is able to set the values.

I'd recommend looking at it as two separate issues so you can be more confident of each one executing successfully independent of one another. It also creates better maintainability for understanding by yourself or others of what's happening in the future.

like image 99
Matthew Avatar answered Oct 27 '22 07:10

Matthew


In my opinion you should treat this in both db/seeds.rb and the migration.

The migration is used to get an existing database from an older version to another version while seeds.rb and schema.rb are used for a fresh database with the latest version.

like image 44
Mihai A Avatar answered Oct 27 '22 08:10

Mihai A