Rake task to truncate all tables in Rails 3
I've found this via google, and then I got a much simpler solution than the one approved, so here it is: Use the database_cleaner gem. Here're the steps.
In your Gemfile (execute bundle after modifying):
gem 'database_cleaner' # you might want to limit this to the dev and staging group
With that gem in place, the statement DatabaseCleaner.clean_with :truncation
will truncate the database. Adding it to a rake task is trivial:
# tasks/db/clean.rake
namespace :db do
desc "Truncate all existing data"
task :truncate => "db:load_config" do
DatabaseCleaner.clean_with :truncation
end
end
That's it. You can also use the DatabaseCleaner.clean_with :truncation
line inside your db/seeds.rb
file directly so that you don't forget to truncate the database before seeding.
rake db:reset Drop all tables but not database
This is the solution I eventually came up with after looking at the Truncate method.
namespace :db do
desc "Erase all tables"
task :clear => :environment do
conn = ActiveRecord::Base.connection
tables = conn.tables
tables.each do |table|
puts "Deleting #{table}"
conn.drop_table(table)
end
end
end
How to delete all data from all tables in Rails?
rake db:reset
It recreates your table from migrations.
As suggested in the comments, a faster way to do it (but you have to add a new rake task) is:
namespace :db do
desc "Truncate all tables"
task :truncate => :environment do
conn = ActiveRecord::Base.connection
tables = conn.execute("show tables").map { |r| r[0] }
tables.delete "schema_migrations"
tables.each { |t| conn.execute("TRUNCATE #{t}") }
end
end
Response copied from: answer on SO.
Delete all records from all tables in database using seeds.rb
Is there a command that does exactly what I want: deletes all the
records from all the tables?
bundle exec rake db:reset
This is functionally equivalent to rake db:drop db:setup
.
Don't want delete the tables?
#app/db/seeds.rb
[Blog, Person, Post, User, Author, Book].each do |table|
ActiveRecord::Base.connection.execute("TRUNCATE #{table.table_name}")
end
SQL-TRUNCATE
Rails Rake Task drop table and import from csv
Try by truncating the table by running a custom sql command:
namespace :csvimportproducts do
desc "Import Products CSV Data."
task :import_products_csv_data => :environment do
ActiveRecord::Base.connection.execute("TRUNCATE TABLE products")
require 'csv'
csv_file_path = '/home/jay/workspace/db/import_tables/products.csv'
CSV.foreach(csv_file_path) do |row|
p = Product.create!({
:product_id => row[0],
:product_name => row[1],
}
)
end
end
end
Want to re-create table when running rake db:seed
rake db:reset
may be what you are looking for. or...
rake db:drop
rake db:create
Related Topics
Oracle SQL "Select Date from Datetime Field "
Field Value Must Be Unique Unless It Is Null
Select Statement in Sqlite Recognizing Row Number
Returning The First X Records in a Postgresql Query with a Unique Field
How to Retrieve a Very Long Xml-String from an SQL Database with R
How to Return Rows from a Declare/Begin/End Block in Oracle
Joining Two Separate Queries in a Postgresql ...Query... (Possible or Not Possible)
How to Pass Two SQL Tables as Input Parameter for R Codes in SQL Server
How to Do a SQL Update in Batches, Like an Update Top
Update Statement Using Join and Group By
Blank Out Duplicate Column Values in SQL Reporting Services
Ruby on Rails Where Query with Nesting Relations
How to Get Array/Bag of Elements from Hive Group by Operator