Repository: rails/rails
File: activerecord/lib/active_record/migration.rb (Ruby)

Code before:

        migrations = pending_migrations.flatten

        if migrations.any?
          raise ActiveRecord::PendingMigrationError.new(pending_migrations: migrations)
        end
      end

      def load_schema_if_pending!
        any_schema_needs_update = false
        pending_migrations = []

        db_configs_in_current_env.each do |db_config|
          ActiveRecord::PendingMigrationConnection.with_temporary_pool(db_config) do |pool|
            any_schema_needs_update ||= schema_needs_update?(db_config, pool)

            if pending = pool.migration_context.open.pending_migrations
              pending_migrations << pending
            end
          end
        end

        if any_schema_needs_update
          load_schema!
        else
          check_pending_migrations(pending_migrations.flatten)
        end
      end

      def maintain_test_schema! # :nodoc:
        if ActiveRecord.maintain_test_schema
          suppress_messages { load_schema_if_pending! }
        end
      end

      def method_missing(name, ...) # :nodoc:
        nearest_delegate.send(name, ...)
      end

      def migrate(direction)
        new.migrate direction
      end

      # Disable the transaction wrapping this migration.
      # You can still create your own transactions even after calling #disable_ddl_transaction!
      #
      # For more details read the {"Transactional Migrations" section above}[rdoc-ref:Migration].
      def disable_ddl_transaction!
        @disable_ddl_transaction = true
      end


Review comment (bug):
Another issue that is causing the tests to fail is that if we called `load_schema!` the list of pending migrations is no longer valid.

Code after:

        migrations = pending_migrations.flatten

        if migrations.any?
          raise ActiveRecord::PendingMigrationError.new(pending_migrations: migrations)
        end
      end

      def load_schema_if_pending!
        any_schema_needs_update = false
        pending_migrations = []

        db_configs_in_current_env.each do |db_config|
          ActiveRecord::PendingMigrationConnection.with_temporary_pool(db_config) do |pool|
            any_schema_needs_update ||= schema_needs_update?(db_config, pool)

            pending = pool.migration_context.open.pending_migrations
            pending_migrations.concat(pending)
          end
        end

        if any_schema_needs_update
          load_schema!

          pending_migrations = []
          db_configs_in_current_env.each do |db_config|
            ActiveRecord::PendingMigrationConnection.with_temporary_pool(db_config) do |pool|
              pending = pool.migration_context.open.pending_migrations
              pending_migrations.concat(pending)
            end
          end
        end

        check_pending_migrations(pending_migrations)
      end

      def maintain_test_schema! # :nodoc:
        if ActiveRecord.maintain_test_schema
          suppress_messages { load_schema_if_pending! }
        end
      end

      def method_missing(name, ...) # :nodoc:
        nearest_delegate.send(name, ...)
      end

      def migrate(direction)
        new.migrate direction
      end

      # Disable the transaction wrapping this migration.