Commit c5ef8cd8 authored by eiriksm's avatar eiriksm Committed by mglaman

Issue #2907367 by mglaman, eiriksm, AlanHDev, bojanz: Add indexes to important fields

parent d85d85a5
......@@ -5,6 +5,7 @@
* Install, update and uninstall functions for the Order module.
*/
use Drupal\commerce\CommerceContentEntityStorageSchema;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
......@@ -207,3 +208,19 @@ function commerce_order_update_8209() {
$view->save(TRUE);
}
}
/**
* Ensure new field indexes on the order entity.
*/
function commerce_order_update_8210() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Get the current order entity type definition, ensure the storage schema
// class is set.
$entity_type = $entity_type_manager->getDefinition('commerce_order')
->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);
// Regenerate entity type indexes.
$definition_update_manager->updateEntityType($entity_type);
}
......@@ -35,6 +35,7 @@ use Drupal\profile\Entity\ProfileInterface;
* handlers = {
* "event" = "Drupal\commerce_order\Event\OrderEvent",
* "storage" = "Drupal\commerce_order\OrderStorage",
* "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
* "access" = "Drupal\commerce_order\OrderAccessControlHandler",
* "query_access" = "Drupal\commerce_order\OrderQueryAccessHandler",
* "permission_provider" = "Drupal\commerce_order\OrderPermissionProvider",
......@@ -60,6 +61,9 @@ use Drupal\profile\Entity\ProfileInterface;
* base_table = "commerce_order",
* admin_permission = "administer commerce_order",
* permission_granularity = "bundle",
* field_indexes = {
* "order_number"
* },
* entity_keys = {
* "id" = "order_id",
* "label" = "order_number",
......
......@@ -5,6 +5,7 @@
* Install, update and uninstall functions for the commerce_payment module.
*/
use Drupal\commerce\CommerceContentEntityStorageSchema;
use Drupal\commerce_payment\Entity\PaymentMethod;
use Drupal\commerce_payment\Event\PaymentEvent;
use Drupal\Core\Field\BaseFieldDefinition;
......@@ -145,3 +146,23 @@ function commerce_payment_update_8206() {
->setDisplayConfigurable('view', TRUE);
$entity_definition_update->installFieldStorageDefinition('avs_response_code_label', 'commerce_payment', 'commerce_payment', $storage_definition);
}
/**
* Ensure new field indexes on the payment and payment method entities.
*/
function commerce_payment_update_8207() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Get the current payment entity type definition, ensure the storage schema
// class is set.
$entity_type = $entity_type_manager->getDefinition('commerce_payment')
->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);
// Regenerate entity type indexes.
$definition_update_manager->updateEntityType($entity_type);
$entity_type = $entity_type_manager->getDefinition('commerce_payment_method')
->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);
$definition_update_manager->updateEntityType($entity_type);
}
......@@ -29,6 +29,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "event" = "Drupal\commerce_payment\Event\PaymentEvent",
* "list_builder" = "Drupal\commerce_payment\PaymentListBuilder",
* "storage" = "Drupal\commerce_payment\PaymentStorage",
* "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
* "form" = {
* "operation" = "Drupal\commerce_payment\Form\PaymentOperationForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
......@@ -40,6 +41,9 @@ use Drupal\Core\Field\BaseFieldDefinition;
* },
* base_table = "commerce_payment",
* admin_permission = "administer commerce_payment",
* field_indexes = {
* "remote_id"
* },
* entity_keys = {
* "id" = "payment_id",
* "bundle" = "type",
......
......@@ -31,6 +31,7 @@ use Drupal\profile\Entity\ProfileInterface;
* "list_builder" = "Drupal\commerce_payment\PaymentMethodListBuilder",
* "storage" = "Drupal\commerce_payment\PaymentMethodStorage",
* "views_data" = "Drupal\commerce\CommerceEntityViewsData",
* "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
* "form" = {
* "edit" = "Drupal\commerce_payment\Form\PaymentMethodEditForm",
* "delete" = "Drupal\commerce_payment\Form\PaymentMethodDeleteForm"
......@@ -41,6 +42,9 @@ use Drupal\profile\Entity\ProfileInterface;
* },
* base_table = "commerce_payment_method",
* admin_permission = "administer commerce_payment_method",
* field_indexes = {
* "remote_id"
* },
* entity_keys = {
* "id" = "method_id",
* "uuid" = "uuid",
......
......@@ -5,6 +5,7 @@
* Install, update and uninstall functions for the Product module.
*/
use Drupal\commerce\CommerceContentEntityStorageSchema;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Core\Field\BaseFieldDefinition;
......@@ -267,3 +268,19 @@ function commerce_product_update_8210() {
$definition_update_manager->updateFieldStorageDefinition($storage_definition);
}
}
/**
* Ensure new field indexes on the product variation entity.
*/
function commerce_product_update_8211() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Get the current product variation entity type definition, ensure the
// storage schema class is set.
$entity_type = $entity_type_manager->getDefinition('commerce_product_variation')
->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);
// Regenerate entity type indexes.
$definition_update_manager->updateEntityType($entity_type);
}
......@@ -32,6 +32,7 @@ use Symfony\Component\Routing\Exception\RouteNotFoundException;
* handlers = {
* "event" = "Drupal\commerce_product\Event\ProductVariationEvent",
* "storage" = "Drupal\commerce_product\ProductVariationStorage",
* "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
* "access" = "Drupal\commerce_product\ProductVariationAccessControlHandler",
* "permission_provider" = "Drupal\commerce_product\ProductVariationPermissionProvider",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
......@@ -53,6 +54,10 @@ use Symfony\Component\Routing\Exception\RouteNotFoundException;
* "translation" = "Drupal\content_translation\ContentTranslationHandler"
* },
* admin_permission = "administer commerce_product",
* fieldable = TRUE,
* field_indexes = {
* "sku"
* },
* translatable = TRUE,
* translation = {
* "content_translation" = {
......
......@@ -5,6 +5,7 @@
* Install, update and uninstall functions for the commerce_promotion module.
*/
use Drupal\commerce\CommerceContentEntityStorageSchema;
use Drupal\Core\Field\BaseFieldDefinition;
/**
......@@ -160,3 +161,19 @@ function commerce_promotion_update_8205() {
$entity_definition_update->installFieldStorageDefinition('usage_limit_customer', 'commerce_promotion', 'commerce_promotion', $storage_definition);
$entity_definition_update->installFieldStorageDefinition('usage_limit_customer', 'commerce_promotion_coupon', 'commerce_promotion', $storage_definition);
}
/**
* Ensure new field indexes on the coupon entity.
*/
function commerce_promotion_update_8206() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Get the current coupon entity type definition, ensure the storage schema
// class is set.
$entity_type = $entity_type_manager->getDefinition('commerce_promotion_coupon')
->setHandlerClass('storage_schema', CommerceContentEntityStorageSchema::class);
// Regenerate entity type indexes.
$definition_update_manager->updateEntityType($entity_type);
}
......@@ -24,6 +24,7 @@ use Drupal\Core\Entity\EntityTypeInterface;
* "event" = "Drupal\commerce_promotion\Event\CouponEvent",
* "list_builder" = "Drupal\commerce_promotion\CouponListBuilder",
* "storage" = "Drupal\commerce_promotion\CouponStorage",
* "storage_schema" = "Drupal\commerce\CommerceContentEntityStorageSchema",
* "access" = "Drupal\commerce_promotion\CouponAccessControlHandler",
* "views_data" = "Drupal\commerce\CommerceEntityViewsData",
* "form" = {
......@@ -40,6 +41,9 @@ use Drupal\Core\Entity\EntityTypeInterface;
* },
* base_table = "commerce_promotion_coupon",
* admin_permission = "administer commerce_promotion",
* field_indexes = {
* "code"
* },
* entity_keys = {
* "id" = "id",
* "label" = "code",
......
<?php
namespace Drupal\commerce;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Defines a schema handler that supports defining base field indexes.
*/
class CommerceContentEntityStorageSchema extends SqlContentEntityStorageSchema {
/**
* {@inheritdoc}
*/
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
parent::onEntityTypeUpdate($entity_type, $original);
// ::onEntityTypeUpdate will only populate new indexes if the entity has
// a change on being translatable, revisionable, or a field change.
// @see https://www.drupal.org/project/drupal/issues/3005447
$entity_schema = $this->getEntitySchema($entity_type, TRUE);
$schema_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
$schema_indexes = $entity_schema[$schema_table]['indexes'];
foreach ($schema_indexes as $index_name => $index_fields) {
if (!$this->database->schema()->indexExists($schema_table, $index_name)) {
$this->database->schema()
->addIndex($schema_table, $index_name, $index_fields, $entity_schema[$schema_table]);
}
}
}
/**
* {@inheritdoc}
*/
protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) {
$schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
$entity_type = $this->entityTypeManager->getDefinition($storage_definition->getTargetEntityTypeId());
$field_indexes = $entity_type->get('field_indexes');
foreach ($field_indexes as $field_name) {
if ($field_name == $storage_definition->getName()) {
$this->addSharedTableFieldIndex($storage_definition, $schema);
}
}
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