private async Task MigrateImplementationAsync()

in EFCore/src/Migrations/Internal/MySQLMigrator.cs [320:393]


    private async Task<bool> MigrateImplementationAsync(
        DbContext context, string? targetMigration, MigrationExecutionState state, bool useTransaction, CancellationToken cancellationToken = default)
    {
      var connectionOpened = await _connection.OpenAsync(cancellationToken).ConfigureAwait(false);
      try
      {
        if (useTransaction)
        {
          state.Transaction = await (MigrationTransactionIsolationLevel == null
              ? context.Database.BeginTransactionAsync(cancellationToken)
              : context.Database.BeginTransactionAsync(MigrationTransactionIsolationLevel.Value, cancellationToken))
                  .ConfigureAwait(false);

          state.DatabaseLock = state.DatabaseLock == null
              ? await _historyRepository.AcquireDatabaseLockAsync(cancellationToken).ConfigureAwait(false)
              : await state.DatabaseLock.ReacquireIfNeededAsync(connectionOpened, useTransaction, cancellationToken)
                  .ConfigureAwait(false);
        }

        PopulateMigrations(
            (await _historyRepository.GetAppliedMigrationsAsync(cancellationToken).ConfigureAwait(false)).Select(t => t.MigrationId),
            targetMigration,
            out var migratorData);

        var commandLists = GetMigrationCommandLists(migratorData);
        foreach (var commandList in commandLists)
        {
          var (id, getCommands) = commandList;
          if (id != state.CurrentMigrationId)
          {
            state.CurrentMigrationId = id;
            state.LastCommittedCommandIndex = 0;
          }

          await _migrationCommandExecutor.ExecuteNonQueryAsync(
              getCommands(), _connection, state, commitTransaction: false, MigrationTransactionIsolationLevel, cancellationToken)
              .ConfigureAwait(false);
        }

        var coreOptionsExtension =
            _dbContextOptions.FindExtension<CoreOptionsExtension>()
            ?? new CoreOptionsExtension();

        var seedAsync = coreOptionsExtension.AsyncSeeder;
        if (seedAsync != null)
        {
          await seedAsync(context, state.AnyOperationPerformed, cancellationToken).ConfigureAwait(false);
        }
        else if (coreOptionsExtension.Seeder != null)
        {
          throw new InvalidOperationException(CoreStrings.MissingSeeder);
        }

        if (state.Transaction != null)
        {
          await state.Transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
        }
        return state.AnyOperationPerformed;
      }
      finally
      {
        if (state.DatabaseLock != null)
        {
          state.DatabaseLock.Dispose();
          state.DatabaseLock = null;
        }
        if (state.Transaction != null)
        {
          await state.Transaction.DisposeAsync().ConfigureAwait(false);
          state.Transaction = null;
        }
        await _connection.CloseAsync().ConfigureAwait(false);
      }
    }