有没有办法自动进行“rake db:migrate RAILS_ENV=test”在“rake db:migrate”之后在开发环境中?

发布于 2024-11-28 00:16:56 字数 773 浏览 2 评论 0原文

在开发环境中,有没有办法在每次 rake db:migrate 后自动执行 rake db:migrate RAILS_ENV=test

我有guardguard-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

终弃我 2024-12-05 00:16:56

可能只需在 .bashrc 文件中创建一个命令别名即可。

~/.bashrc

alias rake_db_migrate='rake db:migrate db:test:prepare'

终端

$ rake_db_migrate

Possibly just make a command alias in your .bashrc file.

~/.bashrc

alias rake_db_migrate='rake db:migrate db:test:prepare'

Terminal

$ rake_db_migrate
饭团 2024-12-05 00:16:56

我使用这个别名:
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.

蹲在坟头点根烟 2024-12-05 00:16:56

我更喜欢这样使用别名:

在你的 ~/.bashrc

alias migrate='rake db:migrate && rake db:test:prepare'

,很容易花 30 分钟试图弄清楚为什么你的测试没有通过,只是为了记住你没有重置数据库。这将解决这个问题。

I prefer to use an alias this way:

In your ~/.bashrc

alias migrate='rake db:migrate && rake db:test:prepare'

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.

只为一人 2024-12-05 00:16:56

更快: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)

心房的律动 2024-12-05 00:16:56

我非常喜欢的一个选项是覆盖另一个 rake 脚本中的实际任务。这将在运行迁移后自动调用。像这样,我总是在迁移数据库后创建一个 ERD 图:

# lib/tasks/database.rake
namespace :db do
  desc 'Additional migrate task that creates the diagram'
  task :migrate do
    if Rails.env.development?
      Rake::Task['diagram:erd'].invoke
    end
  end
end

所以在您的情况下:

# lib/tasks/database.rake
namespace :db do
  desc 'Additional migrate task that creates the diagram'
  task :migrate do
    `rake db:migrate RAILS_ENV=test`
  end
end

另一种方法是运行以下命令,它将您的新模式克隆到测试数据库:

rake db:migrate db:test:clone

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:

# lib/tasks/database.rake
namespace :db do
  desc 'Additional migrate task that creates the diagram'
  task :migrate do
    if Rails.env.development?
      Rake::Task['diagram:erd'].invoke
    end
  end
end

So in your case:

# lib/tasks/database.rake
namespace :db do
  desc 'Additional migrate task that creates the diagram'
  task :migrate do
    `rake db:migrate RAILS_ENV=test`
  end
end

Another approach would be to run the following, which clones your new schema to the test database:

rake db:migrate db:test:clone
南汐寒笙箫 2024-12-05 00:16:56

为了回答您最初使用 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 watch db/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 in db/schma.rb or use rake db:test:prepare instead of rake 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!

情徒 2024-12-05 00:16:56

如果您想将其作为一个行之有效的单行代码,您可以将其设置为:

echo "alias rake-migrate='rake db:migrate && rake db:migrate RAILS_ENV=test'" >> ~/.zshrc && source ~/.zshrc

对于回滚,有:

echo "alias rake-rollback='rake db:rollback && rake db:rollback RAILS_ENV=test'" >> ~/.zshrc && source ~/.zshrc

然后,只需在终端中运行

rake-migrate

rake-rollback

注意: 这将添加一个别名到 ~/.zshrc 文件。
注 2::如果您执行此操作,例如 Terminal1,并使用另一个终端选项卡或 vscode 终端,则只需在您想要的终端上运行 source ~/.zshrc运行它

If you feel like having this as a one-liner that just works, you can have it as:

echo "alias rake-migrate='rake db:migrate && rake db:migrate RAILS_ENV=test'" >> ~/.zshrc && source ~/.zshrc

and for the rollback there is:

echo "alias rake-rollback='rake db:rollback && rake db:rollback RAILS_ENV=test'" >> ~/.zshrc && source ~/.zshrc

then, just run in the terminal

rake-migrate

or

rake-rollback

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文