Commit 66624a8e authored by Ilias Dimopoulos's avatar Ilias Dimopoulos Committed by Paulo Henrique Cota Starling
Browse files

Issue #3258563 by AndySipple, paulocs: password_policy_update_8302 takes too...

Issue #3258563 by AndySipple, paulocs: password_policy_update_8302 takes too long for a large amount of users
parent 966f272d
Loading
Loading
Loading
Loading
+44 −7
Changes for password_policy.install: 44 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Install, update and uninstall functions for the password_policy module.
 */

use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
@@ -70,21 +72,23 @@ function password_policy_update_8301() {
}

/**
 * Import yaml config of `password_expire_sent` field.
 * Create and initialize `password_expire_sent` field.
 */
function password_policy_update_8302(&$sandbox) {
  $db_connection = \Drupal::database();
  if (!isset($sandbox['progress'])) {
    /** @var \Drupal\Core\Config\ConfigInstallerInterface $configInstaller */
    $configInstaller = \Drupal::service('config.installer');
    $configInstaller->installDefaultConfig('module', 'password_policy');

  $db_connection = \Drupal::database();
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = $db_connection->query('SELECT COUNT(uid) FROM {users}')->fetchField();
    $sandbox['current_user'] = -1;
  }

  // Run user updates in batches of 50.
  // Run user updates in batches of 50. This is not much when using direct
  // queries, but that's still plenty fast. It is plenty for updates through the
  // entity API.
  $limit = 50;

  // Retrieve the next group of uids.
@@ -96,8 +100,42 @@ function password_policy_update_8302(&$sandbox) {
    ->orderBy('u.uid', 'ASC')
    ->execute();

  $userStorage = \Drupal::entityTypeManager()->getStorage('user');

  // CHeck of the user storage is SQL based. If so, use optimized update method.
  if ($userStorage instanceof SqlContentEntityStorage) {
    $query = $db_connection->insert('user__field_pending_expire_sent')
      ->fields([
        'bundle',
        'deleted',
        'entity_id',
        'revision_id',
        'langcode',
        'delta',
        'field_pending_expire_sent_value',
      ]);

    foreach ($result as $row) {
      $sandbox['progress']++;
      $query->values([
        'bundle' => 'user',
        'deleted' => 0,
        'entity_id' => $row->uid,
        'revision_id' => $row->uid,
        'langcode' => LanguageInterface::LANGCODE_DEFAULT,
        'delta' => 0,
        'field_pending_expire_sent_value' => '0',
      ]);

      // Update our progress information.
      $sandbox['current_user'] = $row->uid;
    }

    $query->execute();
  }
  else {
    foreach ($result as $row) {
    $user = \Drupal::entityTypeManager()->getStorage('user')->load($row->uid);
      $user = $userStorage->load($row->uid);
      $sandbox['progress']++;
      if ($user->getAccountName() == NULL) {
        continue;
@@ -109,13 +147,12 @@ function password_policy_update_8302(&$sandbox) {
      // Update our progress information.
      $sandbox['current_user'] = $row->uid;
    }
  }

  $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? TRUE : ($sandbox['progress'] / $sandbox['max']);

  if ($sandbox['#finished']) {
  return t('The Password Policy module has updated user accounts. Total update count: @count', ['@count' => $sandbox['progress']]);
}
}

/**
 * Update the field_password_expiration field description.