显示 Rails 中待处理的迁移

发布于 2024-08-02 19:24:29 字数 39 浏览 2 评论 0原文

是否有 rake 任务显示 Rails 应用程序中待处理的迁移?

Is there a rake task that shows the pending migrations in a rails app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

贪恋 2024-08-09 19:24:29

rake db:migrate:status (Rails 3 到 5) 或 rails db:migrate:status (Rails 5+) 将完成此任务。请参阅此提交

up 表示迁移已运行。 down 表示迁移尚未运行。

rake db:migrate:status (Rails 3 to 5) or rails db:migrate:status (Rails 5+) will accomplish this. See this commit.

up means the migration has been run. down means the migration has not been run.

眼角的笑意。 2024-08-09 19:24:29

rake db:abort_if_pending_migrations(至少在Rails 2.3.3中,不确定它是什么时候引入的)。描述说“如果有待处理的迁移,则会引发错误”。这似乎更多地用作其他任务的先决条件,但我猜您可以将其用于您的目的。

编辑:这是刚刚生成但未运行“测试”迁移后的输出示例

rails_project theIV$ rake db:abort_if_pending_migrations
(in /Users/theIV/Sites/rails_project/)
You have 1 pending migrations:
  20090828200602 Test
Run "rake db:migrate" to update your database then try again.

There is rake db:abort_if_pending_migrations (at least in Rails 2.3.3, not sure when it was introduced). The description says 'Raises an error if there are pending migrations'. This seems to be used more as a prerequisite for other tasks, but I'm guessing you could use it for your purposes.

EDIT: Here is an example of the output after having just generated and not run a 'test' migration

rails_project theIV$ rake db:abort_if_pending_migrations
(in /Users/theIV/Sites/rails_project/)
You have 1 pending migrations:
  20090828200602 Test
Run "rake db:migrate" to update your database then try again.
梦屿孤独相伴 2024-08-09 19:24:29

此命令将列出所有迁移及其状态(UPDOWN

Rails 3 和 4

rake db:migrate:status

Rails 5 及更高版本

rake db:migrate:status

# Or

rails db:migrate:status

这将返回类似的东西

database: blog_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20210131124730  Create articles
  down    20210131124801  Create users

This command will list all migrations with their status (UP or DOWN)

Rails 3 and 4

rake db:migrate:status

Rails 5 and above

rake db:migrate:status

# Or

rails db:migrate:status

This will return something like

database: blog_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20210131124730  Create articles
  down    20210131124801  Create users
执妄 2024-08-09 19:24:29

rake db:version 将在 Rails 2 上完成此操作。

rake db:version will accomplish this on Rails 2.

晨曦÷微暖 2024-08-09 19:24:29

这适用于 Rails 5.2

ActiveRecord::Base.connection.migration_context.needs_migration?

This works for rails 5.2

ActiveRecord::Base.connection.migration_context.needs_migration?
无言温柔 2024-08-09 19:24:29

如果您需要 bash 单行代码来确定是否运行迁移(例如,仅在 Heroku 发布阶段命令(当有待处理的迁移时),这有效:

(rails db:migrate:status | grep "^\s*down") && rails db:migrate || echo "No pending migrations found."

If you need a bash one-liner to determine whether to run a migration or not (e.g., only migrate in a Heroku release phase command when there is a pending migration), this works:

(rails db:migrate:status | grep "^\s*down") && rails db:migrate || echo "No pending migrations found."
老旧海报 2024-08-09 19:24:29

如果您想查看已完成或待处理的迁移量,可以使用以下命令查看。

rails db:migrate:status

有关此链接的更多信息:Rails 活动记录迁移

If you want to see how much migration is done or pending you can see using below command.

rails db:migrate:status

More on this link: Rails Active Record Migration

┾廆蒐ゝ 2024-08-09 19:24:29

尝试 rake -h (帮助)并查看 rake -n (= rake --dry-run)。所以可能像 rake -n db:migrate 这样的东西应该可以满足您的需求。

Try rake -h (help) and have a look at rake -n (= rake --dry-run). So probably something like rake -n db:migrate should get you what you want.

何时共饮酒 2024-08-09 19:24:29

可能不完全是 OP 所要求的,但如果您只需要快速检查是否有任何迁移待在 rake 任务中使用,而不诉诸

rake db:migrate:status | grep down(如果您在 Windows 上可能不起作用)

ActiveRecord::Migration.check_pending! (引发您需要救援的 ActiveRecord::PendingMigrationError)

您可以使用needs_migration吗?方法:
https://apidock.com/rails/v4.0.2/ ActiveRecord/Migrator/needs_migration%3F/class

Might not quite be what OP is asking for, but if you just need to quickly check if any migrations are pending for use in a rake task, without resorting to

rake db:migrate:status | grep down (might not work if you're on Windows)

ActiveRecord::Migration.check_pending! (raises ActiveRecord::PendingMigrationError that you need to rescue)

you can use needs_migration? method:
https://apidock.com/rails/v4.0.2/ActiveRecord/Migrator/needs_migration%3F/class

三月梨花 2024-08-09 19:24:29

使用以下命令检查迁移状态:

rake db:migrate:status

或者

当您运行服务器时,它将显示一条消息,要求首先运行挂起的迁移。

Following command to check migration status:

rake db:migrate:status

OR

when you run your server, it will display a message to run your pending migration first.

小霸王臭丫头 2024-08-09 19:24:29
ActiveRecord::Base.connection.migration_context.open.pending_migrations

返回:

#<struct ActiveRecord::MigrationProxy name="AddColumnHabilitaHrSobreavisoToTbhorarios", version=20231013134900, filename="/home/rodrigo/projects/engines/gpweb_frequencia/db/migrate/20231013134900_add_column_habilita_hr_sobreaviso_to_tbhorarios.rb", scope="">
ActiveRecord::Base.connection.migration_context.open.pending_migrations

Return:

#<struct ActiveRecord::MigrationProxy name="AddColumnHabilitaHrSobreavisoToTbhorarios", version=20231013134900, filename="/home/rodrigo/projects/engines/gpweb_frequencia/db/migrate/20231013134900_add_column_habilita_hr_sobreaviso_to_tbhorarios.rb", scope="">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文