Rails migration: best way to retrieve current migration version

后端 未结 4 983
梦谈多话
梦谈多话 2021-01-30 03:06

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.

4条回答
  •  盖世英雄少女心
    2021-01-30 03:34

    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 up to 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,
       ...
    

提交回复
热议问题