Verified Commit baf0e409 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3258430 by mattjones86, smustgrave, larowlan, Berdir: Block Content...

Issue #3258430 by mattjones86, smustgrave, larowlan, Berdir: Block Content database index for reusable column
parent f660bb4c
Loading
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 * Install, update and uninstall functions for the block_content module.
 */

use Drupal\block_content\BlockContentStorageSchema;
use Drupal\Core\Entity\Form\RevisionDeleteForm;
use Drupal\Core\Entity\Form\RevisionRevertForm;
use Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider;
@@ -50,3 +51,15 @@ function block_content_update_10200() {
    $definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
  }
}

/**
 * Apply index to reusable column.
 */
function block_content_update_10300(): void {
  $manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $manager->getEntityType('block_content')
    ->setHandlerClass('storage_schema', BlockContentStorageSchema::class);
  $manager->updateEntityType($entity_type);
  $manager->updateFieldStorageDefinition(\Drupal::service('entity_field.manager')
    ->getBaseFieldDefinitions('block_content')['reusable']);
}
+27 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\block_content;

use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

/**
 * Defines the block content schema handler.
 */
class BlockContentStorageSchema extends SqlContentEntityStorageSchema {

  /**
   * {@inheritdoc}
   */
  protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping): array {
    $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
    $field_name = $storage_definition->getName();

    if ($table_name === $this->storage->getDataTable() && $field_name === 'reusable') {
      $this->addSharedTableFieldIndex($storage_definition, $schema);
    }

    return $schema;
  }

}
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
 *   bundle_label = @Translation("Block type"),
 *   handlers = {
 *     "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
 *     "storage_schema" = "Drupal\block_content\BlockContentStorageSchema",
 *     "access" = "Drupal\block_content\BlockContentAccessControlHandler",
 *     "list_builder" = "Drupal\block_content\BlockContentListBuilder",
 *     "view_builder" = "Drupal\block_content\BlockContentViewBuilder",
+34 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\block_content\Functional\Update;

use Drupal\Core\Database\Database;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;

/**
 * Tests the upgrade path for Block Content reusable column index.
 *
 * @group block_content
 */
class BlockContentReusableIndexUpdatePathTest extends UpdatePathTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setDatabaseDumpFiles(): void {
    $this->databaseDumpFiles = [
      __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.bare.standard.php.gz',
    ];
  }

  /**
   * Tests the upgrade path for Block Content reusable index.
   */
  public function testRunUpdates(): void {
    $connection = Database::getConnection();
    $this->assertFalse($connection->schema()->indexExists('block_content_field_data', 'block_content_field__reusable'), 'Block Content reusable index not yet added.');
    $this->runUpdates();
    $this->assertTrue($connection->schema()->indexExists('block_content_field_data', 'block_content_field__reusable'), 'Block Content reusable index has been added.');
  }

}