Reset the database (purge all), then seed a database
Asked Answered
D

7

169

Is there a rake command to wipe out the data in the database tables?

How do I create a db:seed script to pre-fill data to my tables?

Deangelo answered 23/10, 2010 at 12:35 Comment(0)
M
297

I use rake db:reset which drops and then recreates the database and includes your seeds.rb file. http://guides.rubyonrails.org/migrations.html#resetting-the-database

Mcnelly answered 23/10, 2010 at 13:45 Comment(1)
Side note to this: if in Production, you might have to restart the rails server for changes to appear/pages to update.Satori
U
173

You can delete everything and recreate database + seeds with both:

  1. rake db:reset: loads from schema.rb
  2. rake db:drop db:create db:migrate db:seed: loads from migrations

Make sure you have no connections to db (rails server, sql client..) or the db won't drop.

schema.rb is a snapshot of the current state of your database generated by:

rake db:schema:dump
Unmeaning answered 14/5, 2013 at 13:48 Comment(1)
Thanks for reminding people to turn off any servers to make sure the DB gets dropped properly.Moskva
B
12

As of Rails 5, the rake commandline tool has been aliased as rails so now

rails db:reset instead of rake db:reset

will work just as well

Businessman answered 7/12, 2017 at 20:2 Comment(0)
G
8

If you don't feel like dropping and recreating the whole shebang just to reload your data, you could use MyModel.destroy_all (or delete_all) in the seed.db file to clean out a table before your MyModel.create!(...) statements load the data. Then, you can redo the db:seed operation over and over. (Obviously, this only affects the tables you've loaded data into, not the rest of them.)

There's a "dirty hack" at https://mcmap.net/q/66243/-undo-previously-seeded-data-in-rails to add a "de-seeding" operation similar to migrating up and down...

Godless answered 5/3, 2016 at 0:20 Comment(1)
clever, recreating everything from scratch everytime took to long for meZeeba
B
8

on Rails 6 you can now do something like

rake db:seed:replant This Truncates tables of each database for current environment and loads the seeds

https://blog.saeloun.com/2019/09/30/rails-6-adds-db-seed-replant-task-and-db-truncate_all.html

$ rails db:seed:replant --trace
** Invoke db:seed:replant (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Invoke db:truncate_all (first_time)
** Invoke db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke db:load_config
** Execute db:check_protected_environments
** Execute db:truncate_all
** Invoke db:seed (first_time)
** Invoke db:load_config
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke db:load_config
** Execute db:abort_if_pending_migrations
** Execute db:seed:replant
Belayneh answered 9/9, 2020 at 19:10 Comment(3)
Great solution - far more simple and less intrusive then having to drop the whole db!Abib
I am glad that this was able to help. I think this is the way that it should be done in 2020, but again this original question was asked in 2010Belayneh
Given it's a Rails 6 feature - it would appear the rails devs agree with you 😎Abib
E
0

You can use rake db:reset when you want to drop the local database and start fresh with data loaded from db/seeds.rb. This is a useful command when you are still figuring out your schema, and often need to add fields to existing models.

Once the reset command is used it will do the following: Drop the database: rake db:drop Load the schema: rake db:schema:load Seed the data: rake db:seed

But if you want to completely drop your database you can use rake db:drop. Dropping the database will also remove any schema conflicts or bad data. If you want to keep the data you have, be sure to back it up before running this command.

This is a detailed article about the most important rake database commands.

Egan answered 29/3, 2018 at 14:13 Comment(0)
P
0

Please follow below commands:

  1. rails db:migrate:reset

  2. rails db:seed again

Penthouse answered 28/1, 2022 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.