Ruby-on-rails rake db:migrate 没有检测到新的迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/70318/
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
rake db:migrate doesn't detect new migration?
提问by Rollo Tomazzi
Experienced with Rails / ActiveRecord 2.1.1
有 Rails / ActiveRecord 2.1.1 经验
- You create a first version with (for example) ruby script\generate scaffold product title:string description:text image_url:string
- This create (for example) a migration file called 20080910122415_create_products.rb
- You apply the migration with rake db:migrate
- Now, you add a field to the product table with ruby script\generate migration add_price_to_product price:decimal
- This create a migration file called 20080910125745_add_price_to_product.rb
- If you try to run rake db:migrate, it will actually revert the first migration, not apply the next one! So your product table will get destroyed!
- But if you ran rake alone, it would have told you that one migration was pending
- 您使用(例如)ruby script\generate scaffold product title:string description:text image_url:string 创建第一个版本
- 这将创建(例如)一个名为 20080910122415_create_products.rb 的迁移文件
- 您使用 rake db:migrate 应用迁移
- 现在,您使用 ruby script\generate migration add_price_to_product price:decimal 在产品表中添加一个字段
- 这将创建一个名为 20080910125745_add_price_to_product.rb 的迁移文件
- 如果您尝试运行 rake db:migrate,它实际上会还原第一次迁移,而不是应用下一次!所以你的产品表会被破坏!
- 但是如果你单独运行 rake,它会告诉你有一个迁移正在等待中
Pls note that applying rake db:migrate (once the table has been destroyed) will apply all migrations in order.
请注意,应用 rake db:migrate (一旦表被销毁)将按顺序应用所有迁移。
The only workaround I found is to specify the version of the new migration as in:
我发现的唯一解决方法是指定新迁移的版本,如下所示:
rake db:migrate version=20080910125745
So I'm wondering: is this an expected new behavior?
所以我想知道:这是预期的新行为吗?
采纳答案by Jean
You should be able to use
你应该可以使用
rake db:migrate:up
to force it to go forward, but then you risk missing interleaved migrations from other people on your team
迫使它继续前进,但是你可能会错过团队中其他人的交错迁移
if you run
如果你跑
rake db:migrate
twice, it will reapply all your migrations.
两次,它将重新应用您的所有迁移。
I encounter the same behavior on windows with SQLite, it might be a bug specific to such an environment.
我在使用 SQLite 的 Windows 上遇到了相同的行为,这可能是特定于此类环境的错误。
Edit-- I found why. In the railstie database.rake task you have the following code :
编辑- 我找到了原因。在 railstie database.rake 任务中,您有以下代码:
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
task :migrate => :environment do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
Then in my environment variables I have
然后在我的环境变量中我有
echo %Version% #=> V3.5.0f
in Ruby
在红宝石
ENV["VERSION"] # => V3.5.0f
ENV["VERSION"].to_i #=>0 not nil !
thus the rake task calls
因此 rake 任务调用
ActiveRecord::Migrator.migrate("db/migrate/", 0)
and in ActiveRecord::Migrator we have :
在 ActiveRecord::Migrator 中,我们有:
class Migrator#:nodoc:
class << self
def migrate(migrations_path, target_version = nil)
case
when target_version.nil? then up(migrations_path, target_version)
when current_version > target_version then down(migrations_path, target_version)
else up(migrations_path, target_version)
end
end
Yes, rake db:migrate VERSION=0is the long version for rake db:migrate:down
是的,rake db:migrate VERSION=0是长版rake db:migrate:down
Edit- I would go update the lighthouse bug but I the super company proxy forbids that I connect there
编辑- 我会去更新灯塔错误,但我的超级公司代理禁止我在那里连接
In the meantime you may try to unset Version before you call migrate ...
同时,您可以在调用 migrate ... 之前尝试取消设置版本。
回答by Jean
I respectfully disagree Tom! this isa bug !! V3.5.0f is not a valid version for rake migrations. Rake should not use it to migrate:down just because ruby chose to consider that "V3.5.0f".to_i is 0 ...
我恭敬地不同意汤姆!这是一个错误!V3.5.0f 不是用于 rake 迁移的有效版本。Rake 不应该使用它来 migrate:down 仅仅因为 ruby 选择考虑“V3.5.0f”.to_i 是 0 ...
Rake should loudly complain that VERSION is not valid so that users know what is up (between you and me, checking that the version is a YYYYMMDD formated timestamp by converting to integer is a bit light)
Rake 应该大声抱怨 VERSION 无效,以便用户知道发生了什么(在你和我之间,通过转换为整数来检查版本是 YYYYMMDD 格式的时间戳有点轻)
[Damn IE6 that won't allow me to comment ! and no I can't change browser thanks corporate]
[该死的IE6不允许我发表评论!不,我无法更改浏览器,感谢公司]
回答by Rollo Tomazzi
Jean,
让,
Thanks a lot for your investigation. You're right, and actually I think you've uncovered a more severe bug, of species 'design bug'.
非常感谢您的调查。你是对的,实际上我认为你已经发现了一个更严重的错误,即物种“设计错误”。
What's happening is that rake will grab whatever value you pass to the command line and store them as environment variables. The rake tasks that will eventually get called will just pull this values from the environment variable. When db:migrate queries ENV["VERSION"], it actually requests the version parameter which you set calling rake. When you call rake db:migrate, you don't pass any version.
发生的事情是 rake 将获取您传递给命令行的任何值并将它们存储为环境变量。最终将被调用的 rake 任务只会从环境变量中提取这些值。当 db:migrate 查询 ENV["VERSION"] 时,它实际上是请求您在调用 rake 时设置的版本参数。当您调用 rake db:migrate 时,您不会传递任何版本。
But we do have an environment variable called VERSION that has been set for other purposes by some other program (I don't which one yet). And the guys behind rake (or behind database.rake) haven't figured this would happen. That's a design bug. At least, they could have used more specific variable names like "RAKE_VERSION" or "RAKE_PARAM_VERSION" instead of just "VERSION".
但是我们确实有一个名为 VERSION 的环境变量,它已被其他程序设置用于其他目的(我还不知道是哪个)。而 rake 背后(或 database.rake 背后)的人没有想到会发生这种情况。那是设计错误。至少,他们可以使用更具体的变量名称,例如“RAKE_VERSION”或“RAKE_PARAM_VERSION”,而不仅仅是“VERSION”。
Tom, I will definitely not close but edit my bug report on lighthouse to reflect these new findings.
汤姆,我绝对不会关闭而是编辑我关于灯塔的错误报告以反映这些新发现。
And thanks again Jean for your help. I've posted this bug on lighthouse like 5 days agao and still got no answer!
再次感谢 Jean 的帮助。我已经 5 天前在灯塔上发布了这个错误,但仍然没有答案!
Rollo
罗洛
回答by tomafro
This is not the expected behaviour. I was going to suggest reporting this as a bug on lighthouse, but I see you've already done so! If you provide some more information (including OS/database/ruby version) I will take a look at it.
这不是预期的行为。我打算建议将此报告为灯塔上的错误,但我看到您已经这样做了!如果您提供更多信息(包括操作系统/数据库/ruby 版本),我会查看它。

