Ruby-on-rails Rails 4 如何忽略挂起的迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20244244/
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
Rails 4 how to ignore pending migrations
提问by Waterlink
Problem is following:
问题如下:
- I have db/seed.rb full of initial data.
- One of migrations depends on data this seed provides.
- I'm trying to deploy my app from empty db.
- 我有 db/seed.rb 充满了初始数据。
- 迁移之一取决于此种子提供的数据。
- 我正在尝试从空数据库部署我的应用程序。
Result is:
结果是:
- RAILS_ENV=production rake db:migrate - fails due to lack of initial data
- RAILS_ENV=production rake db:seed - fails due to pending migrations
- RAILS_ENV=production rake db:migrate - 由于缺少初始数据而失败
- RAILS_ENV=production rake db:seed - 由于暂挂迁移而失败
I wanted to somehow tell rake to ignore pending migrations, but unable to do it so far.
我想以某种方式告诉 rake 忽略挂起的迁移,但到目前为止无法做到。
UPDATE (due to additional experience)
更新(由于额外的经验)
Sometimes migrations and model code goes out of sync, so migrations not being run. To avoid this problem recently used redefining of model in migrations:
有时迁移和模型代码不同步,因此不会运行迁移。为了避免这个问题,最近在迁移中使用了重新定义模型:
# reset all callbacks, hooks, etc for this model
class MyAwesomeModel < ActiveRecord::Base
end
class DoSomethingCool < ActiveRecord::Migration
def change
...
end
end
采纳答案by Dan Grahn
Rails stores migration information in a table called schema_migrations.
Rails 将迁移信息存储在名为schema_migrations.
You can add the version from your migration into that table to skip a specific migration.
您可以将迁移中的版本添加到该表中以跳过特定迁移。
The version is the number string which comes before the description in the file name.
版本是文件名中描述之前的数字字符串。
[version]_Create_Awesome.rb
回答by whizcreed
I am not very sure if this will help you. But I was looking for something and found this question. So it looks like this might help:
我不太确定这是否会帮助你。但我正在寻找一些东西并发现了这个问题。所以看起来这可能会有所帮助:
In RAILS_ROOT/config/environments/development.rb Set the following setting to false:
在 RAILS_ROOT/config/environments/development.rb 中将以下设置设为 false:
config.active_record.migration_error = false#:page_load
In my situation it now does not show the pending migration error anymore. Should work for rake tasks and console for the same environment as well.
在我的情况下,它现在不再显示待处理的迁移错误。也应该适用于相同环境的 rake 任务和控制台。
回答by mirelon
Rename the migration dependent on the data from:
根据以下数据重命名迁移:
20140730091353_migration_name.rb
to
到
.20140730091353_migration_name.rb
(add a dot at the start of the filename)
(在文件名的开头添加一个点)
Then run rake db:seed(it will no longer complain on the pending migrations) and then rename back the migration.
然后运行rake db:seed(它不会再抱怨挂起的迁移),然后重命名迁移。
If you have more migrations following after, you have to rename all of them or just move it temporary away.
如果之后有更多迁移,则必须重命名所有迁移或将其暂时移走。
回答by Simone Carletti
There is no way unless you monkey patch the Rails code. I strongly advise you to fix your migrations instead.
除非你给 Rails 代码打补丁,否则没有办法。我强烈建议您改为修复您的迁移。
A migration should not depend on the existence of some data in the database. It can depend on a previous migration, but of course absolutely not on the data on the db.
迁移不应依赖于数据库中某些数据的存在。它可以依赖于以前的迁移,但当然绝对不是依赖于数据库上的数据。
回答by siva
I had a similar issue. I commented out the add_column lines and ran the rake db:migrate commands and then removed the comment when I will need it for the testing or production environment.
我有一个类似的问题。我注释掉了 add_column 行并运行了 rake db:migrate 命令,然后在测试或生产环境需要它时删除了注释。

