Ruby-on-rails Rails 迁移:检索当前迁移版本的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8845706/
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 migration: best way to retrieve current migration version
提问by shigeya
Is there good way to retrieve migration version number?
有没有检索迁移版本号的好方法?
I need to implement a method in a model which behave differently on and beyond a specific migration version.
我需要在模型中实现一种方法,该方法在特定迁移版本内外表现不同。
I found assume_migrated_upto_version in connection adapter is retrieving version from database but can't find others.
我发现连接适配器中的assume_migrated_upto_version 正在从数据库中检索版本,但找不到其他版本。
Background: I'm trying to remove two columns from table A, want to move them into table B, and add association to the table B from the table A.
背景:我试图从表 A 中删除两列,想将它们移到表 B 中,并从表 A 中添加到表 B 的关联。
During this change, I need to access these two columns. but after that, I want to add proxy method for these columns for compatibility.
在此更改期间,我需要访问这两列。但在那之后,我想为这些列添加代理方法以实现兼容性。
回答by Peter Ehrlich
There's a much nicer way: rake db:migrate:status
有一个更好的方法: rake db:migrate:status
up 20120530222941 Create shenanigans
up 20120613030015 *** NO FILE ***
Indicating that I've deleted my latest migration file.
表明我已经删除了我最新的迁移文件。
Or more simply:
或者更简单:
> rake db:version
Current version: 20120613030015
回答by jibiel
> ActiveRecord::Migrator.current_version
(0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
=> 20120110085802
> ActiveRecord::Migrator.get_all_versions
(0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
=> [20111114121610,
20111115091108,
...
回答by Tony
If you don't want to do this without loading your app you can create a script like this:
如果您不想在不加载应用程序的情况下执行此操作,则可以创建如下脚本:
#!/usr/bin/env ruby
root = File.expand_path("../..", __FILE__)
lines = `ls #{root}/db/migrate`
puts lines.split("\n").last.split(" ").last.split("_").first
Note the rootline is because my script is in a bindir
请注意这一root行是因为我的脚本在bin目录中

