# Thanks to Manu, www.manu-j.com/blog/truncate-all-tables-in-a-ruby-on-rails-application/221/

namespace :db do

desc "Truncates tables"
task(:truncate => :load_config) do
  begin
    config = ActiveRecord::Base.configurations[RAILS_ENV]
    ActiveRecord::Base.establish_connection

    case config["adapter"]
    when "mysql", "mysql2"
      ActiveRecord::Base.connection.tables.each do |table|
        ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
      end
    when "sqlite", "sqlite3"
      ActiveRecord::Base.connection.tables.each do |table|
        ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
        ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence where name='#{table}'")
      end
      ActiveRecord::Base.connection.execute("VACUUM")
    end
  rescue
    $stderr.puts "Error while truncating. Make sure you have a valid database.yml file and have created the database tables before running this command. You should be able to run rake db:migrate without an error"
  end
end

end