Jesse Novotny
Jesse Novotny

Reputation: 730

How to clear database before seeding in rails

Is there a simple line of code I can include at the top of my seed file to clear out each table before inserting new seed data without running any rake commands to rollback tables or databases?

I was thinking something like:

[Foo, Bar].each{|class| class.destroy_all}

The point is I want to add new data where each new insertion starts from id: 1. What I want to avoid is deleting a table with 100 rows and when I add new data it's starting from 101.

Upvotes: 5

Views: 4655

Answers (7)

supernikio2
supernikio2

Reputation: 15

As of Rails 6, you can run

rails db:seed:replant

which will truncate the database before seeding.

Upvotes: 1

Without removing(dropping) database:

rollback all migrations, for example if they < 1000 and after migrate and seed

STEP=1000 rails db:rollback && rails db:migrate && rails db:seed

or

rails db:purge && rails db:migrate && rails db:seed

With removing(dropping) database:

remove(drop) database + create database + migrate + seed

rails db:reset

or

rails db:drop && rails db:create && rails db:migrate && rails db:seed

Upvotes: 0

Ian Purton
Ian Purton

Reputation: 15891

In my Rails 5 environment.

rails db:purge
rails db:migrate
rails db:seed

Or all together.

rails db:purge db:migrate db:seed

Upvotes: 2

Vishal Nagda
Vishal Nagda

Reputation: 1165

Updated Answer

You've to install (OR you can add gem 'database_cleaner' to your Gemfile) a GEM called Database Cleaner which helps to clean your database without affecting your database schema._

To clean your database each time whenever you do rake db:seed then paste

# updated
require 'database_cleaner'

DatabaseCleaner.clean_with(:truncation)

on the top of your seed file. It'll clear your database and start count from 1 again.


Disclaimer : This updated answer is tested, and working perfectly in my system.



===========================================================================

Previous Untested Answer

Ya you can do that but it's depends on which database you're using.

Below I'm giving solution for some popular DBs.

In MySQL, TRUNCATE table; deletes all rows and resets the auto increment counter.

In PostgreSQL, it does not do this automatically. You can use TRUNCATE TABLE table RESTART IDENTITY;.

In SQLite, there is no TRUNCATE statement, instead, it's

DELETE FROM table;
DELETE FROM sqlite_sequence WHERE name='table';


You can also try this

ActiveRecord::Base.connection.tables.each do |table|
  ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
end


You can select any one of the solution & implement it to your seed file.

I hope this will help you... ;)


Disclaimer : I've share my knowledge for the purpose of your help, but this solution was didn't tested.

Upvotes: 11

Jesse Novotny
Jesse Novotny

Reputation: 730

After doing some more research on this topic, I found the 'database_cleaner' gem. After installing, I simply added DatabaseCleaner.clean_with(:truncation) at the top of my seed.rb and Bingo! It seems to have worked locally running SQLite and with PostgreSQL on Heroku. Thanks for all the input everyone! :)

Upvotes: 0

Deepak Mahakale
Deepak Mahakale

Reputation: 23671

You can use this command

rake db:reset

Upvotes: 3

Charan Kumar Borra
Charan Kumar Borra

Reputation: 282

Before doing the rake db:seed.

rake db:purge

has recently been added to ActiveRecord in the master branch of rails 4.2.0.alpha

https://github.com/rails/rails/commit/e2f232aba15937a4b9d14bd91e0392c6d55be58d

Upvotes: 0

Related Questions