You are here

class EntityUpdate in Entity Update 8

Same name and namespace in other branches
  1. 2.0.x src/EntityUpdate.php \Drupal\entity_update\EntityUpdate

EntityUpdate Main Class.

Hierarchy

Expanded class hierarchy of EntityUpdate

6 files declare their use of EntityUpdate
EntityUpdateExec.php in src/Form/EntityUpdateExec.php
EntityUpdateFunctionsTest.php in modules/entity_update_tests/src/Tests/EntityUpdateFunctionsTest.php
EntityUpdateProgUpTest.php in modules/entity_update_tests/src/Tests/EntityUpdateProgUpTest.php
EntityUpdateStatus.php in src/Controller/EntityUpdateStatus.php
entity_update.drush.inc in ./entity_update.drush.inc
Entity update module (entity_update).

... See full list

File

src/EntityUpdate.php, line 12

Namespace

Drupal\entity_update
View source
class EntityUpdate {

  /**
   * Update all empty entities.
   */
  public static function basicUpdate($force = FALSE) {

    // Check all updateble entities are empty.
    $flgOK = TRUE;
    try {
      $list = self::getEntityTypesToUpdate();
      foreach ($list as $item => $entity_type_changes) {
        if (!empty(\Drupal::entityQuery($item)
          ->execute())) {
          $flgOK = FALSE;
          EntityUpdatePrint::drushLog("The entity '{$item}' is not empty", 'warning');
        }
      }
    } catch (\Exception $e) {
      EntityUpdatePrint::drushLog($e
        ->getMessage(), 'error', NULL, TRUE);
      $flgOK = FALSE;
    }

    // Return if one of the entity has data.
    if (!$flgOK && !$force) {
      EntityUpdatePrint::drushLog("Cant update as basic, use --all or --force option", 'cancel', NULL, TRUE);
      EntityUpdatePrint::drushPrint("Example : drush upe --basic --force --nobackup");
      return FALSE;
    }

    // Update.
    try {
      \Drupal::entityDefinitionUpdateManager()
        ->applyUpdates();

      // No need to update = OK.
      return !\Drupal::entityDefinitionUpdateManager()
        ->needsUpdates();
    } catch (\Exception $e) {
      EntityUpdatePrint::drushLog($e
        ->getMessage(), 'warning', NULL, TRUE);
    }
    return FALSE;
  }

  /**
   * Update all entities.
   *
   * @return bool
   *   Update is done or FAIL
   */
  public static function safeUpdateMain(EntityTypeInterface $entity_type = NULL) {

    // Get entitiy types to update.
    $entity_change_summerys = self::getEntityTypesToUpdate();
    if (empty($entity_change_summerys)) {
      EntityUpdatePrint::drushLog('No entities to update.', 'cancel');
      return TRUE;
    }

    // Check entity type to update.
    if ($entity_type) {
      $entity_type_id = $entity_type
        ->id();
      foreach ($entity_change_summerys as $type_id => $entity_type_changes) {
        if ($type_id !== $entity_type_id) {
          unset($entity_change_summerys[$type_id]);
        }
      }

      // Check update for $entity_type_id.
      if (empty($entity_change_summerys)) {
        EntityUpdatePrint::drushLog("No updates for {$entity_type_id}", 'cancel');
        return TRUE;
      }
      else {

        // Run Update for $entity_type.
        return self::safeUpdateEntityType($entity_type);
      }
      return FALSE;
    }

    // Flags to select install / uninstall methode.
    $flg_has_install = FALSE;
    $flg_has_uninstall = FALSE;

    // Select the method to use.
    foreach ($entity_change_summerys as $entity_type_changes) {
      foreach ($entity_type_changes as $entity_change_summ) {
        if (strstr($entity_change_summ, "uninstalled")) {
          $flg_has_uninstall = TRUE;
        }
        else {
          $flg_has_install = TRUE;
        }
      }
    }

    // Execute Update all entities.
    if (!$flg_has_install && !$flg_has_uninstall) {

      // Install Or Uninstall not found.
      EntityUpdatePrint::drushLog('Entity install / Uninstall not found.', 'cancel');
      return FALSE;
    }
    elseif ($flg_has_install && $flg_has_uninstall) {
      EntityUpdatePrint::drushLog('Has fields to install and to uninstall. Do one action at a time.', 'cancel');
      return FALSE;
    }
    elseif ($flg_has_install) {
      return self::safeUpdateInstallFields();
    }
    elseif ($flg_has_uninstall) {
      return self::safeUpdateUninstallFields($entity_change_summerys);
    }
    EntityUpdatePrint::drushLog('UNKNOWN ERROR.', 'error');
    return FALSE;
  }

  /**
   * Update all entities / Uninstall fields.
   *
   * @return bool
   *   Update is done or FAIL
   */
  private static function safeUpdateUninstallFields($entity_change_summerys) {

    // Read and backup data into entity_update entity..
    EntityUpdatePrint::drushPrint("Read and backup data");

    // Backup and delete entities has data, get the data flag.
    $flg_has_data = self::entityUpdateDataBackupDel($entity_change_summerys);

    // Exec Update.
    EntityUpdatePrint::drushPrint("Update entity Schema");
    try {
      \Drupal::entityDefinitionUpdateManager()
        ->applyUpdates();
      EntityUpdatePrint::drushLog('Entities update success', 'ok');
    } catch (Exception $ex) {
      EntityUpdatePrint::drushLog($ex
        ->getMessage(), 'warning');
    }

    // Re create.
    if ($flg_has_data) {
      EntityUpdatePrint::drushPrint("Re creating entities.");

      // Restore entities.
      $result = self::entityUpdateDataRestore();

      // Message to Flush 'entity_update' via drush command.
      EntityUpdatePrint::drushLog("Entiti recreate Success / End", $result ? 'ok' : 'warning');
      EntityUpdatePrint::drushLog("CAUTION : Before next operation, Flush 'Entity Data' using : drush upe --clean", 'warning');
    }

    // No need to update = OK.
    return !\Drupal::entityDefinitionUpdateManager()
      ->needsUpdates();
  }

  /**
   * Backup and delete entities before an update.
   *
   * @return bool
   *   Has Entity data
   */
  public static function entityUpdateDataBackupDel($entity_change_summerys, $force_type = NULL) {

    // Force a type to backup and delete even no updates.
    if (empty($entity_change_summerys) && $force_type) {
      $entity_change_summerys[$force_type] = 1;
    }

    // Get Database connection.
    $con = Database::getConnection();
    $excludes = EntityUpdateHelper::getConfig()
      ->get('excludes');
    $flg_has_data = FALSE;

    // Read and backup data into entity_update entity..
    EntityUpdatePrint::drushPrint("Read and backup data");
    foreach ($entity_change_summerys as $entity_type_id => $entity_type_changes) {

      // Check entity types excludes.
      if (empty($excludes[$entity_type_id])) {
        EntityUpdatePrint::drushPrint(" - Reading : {$entity_type_id}");
        $entities = \Drupal::entityTypeManager()
          ->getStorage($entity_type_id)
          ->loadMultiple();
        foreach ($entities as $entity_id => $entity) {
          $entity_type = $entity
            ->getEntityType();

          // Backup entity to DB.
          $con
            ->insert('entity_update')
            ->fields([
            'entity_type' => $entity_type_id,
            'entity_id' => $entity_id,
            'entity_class' => $entity_type
              ->getClass(),
            'status' => 0,
            'data' => Json::encode($entity
              ->toArray()),
          ])
            ->execute();

          // Delete Entity.
          $entity
            ->delete();
          $flg_has_data = TRUE;
        }
        EntityUpdatePrint::drushLog("Backup ok", 'ok');
      }
      else {

        // Exclude by config.
        EntityUpdatePrint::drushLog("Deletation of {$entity_type_id} is excluded by config.", 'cancel');
      }
    }
    return $flg_has_data;
  }

  /**
   * Restore entities after an update.
   *
   * @return bool
   *   Restore success
   */
  public static function entityUpdateDataRestore() {

    // Get Database connection.
    $con = Database::getConnection();
    $db_data = $con
      ->select('entity_update', 't')
      ->fields('t')
      ->execute()
      ->fetchAll(\PDO::FETCH_ASSOC);
    if (!$db_data) {
      EntityUpdatePrint::drushLog("ERROR, Data read error", 'error');
      return FALSE;
    }
    foreach ($db_data as $row) {
      $entity_class = $row['entity_class'];
      $entity_data = Json::decode($row['data']);

      // Create Entity.
      $entity = $entity_class::create($entity_data);

      // Save entity.
      $entity
        ->save();
    }
    return TRUE;
  }

  /**
   * Update all entities / Install fields.
   *
   * @return bool
   *   Update is done or FAIL
   */
  private static function safeUpdateInstallFields() {

    // Update.
    try {
      \Drupal::entityDefinitionUpdateManager()
        ->applyUpdates();
      EntityUpdatePrint::drushLog('Entities update success', 'ok');

      // No need to update = OK.
      return !\Drupal::entityDefinitionUpdateManager()
        ->needsUpdates();
    } catch (\Exception $e) {
      EntityUpdatePrint::drushLog($e
        ->getMessage(), 'warning');
    }
    return FALSE;
  }

  /**
   * Update an entity type.
   */
  private static function safeUpdateEntityType(EntityTypeInterface $entity_type) {

    // Get entity change summerys.
    $entity_change_summerys = self::getEntityTypesToUpdate($entity_type
      ->id());
    $flg_done = FALSE;
    try {

      // Get entity update manager.
      $update_manager = entity_update_get_entity_definition_update_manager();
      $complete_change_list = $update_manager
        ->publicGetChangeList();
      if ($complete_change_list) {
        $update_manager->entityManager
          ->clearCachedDefinitions();
      }
      foreach ($complete_change_list as $entity_type_id => $change_list) {

        // Update selected entity type only.
        if ($entity_type_id === $entity_type
          ->id()) {

          // Check if has a field to install.
          $flg_has_install = FALSE;
          foreach ($entity_change_summerys[$entity_type_id] as $entity_change_summ) {
            if (strstr($entity_change_summ, "uninstalled")) {

              // Has fields to un install (Nothig todo at the moment).
            }
            else {
              $flg_has_install = TRUE;
            }
          }

          // Backup and delete entities data if no fields to install.
          if (!$flg_has_install) {
            $entity_change_summerys = [
              $entity_type_id => 1,
            ];
            $flg_has_data = self::entityUpdateDataBackupDel($entity_change_summerys);
          }
          else {
            $flg_has_data = FALSE;
          }

          // Process entity types.
          if (!empty($change_list['entity_type'])) {

            // TODO : Backup and Restore data via SQL.
            $update_manager
              ->publicDoEntityUpdate($change_list['entity_type'], $entity_type_id);
          }

          // Process field storage definition changes.
          if (!empty($change_list['field_storage_definitions'])) {
            $storage_definitions = $update_manager->entityManager
              ->getFieldStorageDefinitions($entity_type_id);
            $original_storage_definitions = $update_manager->entityManager
              ->getLastInstalledFieldStorageDefinitions($entity_type_id);
            foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
              $storage_definition = isset($storage_definitions[$field_name]) ? $storage_definitions[$field_name] : NULL;
              $original_storage_definition = isset($original_storage_definitions[$field_name]) ? $original_storage_definitions[$field_name] : NULL;
              $update_manager
                ->publicDoFieldUpdate($change, $storage_definition, $original_storage_definition);
            }
          }

          // Re create if necessary.
          if ($flg_has_data) {
            $result = self::entityUpdateDataRestore();
            if ($result) {
              self::cleanupEntityBackup();
            }
          }
          $flg_done = TRUE;
        }
      }
    } catch (Exception $e) {
      EntityUpdatePrint::drushLog($e
        ->getMessage(), 'error');
      return FALSE;
    }
    return $flg_done;
  }

  /**
   * Cleanup entity backup database.
   *
   * @return array
   *   The list of entities to update.
   */
  public static function cleanupEntityBackup() {

    // Get Database connection.
    $con = Database::getConnection();
    $con
      ->truncate('entity_update')
      ->execute();
    return TRUE;
  }

  /**
   * Get Entity types to update.
   *
   * @return array
   *   The list of entities to update.
   */
  public static function getEntityTypesToUpdate($type_id = NULL) {
    $list = entity_update_get_entity_changes();
    foreach ($list as $entity_type_id => $entity_change_summery) {
      if (!$type_id || $type_id == $entity_type_id) {
        $list[$entity_type_id] = $entity_change_summery;
      }
    }
    return $list;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityUpdate::basicUpdate public static function Update all empty entities.
EntityUpdate::cleanupEntityBackup public static function Cleanup entity backup database.
EntityUpdate::entityUpdateDataBackupDel public static function Backup and delete entities before an update.
EntityUpdate::entityUpdateDataRestore public static function Restore entities after an update.
EntityUpdate::getEntityTypesToUpdate public static function Get Entity types to update.
EntityUpdate::safeUpdateEntityType private static function Update an entity type.
EntityUpdate::safeUpdateInstallFields private static function Update all entities / Install fields.
EntityUpdate::safeUpdateMain public static function Update all entities.
EntityUpdate::safeUpdateUninstallFields private static function Update all entities / Uninstall fields.