5

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.

Jesse Novotny
  • 704
  • 7
  • 16
  • which DB you're using ? – Vishal Nagda Sep 22 '16 at 19:57
  • I'm using SQLite locally and PostgreSQL through Heroku. This is my first SO question... how do I mark it answered? Also, after doing some more research, I found the database_cleaner gem whereby I just added `DatabaseCleaner.clean_with(:truncation)` at the top of my seed file and Bingo! :) Thanks for the help Vishal! – Jesse Novotny Sep 23 '16 at 18:01
  • [How to mark question as answered](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Vishal Nagda Sep 24 '16 at 11:36

7 Answers7

11

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.

VAD
  • 2,351
  • 4
  • 20
  • 32
Vishal Nagda
  • 1,165
  • 15
  • 20
2

You can use this command

rake db:reset
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
2

In my Rails 5 environment.

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

Or all together.

rails db:purge db:migrate db:seed
Ian Purton
  • 15,331
  • 2
  • 27
  • 26
1

As of Rails 6, you can run

rails db:seed:replant

which will truncate the database before seeding.

supernikio2
  • 15
  • 1
  • 5
0

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

  • Not sure if this resets the counts for new ids, but this is how to do it for sqlite3: `sql = "delete from sqlite_sequence where name = '#{table}'";ActiveRecord::Base.connection.execute(sql)` – Teddy Sep 22 '16 at 18:16
0

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! :)

Jesse Novotny
  • 704
  • 7
  • 16
0

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
shilovk
  • 11,718
  • 17
  • 75
  • 74