Rake in Rails:我应该使用 db:reset 吗?
我对默认 Rails Rake 任务的预期用途有点困惑,想了解是否应该使用 db:reset 或编写自定义 Rake 任务的建议。没什么聪明的,只是日常整理,而且我很可能会错过一个明显的文档,因为我是 Rails 新手。
我的问题:我想扔掉我的数据库并从完全干净的设置运行,以便我可以确保数据库仅包含已知数据。这对于演示准备、调试以及确保 Jenkins 在测试中进行同类比较非常有用。
目前,我正在写这篇文章:
bin/rake db:drop:all db:create:all db:migrate db:seed db:test:prepare
这需要输入很多内容,但仅将种子数据保留在开发和测试数据库中。我不确定这与 db:reset 有何不同,后者输入起来更方便。
我应该使用 db:reset
还是编写自定义 db:from_scratch
任务?
I'm a little confused by the intended use of the default Rails Rake tasks, and would like advice on whether I should be using db:reset
or writing a custom Rake task. Nothing clever, just daily housekeeping, and I may well be missing an obvious doc as I'm new to Rails.
My problem: I want to throw away my database and run from a completely clean setup, in order that I can be sure the database contains known data only. This is useful for demo prep, for debugging, and for making sure Jenkins is comparing like-with-like in tests.
Currently, I'm writing this:
bin/rake db:drop:all db:create:all db:migrate db:seed db:test:prepare
This is a lot to type, but leaves seed data only in both dev and test databases. I am unsure how this differs from db:reset
, which would be more convenient to type.
Should I use db:reset
or write a custom db:from_scratch
task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用:
这将删除数据库,重新创建它并加载当前架构到其中。
有关 rake db 任务的完整列表:
如果您的要求发生变化,那么最好编写一个自定义 rake 任务,您可以在其中应用自己的自定义设置。
You should be using:
This will drop the database, recreate it and load the current schema into it.
For a full list of rake db tasks:
If your requirements change then it would be better to write a custom rake task, where you can apply your own customisation.
如果您不确定 rake 任务的作用,那么我建议不要使用它。在这种情况下,您可能没问题,但是
db:reset
并不等同于您上面所做的事情。db:reset
从scheme.rb重新创建数据库,这可能会有所不同,因为您可能已经编写了尚未运行的迁移。我建议您使用自定义 rake 任务,然后您可以修改它以适合您的确切目的,特别是如果您还想执行其他任务(例如在 git 中标记)
If you're not sure what a rake task does, then I would suggest not using it. In this case, you're probably ok, however
db:reset
is not the equivalent to what you are doing above.db:reset
recreates the database from scheme.rb, this may be different as you could have written migrations that have not yet been run.I would suggest that you use a custom rake task, you can then modify it to fit your exact purposes, especially if you want to perform other tasks as well (for example tagging in git)
您在通过 rake 运行的任务中试图实现的目标是设置测试数据库和开发数据库。 rake db:reset 只会根据 schema.rb 对当前环境数据库执行此操作,然后将种子数据加载到数据库中。 schema.rb 一般来说不会被编辑,它是为了只知道/仅供参考的目的,但是有些人确实倾向于使用不同的 schema.rb 这可能会产生问题:(。对我来说听起来更好的是如果你需要设置您的开发和测试数据库,然后为开发环境运行迁移并使用 schema.rb 创建测试环境数据库,但我绝对建议您从一些 Rails 专家那里获得第二意见,以找出真正完美的方案。的方式来实现你想要的。
What you are trying to achieve in your the tasks you are running through rake is setting both the test and the development databases.
rake db:reset
will just do it for your current environment db according to the schema.rb and then load the seeds data into the db. The schema.rb in general is never edited, its for a know-only/refer-only purpose, however some people do tend have different schema.rb which might create a problem :(. What sounds better to me is if you need to set both your development and test database, then run your migrations for the dev environment and use the schema.rb to create the test environment db. I would definitely suggest you to get a second opinion from some Rails guru though to find out a real perfect way to achieve what you want.耙数据库:重置
这将删除数据库,重新创建它并将当前模式加载到其中。
rake db:reset 将运行 rake db:seed
示例:
如果您有运行迁移后编写的种子文件,它也会运行该文件。
rake db:reset
This will drop the database, recreate it and load the current schema into it.
rake db:reset will run rake db:seed
Example :
if you have seed file that you wrote after you ran your migration , it will run that too.