Rake 任务截断 Rails 3 中的所有表
我想要一个 rake 任务来截断所有表。我找到了一个 在互联网上,但它只适用于 Rails 2,不适用于 Rails 3 (问题在于获取数据库连接)。
rake db:reset 不是一个选项,因为我使用的是 PostgreSQL,它也会删除用户。因此迁移失败。我只想清除数据。
你们有什么事情要给我吗?
I would like to have a rake task for truncating all the tables. I have found one on the internet, but it is supposed only for Rails 2 and does not work for Rails 3 (problem is in getting a database connection).
rake db:reset
is not an option, because I am using PostgreSQL and it also drops the user. Therefore migration fails. I only want to clear the data.
Do you guys have somehting for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我通过谷歌找到了这个,然后我得到了一个比批准的解决方案更简单的解决方案,所以这里是:使用 database_cleaner 宝石。这是步骤。
在 Gemfile 中(修改后执行包):
使用该 gem 后,语句
DatabaseCleaner.clean_with :truncation
将截断数据库。将其添加到 rake 任务中很简单:就是这样。您还可以直接使用
db/seeds.rb
文件中的DatabaseCleaner.clean_with :truncation
行,这样您就不会忘记在播种前截断数据库。I've found this via google, and then I got a much simpler solution than the one approved, so here it is: Use the database_cleaner gem. Here're the steps.
In your Gemfile (execute bundle after modifying):
With that gem in place, the statement
DatabaseCleaner.clean_with :truncation
will truncate the database. Adding it to a rake task is trivial:That's it. You can also use the
DatabaseCleaner.clean_with :truncation
line inside yourdb/seeds.rb
file directly so that you don't forget to truncate the database before seeding.因此,我将链接的示例编辑为:
此示例基于 Chris Ledet 的以下代码(谢谢),并适用于 Rails 3.X。
感谢所有提示。
So I edited the linked example into this:
This example is based on Chris Ledet's code bellow (thanks) and works with Rails 3.X.
Thanks for all hints.
在 Rails 6 中截断数据库
To truncate db in Rails 6
根据 Chris Ledet 的回答,这变得简单得多:
According to Chris Ledet answer, this becomes much simpler:
您始终可以迁移到版本 0,如下所示:
这样您甚至不必截断表,然后就可以再次迁移。唯一的问题是您需要
down
迁移才能正常工作。尽管版本是基于时间戳的,但该解决方案确实适用于 Rails 3。
此解决方案如下所示: https://stackoverflow.com/a/1196822/241367
此外,您可以随时运行假设您的 schema.rb 是最新的,如下所示:
正如@kikito 建议的那样,您可以运行database_cleaner(它是
cucumber
和rspec
喜欢在测试之间使用什么),如下所示:You could always migrate to version 0, like so:
That way you don't even have to truncate your tables, and you can then migrate again. The only catch is that you need your
down
migrations to work properly.This solution does work in rails 3, despite the fact that the versions are timestamp-based.
This solution is as seen here: https://stackoverflow.com/a/1196822/241367
Additionally, you could always run the following, assuming your
schema.rb
is up to date:And as @kikito suggests, you can run
database_cleaner
(it's whatcucumber
andrspec
like to use between tests) like so:这将获取数据库中的所有表,找到与该表关联的模型并调用 #destroy_all。
This will get all of the tables in your database, find a model associated with that table and call #destroy_all.
lzap给出的答案有一个具体问题。 Rails 希望再次运行所有迁移。以下代码是 Anthony Alberto 建议的并且它有效。此添加检查 schema_migrations 表
The answer given by lzap has one specific problem. Rails wants to run all the migrations again. The following code is as suggested by Anthony Alberto and it works. This addition checks with the schema_migrations table