rake db:reset删除所有表但不删除数据库

时间:2022-07-15 22:49:33

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.

我需要删除数据库中的所有表而不删除数据库,因为此数据库的用户没有create database特权。

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.

此外,我们使用rake db:seed将一些条目添加到其中一个表中,因此我不想使用种子文件。

4 个解决方案

#1


8  

This is the solution I eventually came up with after looking at the Truncate method.

这是我在查看Truncate方法后最终想出的解决方案。

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

#2


2  

for testing, i would suggest using:

为了测试,我建议使用:

rake db:test:prepare

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

它将根据您的db / schema.rb重新生成所有表

#3


0  

You could achieve that with the following rake task (which i found here time ago)

您可以通过以下rake任务实现这一点(我在此前发现)

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

Edited to add a link to the question i found before:

编辑添加我之前找到的问题的链接:

Delete everything from all tables (in Activerecord)

删除所有表中的所有内容(在Activerecord中)

I don't understand why you want to drop all tables if the truncate instruction also make a fresh start of your table.

我不明白为什么你要删除所有表,如果truncate指令也重新开始你的表。

Anyway, maybe this answers could be helpful too:

无论如何,也许这个答案也可能有用:

https://*.com/a/2789515/1639291

https://*.com/a/2789515/1639291

https://*.com/a/1197218/1639291

https://*.com/a/1197218/1639291

#4


0  

If you run rake -P | grep db you will see all the database tasks built into Rails, but none of them seem to do what you're looking for. I believe the only way to do it is using a migration. This answer shows you how.

如果你运行rake -P | grep db你将看到Rails中内置的所有数据库任务,但它们似乎都没有做你想要的。我认为唯一的方法是使用迁移。这个答案告诉你如何。

#1


8  

This is the solution I eventually came up with after looking at the Truncate method.

这是我在查看Truncate方法后最终想出的解决方案。

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

#2


2  

for testing, i would suggest using:

为了测试,我建议使用:

rake db:test:prepare

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

它将根据您的db / schema.rb重新生成所有表

#3


0  

You could achieve that with the following rake task (which i found here time ago)

您可以通过以下rake任务实现这一点(我在此前发现)

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

Edited to add a link to the question i found before:

编辑添加我之前找到的问题的链接:

Delete everything from all tables (in Activerecord)

删除所有表中的所有内容(在Activerecord中)

I don't understand why you want to drop all tables if the truncate instruction also make a fresh start of your table.

我不明白为什么你要删除所有表,如果truncate指令也重新开始你的表。

Anyway, maybe this answers could be helpful too:

无论如何,也许这个答案也可能有用:

https://*.com/a/2789515/1639291

https://*.com/a/2789515/1639291

https://*.com/a/1197218/1639291

https://*.com/a/1197218/1639291

#4


0  

If you run rake -P | grep db you will see all the database tasks built into Rails, but none of them seem to do what you're looking for. I believe the only way to do it is using a migration. This answer shows you how.

如果你运行rake -P | grep db你将看到Rails中内置的所有数据库任务,但它们似乎都没有做你想要的。我认为唯一的方法是使用迁移。这个答案告诉你如何。