简体   繁体   中英

How does Rails know that migrations are pending?

In Rails, sometimes we get the error

ActiveRecord::PendingMigrationError

How does Rails know migrations are pending?

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.

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

and compare it to @last_check and if it is less than mtime

check_pending! is called and ActiveRecord::PendingMigrationError is shown.

       ActiveRecord::Migration.check_pending!(connection) 

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

There is a table in your application's database called schema_migrations , that has a single column called versions. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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