From 83018b7596ab807148acc9d45b6ffef572eb61bf Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Thu, 29 Dec 2016 16:26:55 +0000
Subject: [PATCH] Revert "Revert "Issue #2834291 by claudiu.cristea, Berdir,
 amateescu, timmillwood, catch: Add a SQL index for entity types that are
 using EntityPublishedInterface""

This reverts commit d2e8cbdc2e5d6150eef7f78669a1d91807d2ad6d.
---
 .../Sql/SqlContentEntityStorageSchema.php     | 19 +++++++++++++++++++
 .../src/Tests/Update/CommentUpdateTest.php    |  3 +++
 core/modules/node/src/NodeStorageSchema.php   |  1 -
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
index 61da55f04ff6..96cea9f49db6 100644
--- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
+++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php
@@ -7,6 +7,7 @@
 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
 use Drupal\Core\Entity\ContentEntityTypeInterface;
 use Drupal\Core\Entity\EntityManagerInterface;
+use Drupal\Core\Entity\EntityPublishedInterface;
 use Drupal\Core\Entity\EntityStorageException;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
@@ -548,6 +549,24 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
         $this->processRevisionDataTable($entity_type, $schema[$tables['revision_data_table']]);
       }
 
+      // Add an index for the 'published' entity key.
+      if (is_subclass_of($entity_type->getClass(), EntityPublishedInterface::class)) {
+        $published_key = $entity_type->getKey('published');
+        if ($published_key && !$storage_definitions[$published_key]->hasCustomStorage()) {
+          $published_field_table = $table_mapping->getFieldTableName($published_key);
+          $id_key = $entity_type->getKey('id');
+          if ($bundle_key = $entity_type->getKey('bundle')) {
+            $key = "{$published_key}_{$bundle_key}";
+            $columns = [$published_key, $bundle_key, $id_key];
+          }
+          else {
+            $key = $published_key;
+            $columns = [$published_key, $id_key];
+          }
+          $schema[$published_field_table]['indexes'][$this->getEntityIndexName($entity_type, $key)] = $columns;
+        }
+      }
+
       $this->schema[$entity_type_id] = $schema;
 
       // Restore the actual definition.
diff --git a/core/modules/comment/src/Tests/Update/CommentUpdateTest.php b/core/modules/comment/src/Tests/Update/CommentUpdateTest.php
index 1bf3bdc34093..3db9c4a67b39 100644
--- a/core/modules/comment/src/Tests/Update/CommentUpdateTest.php
+++ b/core/modules/comment/src/Tests/Update/CommentUpdateTest.php
@@ -66,6 +66,9 @@ public function testPublishedEntityKey() {
     // Check that the entity key exists and it has the correct value.
     $entity_type = \Drupal::entityDefinitionUpdateManager()->getEntityType('comment');
     $this->assertEqual('status', $entity_type->getKey('published'));
+
+    // Check that the {comment_field_data} table status index has been created.
+    $this->assertTrue(\Drupal::database()->schema()->indexExists('comment_field_data', 'comment__status_comment_type'));
   }
 
 }
diff --git a/core/modules/node/src/NodeStorageSchema.php b/core/modules/node/src/NodeStorageSchema.php
index 43bd465dff78..18d6303458af 100644
--- a/core/modules/node/src/NodeStorageSchema.php
+++ b/core/modules/node/src/NodeStorageSchema.php
@@ -19,7 +19,6 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
 
     $schema['node_field_data']['indexes'] += array(
       'node__frontpage' => array('promote', 'status', 'sticky', 'created'),
-      'node__status_type' => array('status', 'type', 'nid'),
       'node__title_type' => array('title', array('type', 4)),
     );
 
-- 
GitLab