有没有办法自动进行“rake db:migrate RAILS_ENV=test”在“rake db:migrate”之后在开发环境中?
在开发环境中,有没有办法在每次 rake db:migrate
后自动执行 rake db:migrate RAILS_ENV=test
?
我有guard和guard-rspec 正在运行,我对失败的测试感到非常恼火,即使它在浏览器中手动工作。
每次我暂停开发时,我都会花费至少 15 分钟的时间来弄清楚我只是忘记在数据库更改后调用 rake db:migrate:test
。
由于我已经在使用guard,我考虑添加guard-rake 也到该项目,但我不知道应该观看哪个文件。当观看 development.sqlite3 时,每次我通过浏览器对记录执行某些操作时,rake db:migrate RAILS_ENV=test
都会被触发,所以这并不是我真正想要的。
有人可以帮我解决我的问题吗?
Is there a way to automatically do a rake db:migrate RAILS_ENV=test
after each rake db:migrate
when in development environment?
I have guard and guard-rspec running, and I am really annoyed about the failing tests, even if it works manually in the browser.
It costs me at least 15 minutes every time I had a pause from development, to figure out that I simply forgot to call rake db:migrate:test
after the change of the database.
Since I am already using guard I thought about adding guard-rake to the project also, but I dont know which file I should watch. When watching development.sqlite3, rake db:migrate RAILS_ENV=test
would be fired every time I do something with my records through the browser, so this is not really what I want.
Can someone help me with my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
可能只需在
.bashrc
文件中创建一个命令别名即可。~/.bashrc
终端
Possibly just make a command alias in your
.bashrc
file.~/.bashrc
Terminal
我使用这个别名:
alias rake_db_migrate='rake db:migrate && rake db:migrate RAILS_ENV=test'
因为
rake db:test:prepare
已弃用。我使用它的原因是因为我们的项目使用 pg_search (postgreSQL) 以及 Structure.sql (不是 schema.rb),并且出于某种原因运行 rake db:migrate 不会准备测试数据库。
I use this alias:
alias rake_db_migrate='rake db:migrate && rake db:migrate RAILS_ENV=test'
because
rake db:test:prepare
is deprecated.The reason why I use this is because our project uses pg_search (postgreSQL) along with the structure.sql (not schema.rb) and for some reason running
rake db:migrate
does not prepare the test database.我更喜欢这样使用别名:
在你的
~/.bashrc
中,很容易花 30 分钟试图弄清楚为什么你的测试没有通过,只是为了记住你没有重置数据库。这将解决这个问题。
I prefer to use an alias this way:
In your
~/.bashrc
Its easy to spend 30 minutes trying to figure why your tests are not passing only to remember you didn't reset the database. This will solve that problem.
更快:
alias migrate='rake db:migrate db:test:prepare'
(将其添加到.bashrc
中,它将仅加载 Rails 一次)Faster:
alias migrate='rake db:migrate db:test:prepare'
(add this in your.bashrc
, it will load Rails only one time)我非常喜欢的一个选项是覆盖另一个 rake 脚本中的实际任务。这将在运行迁移后自动调用。像这样,我总是在迁移数据库后创建一个 ERD 图:
所以在您的情况下:
另一种方法是运行以下命令,它将您的新模式克隆到测试数据库:
An option which I quite like is to override the actual task in another rake script. This will be invoked automatically after running the migration. Like this I always create an ERD diagram after I migrate the database:
So in your case:
Another approach would be to run the following, which clones your new schema to the test database:
为了回答您最初使用 Guard-rake 的问题,您可以观看 db/schema.rb ,因为每当您迁移数据库时它都会更新。但是,如果您执行回滚,该文件也会更改,因此您可能必须从 db/schma.rb 中的架构定义行中提取迁移版本或使用 rake db :test:prepare 而不是
rake db:migrate RAILS_ENV=test
。这似乎很脆弱,因此使用 shell 别名可能是更好的方法。我只是希望有更好的方法!
In response to your original question of using
guard-rake
, you could watchdb/schema.rb
, as this will be updated whenever you migrate the database. However, this file will also be changed if you do a rollback, so you would likely have to pull out the migration version from the schema definition line indb/schma.rb
or userake db:test:prepare
instead ofrake db:migrate RAILS_ENV=test
.It seems like this is brittle, so using a shell alias is probably the better approach. I just wish there was a better way!
如果您想将其作为一个行之有效的单行代码,您可以将其设置为:
对于回滚,有:
然后,只需在终端中运行
或
注意: 这将添加一个别名到
~/.zshrc
文件。注 2::如果您执行此操作,例如 Terminal1,并使用另一个终端选项卡或 vscode 终端,则只需在您想要的终端上运行
source ~/.zshrc
运行它If you feel like having this as a one-liner that just works, you can have it as:
and for the rollback there is:
then, just run in the terminal
or
note: this will add an alias to the
~/.zshrc
file.note 2:: if you do this, say terminal1, and use another terminal tab, or vscode terminal, then just run
source ~/.zshrc
on the terminal you want to run it