Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake db:reset Drop all tables but not database

I need to drop all the tables in my database without dropping the database because the user for this database does not have create database privileges.

What is the best way to drop all the tables but not the actual database?

Also, we use rake db:seed to add some entries into one of the tables so I don't want to use a seed file.

like image 538
Phillip Boushy Avatar asked Apr 22 '14 17:04

Phillip Boushy


2 Answers

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
like image 145
Phillip Boushy Avatar answered Oct 06 '22 00:10

Phillip Boushy


for testing, i would suggest using:

rake db:test:prepare

It will re-generate all your tables based on your db/schema.rb

like image 44
manu29.d Avatar answered Oct 06 '22 00:10

manu29.d