Search code examples
ruby-on-railspostgresqlrake

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.


Solution

  • 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