Ruby-on-rails 为什么要求我运行“rake db:migrate RAILS_ENV=test”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17150529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I asked to run 'rake db:migrate RAILS_ENV=test'?
提问by Peeja
On Rails 4.0.0.rc1, Ruby 2.0.0, after I run a migration, I see the following error when I try to run a test through rspec:
在 Rails 4.0.0.rc1、Ruby 2.0.0 上,运行迁移后,当我尝试通过以下方式运行测试时看到以下错误rspec:
/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in `check_pending!': Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError)
/Users/peeja/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.rc1/lib/active_record/migration.rb:376:in `check_pending! ':迁移待定;运行“rake db:migrate RAILS_ENV=test”来解决这个问题。(ActiveRecord::PendingMigrationError)
That doesn't seem right. No one migrates their test database, do they? They db:test:prepareit, which—to be fair—I've forgotten to do. So I run rake db:test:prepareand run my rspeccommand again…and see the same error.
这似乎不对。没有人迁移他们的测试数据库,是吗?他们db:test:prepare做到了,公平地说,我忘记了这样做。所以我再次运行rake db:test:prepare并运行我的rspec命令......并看到相同的错误。
If I actually rake db:migrate RAILS_ENV=test, the error does in fact go away.
如果我真的rake db:migrate RAILS_ENV=test,错误确实消失了。
What's going on? Is this new in Rails 4?
这是怎么回事?这是 Rails 4 的新功能吗?
回答by Peeja
As of Rails 4.1, the rake db:test:*tasks are deprecated. Instead, your (test|spec)_helper.rbshould include:
从 Rails 4.1 开始,rake db:test:*不推荐使用这些任务。相反,您(test|spec)_helper.rb应该包括:
ActiveRecord::Migration.maintain_test_schema!
This means that your test database will get the correct schema every time your tests run, whether you run them from a Rake task or not.
这意味着每次运行测试时,您的测试数据库都会获得正确的模式,无论您是否从 Rake 任务中运行它们。
回答by Kris
Looks like rake test:prepareworks, not sure what db:test:preparenow does.
看起来rake test:prepare有效,不确定db:test:prepare现在做什么。
回答by meejoe
You can also try
你也可以试试
rake db:migrate RAILS_ENV=test
which works as
它的作用是
db:test:prepare
does:)
做:)
回答by SilasOtoko
I still have trouble sometimes in sorting this problem out when I just follow one person's answer so I have thrown a couple together to get better results. Here are the steps I take, not sure which ones are unnecessary, but it works in the end.
当我只是按照一个人的答案来解决这个问题时,有时我仍然很难解决这个问题,所以我把几个人放在一起以获得更好的结果。这是我采取的步骤,不确定哪些是不必要的,但最终还是有效的。
- add
ActiveRecord::Migration.maintain_test_schema!to the top of the test_helper.rb file. rake test:preparerake db:migraterake db:migrate RAILS_ENV=test
- 添加
ActiveRecord::Migration.maintain_test_schema!到 test_helper.rb 文件的顶部。 rake test:preparerake db:migraterake db:migrate RAILS_ENV=test
Then when I run bundle exec rake testI get clean results every time with no pending migrations. (This is what I do right after generating the scaffold the first time). Someone feel free to correct me if you know for sure that one of these steps is absolutely not necessary, but this is how I make sure it works every time.
然后当我运行时,我bundle exec rake test每次都得到干净的结果,没有挂起的迁移。(这是我第一次生成脚手架后立即做的)。如果您确定这些步骤之一绝对没有必要,有人可以随时纠正我,但这就是我确保它每次都能正常工作的方式。
回答by Jesse Brown
I've found I have this problem when using chrubyto manage my ruby versions. Rails calls bin/rails db:test:preparevia the system command. This doesn't take advantage of chrubys $PATHenv var, so it runs as whatever the system ruby is, and fails because of missing gems typically. Unfortunately, I don't currently have a good solution for this.
我发现在chruby用于管理我的 ruby 版本时遇到了这个问题。Railsbin/rails db:test:prepare通过系统命令调用。这没有利用 chrubys $PATHenv var,所以它像系统 ruby 一样运行,并且通常由于缺少 gems 而失败。不幸的是,我目前没有一个很好的解决方案。
回答by Unkas
You can try to set variable BEFORE command, like this. This statement solved my problem:
您可以尝试像这样设置变量 BEFORE 命令。这句话解决了我的问题:
RAILS_ENV=test rake db:migrate

