显示 Rails 中待处理的迁移
是否有 rake 任务显示 Rails 应用程序中待处理的迁移?
Is there a rake task that shows the pending migrations in a rails app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否有 rake 任务显示 Rails 应用程序中待处理的迁移?
Is there a rake task that shows the pending migrations in a rails app?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
rake db:migrate:status
(Rails 3 到 5) 或rails db:migrate:status
(Rails 5+) 将完成此任务。请参阅此提交。up
表示迁移已运行。down
表示迁移尚未运行。rake db:migrate:status
(Rails 3 to 5) orrails 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.有
rake db:abort_if_pending_migrations
(至少在Rails 2.3.3中,不确定它是什么时候引入的)。描述说“如果有待处理的迁移,则会引发错误”。这似乎更多地用作其他任务的先决条件,但我猜您可以将其用于您的目的。编辑:这是刚刚生成但未运行“测试”迁移后的输出示例
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
此命令将列出所有迁移及其状态(
UP
或DOWN
)Rails 3 和 4
Rails 5 及更高版本
这将返回类似的东西
This command will list all migrations with their status (
UP
orDOWN
)Rails 3 and 4
Rails 5 and above
This will return something like
rake db:version
将在 Rails 2 上完成此操作。rake db:version
will accomplish this on Rails 2.这适用于 Rails 5.2
This works for rails 5.2
如果您需要 bash 单行代码来确定是否运行迁移(例如,仅在 Heroku 发布阶段命令(当有待处理的迁移时),这有效:
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 活动记录迁移
If you want to see how much migration is done or pending you can see using below command.
More on this link: Rails Active Record Migration
尝试 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.
可能不完全是 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
使用以下命令检查迁移状态:
或者
当您运行服务器时,它将显示一条消息,要求首先运行挂起的迁移。
Following command to check migration status:
OR
when you run your server, it will display a message to run your pending migration first.
返回:
Return: