Ruby-on-rails 在 rails 中显示挂起的迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1349047/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:41:04  来源:igfitidea点击:

Show pending migrations in rails

ruby-on-railsrails-migrations

提问by Readonly

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

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

回答by jrdioko

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

rake db:migrate:status(Rails 3 到 5) 或rails db:migrate:status(Rails 5) 将实现这一点。看到这个提交

upmeans the migration has been run. downmeans the migration has not been run.

up意味着迁移已经运行。down表示尚未运行迁移。

回答by theIV

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.

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

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.

回答by Deepak Mahakale

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

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

Rails 3 and 4

轨道 3 和 4

rake db:migrate:status

Rails 5

导轨 5

rake db:migrate:status

# Or

rails db:migrate:status

回答by gerryster

rake db:versionwill accomplish this on Rails 2.

rake db:version将在 Rails 2 上实现这一点。

回答by Sairam

This works for rails 5.2

这适用于 rails 5.2

ActiveRecord::Base.connection.migration_context.needs_migration?

回答by John Lockwood

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.

尝试 rake -h (help) 并查看 rake -n (= rake --dry-run)。所以可能像 rake -n db:migrate 这样的东西应该能让你得到你想要的。

回答by wondersz1

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

可能不是 OP 所要求的,但是如果您只需要快速检查是否有任何迁移正在等待在 rake 任务中使用,而无需求助于

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

耙数据库:迁移:状态| grep down(如果你在 Windows 上可能不起作用)

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

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

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

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

回答by Foram Thakral

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

有关此链接的更多信息:Rails Active Record Migration

回答by niborg

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 commandwhen there is a pending migration), this works:

如果您需要一个 bash one-liner 来确定是否运行迁移(例如,只有在有待处理的迁移时才在Heroku 发布阶段命令中迁移),这有效:

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

回答by puneet18

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.

当您运行服务器时,它会显示一条消息,要求您先运行待处理的迁移。