Skip to content
Snippets Groups Projects
Commit e67a0836 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3494480 by alexpott: Query in...

Issue #3494480 by alexpott: Query in \Drupal\entity_usage_updater\EntityUsageUpdater::getHostEntities() is broken on large sites
parent 537aa544
Branches
Tags
1 merge request!11Use more performant queries instead of a single query to find host entities
Pipeline #371175 passed
......@@ -468,7 +468,7 @@ class EntityUsageUpdater implements EntityUsageUpdaterInterface {
static::processRevisionReferences($entity, $context);
$revision = static::createRevision($entity);
foreach ($batch[$revision->id()] as ['field' => $field_name, 'targets' => $targets]) {
foreach ($batch[$revision->id()] as $field_name => $targets) {
// We're just updating the revision to the latest active.
$field = $revision->{$field_name};
assert($field instanceof FieldItemListInterface);
......@@ -527,15 +527,14 @@ class EntityUsageUpdater implements EntityUsageUpdaterInterface {
* 'node' => [
* // 42 is *revision* ID
* 42 => [
* [
* // The ERR field that needs updating.
* 'field' =>'field_a',
* 'field_a' => [
* // Which ERR entities need updating.
* 'targets => [1 => TRUE, 4 => TRUE],
* 1 => TRUE,
* 4 => TRUE,
* ],
* [
* 'field' => 'field_b',
* 'targets' => [3 => TRUE],
* 'field_b' => [
* 3 => TRUE,
* ],
* ],
* ],
......@@ -555,7 +554,8 @@ class EntityUsageUpdater implements EntityUsageUpdaterInterface {
$is_revisionable = $storage->getEntityType()->isRevisionable();
$field_storage_definitions = $entity_field_manager->getFieldStorageDefinitions($host_entity_type_id);
foreach ($field_map as $field_name => $field_data) {
$targets = [];
foreach ($entity_ids as ['id' => $entity_id, 'vid' => $revision_id, 'entity_type' => $entity_type_id]) {
if ($field_storage_definitions[$field_name]->getSetting('target_type') === $entity_type_id) {
// As ERR fields shouldn't be marked as translatable, we don't set the
// language (which won't work with content moderation until #3088341
// lands anyway).
......@@ -564,26 +564,14 @@ class EntityUsageUpdater implements EntityUsageUpdaterInterface {
if ($is_revisionable) {
$query->allRevisions();
}
$fields_conditions = $query->orConditionGroup();
foreach ($entity_ids as ['id' => $entity_id, 'vid' => $revision_id, 'entity_type' => $entity_type_id]) {
if ($field_storage_definitions[$field_name]->getSetting('target_type') === $entity_type_id) {
$field_condition = $query->andConditionGroup();
$field_condition->condition("$field_name.target_id", $entity_id);
$field_condition->condition("$field_name.target_revision_id", $revision_id);
$fields_conditions->condition($field_condition);
$targets[$entity_id] = TRUE;
}
}
$query->condition($fields_conditions);
$results = $query->execute();
$query->condition("$field_name.target_id", $entity_id);
$query->condition("$field_name.target_revision_id", $revision_id);
// Ignore revision ID, just assume it's the active ID for the entity.
// (In case the entity gets updated in the meantime by this script.)
foreach ($results as $id) {
$out[$host_entity_type_id][$id][] = [
'field' => $field_name,
'targets' => $targets,
];
foreach ($query->execute() as $id) {
$out[$host_entity_type_id][$id][$field_name][$entity_id] = TRUE;
}
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment