Skip to content

Instantly share code, notes, and snippets.

@xuxucode
Last active January 18, 2018 08:40
Show Gist options
  • Save xuxucode/2df402d3036978badc9eb1821cd963ed to your computer and use it in GitHub Desktop.
Save xuxucode/2df402d3036978badc9eb1821cd963ed to your computer and use it in GitHub Desktop.
Drupal entity_id integer and flag_id__uid index
diff --git a/flag.install b/flag.install
index ec4794b..42ba3ba 100644
--- a/flag.install
+++ b/flag.install
@@ -30,9 +30,10 @@ function flag_schema() {
],
'entity_id' => [
'description' => 'The unique ID of the flagged entity, for example the uid, cid, or nid.',
- 'type' => 'varchar_ascii',
- 'length' => ConfigEntityStorage::MAX_ID_LENGTH,
+ 'type' => 'int',
+ 'unsigned' => TRUE,
'not null' => TRUE,
+ 'default' => 0,
],
'count' => [
'description' => 'The number of times this object has been flagged for this flag.',
@@ -103,3 +104,19 @@ function flag_requirements($phase) {
*/
return $requirements;
}
+
+/**
+ * Change {flag_counts}.entity_id to an unsigned int.
+ *
+ * @link https://www.drupal.org/project/flag/issues/2921864
+ */
+function flag_update_8001() {
+ $schema = \Drupal::database()->schema();
+ $schema->changeField('flag_counts', 'entity_id', 'entity_id', [
+ 'description' => 'The unique ID of the flagged entity, for example the uid, cid, or nid.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ 'default' => 0,
+ ]);
+}
diff --git a/src/Entity/Flagging.php b/src/Entity/Flagging.php
index 7d43c29..8086c00 100644
--- a/src/Entity/Flagging.php
+++ b/src/Entity/Flagging.php
@@ -117,9 +117,14 @@ class Flagging extends ContentEntityBase implements FlaggingInterface {
->setLabel(t('Entity Type'))
->setDescription(t('The Entity Type.'));
- $fields['entity_id'] = BaseFieldDefinition::create('string')
+ // Setting to 'integer' type can make the 'flag_id__uid__entity_id' index
+ // work when executing the query 'select uid from flagging where flag_id=
+ // 'follower' and entity_id=1'. See EXPLAIN for more information.
+ // see https://www.drupal.org/project/flag/issues/2921864
+ $fields['entity_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Entity ID'))
->setRequired(TRUE)
+ ->setSetting('unsigned', TRUE)
->setDescription(t('The Entity ID.'));
$fields['flagged_entity'] = BaseFieldDefinition::create('entity_reference')
diff --git a/src/Entity/Storage/FlaggingStorageSchema.php b/src/Entity/Storage/FlaggingStorageSchema.php
index e16e6ed..cffbe04 100644
--- a/src/Entity/Storage/FlaggingStorageSchema.php
+++ b/src/Entity/Storage/FlaggingStorageSchema.php
@@ -4,6 +4,7 @@ namespace Drupal\flag\Entity\Storage;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
* Defines the flag schema handler.
@@ -19,6 +20,28 @@ class FlaggingStorageSchema extends SqlContentEntityStorageSchema {
$schema['flagging']['indexes'] += [
'entity_id__uid' => ['entity_id', 'uid'],
];
+ $schema['flagging']['indexes'] += [
+ 'flag_id__uid__entity_id' => ['flag_id', 'uid', 'entity_id'],
+ ];
+
+ return $schema;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $storage_definition, $table_name, array $column_mapping) {
+ $schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
+ $field_name = $storage_definition->getName();
+
+ if ($table_name == 'flagging') {
+ switch ($field_name) {
+ case 'entity_id':
+ // Improves the performance of the indexes defined in getEntitySchema().
+ $schema['fields'][$field_name]['not null'] = TRUE;
+ break;
+ }
+ }
return $schema;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment