Commit 25cba046 authored by Erik Stielstra's avatar Erik Stielstra Committed by Erik Stielstra
Browse files

Issue #3265494 by Sutharsan: Remove radioactivity_incident table at un-install

parent 60f06e42
Loading
Loading
Loading
Loading
+46 −1
Original line number Diff line number Diff line
@@ -28,6 +28,40 @@ function radioactivity_requirements($phase) {
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function radioactivity_schema() {
  $schema['radioactivity_incident'] = [
    'description' => 'Stores radioactivity incidents.',
    'fields' => [
      'iid' => [
        'description' => 'Unique incident ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'entity_type' => [
        'description' => 'The entity type this incident belongs to.',
        'type' => 'varchar_ascii',
        'length' => 64,
        'not null' => TRUE,
      ],
      'incident' => [
        'description' => 'The incident object.',
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'normal',
      ],
    ],
    'primary key' => ['iid'],
    'indexes' => [
      'entity_type' => ['entity_type'],
    ],
  ];

  return $schema;
}

/**
 * Implements hook_update_dependencies().
 */
@@ -90,9 +124,20 @@ function radioactivity_update_9002() {
}

/**
 * Move incidents from Drupal state storage into new database storage.
 * Create radioactivity_incident table if not yet exists.
 */
function radioactivity_update_9003() {
  $database_schema = \Drupal::database()->schema();
  if (!$database_schema->tableExists('radioactivity_incident')) {
    $schema_definition = radioactivity_schema()['radioactivity_incident'];
    $database_schema->createTable('radioactivity_incident', $schema_definition);
  }
}

/**
 * Move incidents from Drupal state storage into new database storage.
 */
function radioactivity_update_9004() {
  $state = \Drupal::state();
  /** @var \Drupal\radioactivity\IncidentStorageInterface $storage */
  $storage = \Drupal::service('radioactivity.default_incident_storage');
+27 −127
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ namespace Drupal\radioactivity;

use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;

/**
 * Defines a default incident storage.
@@ -47,22 +46,6 @@ class DefaultIncidentStorage implements IncidentStorageInterface {
   * {@inheritdoc}
   */
  public function addIncident(IncidentInterface $incident) {
    try {
      $this->doAdd($incident);
    }
    catch (\Exception $exception) {
      $this->ensureTableExists();
      $this->doAdd($incident);
    }
  }

  /**
   * Adds incident data to the database.
   *
   * @param \Drupal\radioactivity\IncidentInterface $incident
   *   The incident object.
   */
  protected function doAdd(IncidentInterface $incident) {
    $this->connection->insert(self::TABLE_NAME)
      ->fields([
        'incident' => $this->serializer->encode($incident),
@@ -78,7 +61,7 @@ class DefaultIncidentStorage implements IncidentStorageInterface {
   *   Throws exception when an unexpected database error occurs.
   */
  public function getIncidents(): array {
    try {

    $result = $this->connection->select(self::TABLE_NAME, 'ri')
      ->fields('ri', ['iid', 'incident'])
      ->orderBy('ri.iid', 'ASC')
@@ -92,15 +75,6 @@ class DefaultIncidentStorage implements IncidentStorageInterface {
    }
    return $values;
  }
    catch (\Exception $exception) {
      if ($this->connection->schema()->tableExists(self::TABLE_NAME)) {
        throw $exception;
      }

      $this->ensureTableExists();
      return [];
    }
  }

  /**
   * {@inheritdoc}
@@ -109,7 +83,7 @@ class DefaultIncidentStorage implements IncidentStorageInterface {
   *   Throws exception when an unexpected database error occurs.
   */
  public function getIncidentsByType(string $entity_type = ''): array {
    try {

    $query = $this->connection->select(self::TABLE_NAME, 'ri');
    $query->fields('ri', ['iid', 'incident', 'entity_type']);
    $query->orderBy('ri.iid', 'ASC');
@@ -127,29 +101,15 @@ class DefaultIncidentStorage implements IncidentStorageInterface {

    return $incidents ?: [[]];
  }
    catch (\Exception $exception) {
      if ($this->connection->schema()->tableExists(self::TABLE_NAME)) {
        throw $exception;
      }

      $this->ensureTableExists();
      return [[]];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function clearIncidents() {
    try {
    $this->connection
      ->truncate(self::TABLE_NAME)
      ->execute();
  }
    catch (\Exception $exception) {
      $this->ensureTableExists();
    }
  }

  /**
   * {@inheritdoc}
@@ -160,64 +120,4 @@ class DefaultIncidentStorage implements IncidentStorageInterface {
    $page['#attached']['drupalSettings']['radioactivity']['endpoint'] = $base_url . '/radioactivity/emit';
  }

  /**
   * Check if the table exists and create it if not.
   *
   * Thanks to \Drupal\Core\Batch\BatchStorage::ensureTableExists.
   *
   * @return bool
   *   Returns true if the database table has been created.
   */
  protected function ensureTableExists() {
    try {
      $database_schema = $this->connection->schema();
      if (!$database_schema->tableExists(static::TABLE_NAME)) {
        $schema_definition = $this->schemaDefinition();
        $database_schema->createTable(static::TABLE_NAME, $schema_definition);
        return TRUE;
      }
    }
      // If another process has already created the table, attempting to
      // recreate it will throw an exception. In this case just catch the
      // exception and do nothing.
    catch (DatabaseException $e) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Defines the schema for the batch table.
   *
   * @internal
   */
  public function schemaDefinition() {
    return [
      'description' => 'Stores details about batches (processes that run in multiple HTTP requests).',
      'fields' => [
        'iid' => [
          'description' => 'Unique incident ID.',
          'type' => 'serial',
          'not null' => TRUE,
        ],
        'entity_type' => [
          'description' => 'The entity type this incident belongs to.',
          'type' => 'varchar_ascii',
          'length' => 64,
          'not null' => TRUE,
        ],
        'incident' => [
          'description' => 'The incident object.',
          'type' => 'blob',
          'not null' => FALSE,
          'size' => 'normal',
        ],
      ],
      'primary key' => ['iid'],
      'indexes' => [
        'entity_type' => ['entity_type'],
      ],
    ];
  }

}
+2 −36
Original line number Diff line number Diff line
@@ -3,11 +3,9 @@
namespace Drupal\radioactivity;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\radioactivity\Entity\Radioactivity;

/**
@@ -88,45 +86,14 @@ class RadioactivityReferenceUpdater implements RadioactivityReferenceUpdaterInte
  /**
   * {@inheritdoc}
   */
  public function updateReferenceFields(ContentEntityInterface $entity): bool {
    if (!$entity instanceof FieldableEntityInterface) {
      return FALSE;
    }

    if ($entity instanceof TranslatableInterface) {
      $languages = $entity->getTranslationLanguages();
      $entityIsUpdated = FALSE;

      foreach ($languages as $language) {
        $entityIsUpdated |= $this->doUpdateReferenceFields($entity->getTranslation($language->getId()));
      }
    }
    else {
      $entityIsUpdated = $this->doUpdateReferenceFields($entity);
    }

    return $entityIsUpdated;
  }

  /**
   * Adds missing radioactivity entities to radioactivity reference fields.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   The entity to use.
   *
   * @return bool
   *   True if the entity has been updated.
   */
  protected function doUpdateReferenceFields(ContentEntityInterface $entity): bool {
    $entityIsUpdated = FALSE;
  public function updateReferenceFields(FieldableEntityInterface $entity): bool {

    // Ignore entities that do not contain a radioactivity reference field.
    $fieldNames = $this->getReferenceFields($entity->getEntityTypeId(), $entity->bundle());
    $entityIsUpdated = FALSE;

    foreach ($fieldNames as $fieldName) {
      /** @var \Drupal\radioactivity\RadioactivityReferenceFieldItemList $radioactiveReferenceField */
      $radioactiveReferenceField = $entity->get($fieldName);

      if ($radioactiveReferenceField->isEmpty()) {

        // Create a radioactivity entity as target.
@@ -168,7 +135,6 @@ class RadioactivityReferenceUpdater implements RadioactivityReferenceUpdaterInte
      ->getKey('bundle');

    $query = $entityStorage->getQuery();
    $query->accessCheck(FALSE);
    if ($bundleKey) {
      $query->condition($bundleKey, $bundle);
    }
+2 −3
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\radioactivity;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;

/**
@@ -40,12 +39,12 @@ interface RadioactivityReferenceUpdaterInterface {
   *
   * Note that the entity may be (re-)saved during this process.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
   *   The entity to use.
   *
   * @return bool
   *   True if the entity has been updated.
   */
  public function updateReferenceFields(ContentEntityInterface $entity): bool;
  public function updateReferenceFields(FieldableEntityInterface $entity): bool;

}
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ class DefaultIncidentStorageTest extends KernelTestBase {
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installSchema('radioactivity', ['radioactivity_incident']);

    $this->sut = \Drupal::service('radioactivity.default_incident_storage');
  }
Loading