Commit 62d86ade authored by jsacksick's avatar jsacksick Committed by jsacksick
Browse files

Issue #3089661 by AlanHDev, jsacksick, bojanz, tbradbury, rszrama: Improve...

Issue #3089661 by AlanHDev, jsacksick, bojanz, tbradbury, rszrama: Improve Order page load times with large amounts of logs.
parent 536f9686
<?php
/**
* @file
* Install, update and uninstall functions for the commerce_log module.
*/
use Drupal\commerce_log\LogStorageSchema;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Add a "source_entity" index and reduce the "source_entity_type" maxlength.
*/
function commerce_log_update_8201() {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_field_manager->clearCachedFieldDefinitions();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$database = \Drupal::database();
// Resize the source_entity_type field.
$database->query("ALTER TABLE {commerce_log} MODIFY source_entity_type VARCHAR(32)");
// Update storage schema.
$storage_key = 'commerce_log.field_schema_data.source_entity_type';
$storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
$field_schema = $storage_schema->get($storage_key);
$field_schema['commerce_log']['fields']['source_entity_type']['length'] = EntityTypeInterface::ID_MAX_LENGTH;
$storage_schema->set($storage_key, $field_schema);
$key = 'commerce_log.field_storage_definitions';
$entity_definitions_installed = \Drupal::keyValue('entity.definitions.installed');
$definitions = $entity_definitions_installed->get($key);
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions */
$base_field_definitions = $entity_field_manager->getBaseFieldDefinitions('commerce_log');
$definitions['source_entity_type'] = $base_field_definitions['source_entity_type']->getFieldStorageDefinition();
$entity_definitions_installed->set($key, $definitions);
// Get the current log entity type definition, ensure the storage schema
// class is set.
$entity_type = $entity_type_manager->getDefinition('commerce_log')
->setHandlerClass('storage_schema', LogStorageSchema::class);
// Regenerate entity type indexes.
$definition_update_manager->updateEntityType($entity_type);
}
......@@ -22,6 +22,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "access" = "Drupal\commerce_log\LogAccessControlHandler",
* "list_builder" = "Drupal\commerce_log\LogListBuilder",
* "storage" = "Drupal\commerce_log\LogStorage",
* "storage_schema" = "Drupal\commerce_log\LogStorageSchema",
* "view_builder" = "Drupal\commerce_log\LogViewBuilder",
* "views_data" = "Drupal\commerce\CommerceEntityViewsData",
* },
......@@ -171,7 +172,8 @@ class Log extends ContentEntityBase implements LogInterface {
$fields['source_entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Source entity type'))
->setDescription(t('The source entity type'));
->setDescription(t('The source entity type'))
->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);
$fields['params'] = BaseFieldDefinition::create('map')
->setLabel(t('Params'))
......
<?php
namespace Drupal\commerce_log;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
/**
* Defines the log schema handler.
*/
class LogStorageSchema extends SqlContentEntityStorageSchema {
/**
* {@inheritdoc}
*/
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
$schema[$this->storage->getBaseTable()]['indexes'] += [
'source_entity' => [
'source_entity_id',
'source_entity_type',
],
];
return $schema;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment