Rails 迁移:检索当前迁移版本的最佳方式 [英] Rails migration: best way to retrieve current migration version
问题描述
有没有什么好的方法可以检索迁移版本号?
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.
我发现连接适配器中的 assert_migrated_upto_version 正在从数据库中检索版本但找不到其他版本.
I found assume_migrated_upto_version in connection adapter is retrieving version from database but can't find others.
背景:我试图从表 A 中删除两列,想将它们移到表 B 中,并从表 A 中添加与表 B 的关联.
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.
在此更改期间,我需要访问这两列.但在那之后,我想为这些列添加代理方法以实现兼容性.
During this change, I need to access these two columns. but after that, I want to add proxy method for these columns for compatibility.
推荐答案
Rails 5.2
及更高版本:
Rails 5.2
and higher:
> ApplicationRecord.connection.migration_context.current_version
(0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
=> 20200510093804
> ApplicationRecord.connection.migration_context.get_all_versions
(0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
=> [20191005164928,
20191006111502,
...
Rails 到 5.1.7
:
> 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,
...
这篇关于Rails 迁移:检索当前迁移版本的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!