简体   繁体   English

Rails如何知道迁移正在等待中?

[英]How does Rails know that migrations are pending?

In Rails, sometimes we get the error 在Rails中,有时我们会收到错误消息

ActiveRecord::PendingMigrationError

How does Rails know migrations are pending? Rails如何知道迁移正在等待中?

Where is that flag/information stored? 该标志/信息存储在哪里?

When model or migration is created a time-stamp is added along to the filename. 创建模型或迁移时,会将时间戳添加到文件名中。

         20160727050119_create_user.rb
         #time-stamp 20160727050119

Then a method call(env) that retrieve the last migration stamp. 然后,一个方法call(env)检索上一个迁移标记。

       `mtime = ActiveRecord::Migrator.last_migration.mtime.to_i`

and compare it to @last_check and if it is less than mtime 并将其与@last_check比较,如果小于mtime

check_pending! is called and ActiveRecord::PendingMigrationError is shown. 被调用,并显示ActiveRecord::PendingMigrationError

       ActiveRecord::Migration.check_pending!(connection) 

check_pending! check_pending!

      def check_pending!(connection = Base.connection)
        raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)
      end

You can find all this info .../lib/active-record/migration.rb 您可以找到所有这些信息.../lib/active-record/migration.rb

There is a table in your application's database called schema_migrations , that has a single column called versions. 应用程序的数据库中有一个名为schema_migrations的表,该表只有一个列,称为版本。 There will be a row for every migration that has been run. 每个已运行的迁移都会有一行。 If there is a migration file on disc, whose timestamp is not included in the schema_migrations table, then Rails knows that migrations need to be run. 如果磁盘上有一个迁移文件,其时间戳未包含在schema_migrations表中,则Rails知道需要运行迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM