Skip to content

Instantly share code, notes, and snippets.

@samuelpismel
Last active December 14, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelpismel/5066468 to your computer and use it in GitHub Desktop.
Save samuelpismel/5066468 to your computer and use it in GitHub Desktop.
Truncates all tables in your rails database except the schema_migrations table. how to use: rake db:truncate
# how to use:
# rake db:truncate
#
# Truncates all tables in your rails database except the schema_migrations table.
# This is a destructive rake task, it will delete all records in your database,
# so don't use it if you don't know what you are doing and be careful when using it.
# I recommend use it just when you have a really good seed.rb in your project
# as a easy way to clean the database and then seed the database with rake db:seed command.
# Como usar:
# rake db:truncate
#
# Trunca todas as tabelas do banco de dados de sua aplicação rails exceto a tabela schema_migrations
# Essa é uma tarefa destrutiva, ela vai apagar todos os registros do seu banco de dados,
# então não use se você não souber o que está fazendo e seja cuidadoso quando usá-la.
# Eu recomendo usá-la apenas quando você tiver um arquivo seed.rb realmente bom em seu projeto
# como uma maneira fácil de limpar o banco e semear o banco usando o comando rake db:seed
namespace :db do
desc "Truncates all tables in database except the 'schema_migrations'"
task :truncate => :environment do
ActiveRecord::Base.connection.tables.select{|table| table != 'schema_migrations'}.each do |table|
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment