Fix ActiveRecord::Migrator#needs_migration? inconsistency (fix #10856)

Issue (see #10856 too):
  Consider the situation when there are 3 migrations:
    * 1st is up,
    * 2nd is down,
    * 3rd is up.
  In such case `ActiveRecord::Migrator.needs_migration?` returns `false`,
  but it should return `true`.

Consequences:
  Pending migrations check in development environment will not work
  (see `config.active_record.migration_error` option).

Solution:
  Consider all migrations while checking for pending ones
  (not only the latest migration).

Tests were added for the issue and `ActiveRecord::Migration::CheckPending`
middleware.