Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice for updating rails seed data

I have a rails application in production with seed data. We need to add more seed data, but using rake db:populate will replicate all the old seed data and of course we don't want to add the data to migrations.

What is the best method of adding extra seed data to the application?

like image 688
Joel Jackson Avatar asked Dec 22 '11 01:12

Joel Jackson


People also ask

What is data seeding in rails?

What is Seed in Rails ? A useful way of populating a database with the initial data needed for a Rails project. This will allow us to populate the database in within our Rails Web application.

How do I move a seed in rails?

Create a migration with rails g seed_migration NameOfMigration . That will create a new file under db/data . Write your changes in the up and down methods, exactly like you would do for a regular migration. For example, we have marked our Product , TaxRate and ShippingType models as seed data.

What is Seeds rb?

The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using rake -T in your project directory shows information about following tasks: rake db:seed. Load the seed data from db/seeds.rb. rake db:setup.

What does Rails DB Reset do?

db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop , db:create , db:migrate tasks. db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran.


2 Answers

Stand on the Shoulders of Giants

Take a look at the SeedFu gem.

It allows you to create a seed file like this, that automatically links to one, or more, columns:

User.seed(:id,
  { id: 1, login: 'jon',   email: '[email protected]',   name: 'Jon'   },
  { id: 2, login: 'emily', email: '[email protected]', name: 'Emily' }
)

You can also update these seed files and it will handle updating the DB values.

This, in combination with Seedbank, is what I ended up using.

like image 54
Joshua Pinter Avatar answered Sep 18 '22 19:09

Joshua Pinter


Your probably going to have to build another rake task. Or you could just do checks on each new row to see if it already exists. It may take more time to run like that, but at least you won't have duplicates.

like image 35
thatmiddleway Avatar answered Sep 19 '22 19:09

thatmiddleway