protected function MultiversionStorageSchemaConverter::copyData in Multiversion 8.2
1 call to MultiversionStorageSchemaConverter::copyData()
- MultiversionStorageSchemaConverter::convertToMultiversionable in src/Entity/Storage/Sql/MultiversionStorageSchemaConverter.php
File
- src/Entity/Storage/Sql/MultiversionStorageSchemaConverter.php, line 417
Class
- MultiversionStorageSchemaConverter
Namespace
Drupal\multiversion\Entity\Storage\Sql
Code
protected function copyData(array &$sandbox) {
$temporary_table_mapping = $sandbox['temporary_table_mapping'];
$temporary_entity_type = $sandbox['temporary_entity_type'];
$original_table_mapping = $sandbox['original_table_mapping'];
$original_entity_type = $sandbox['original_entity_type'];
$original_base_table = $original_entity_type
->getBaseTable();
$revision_id_key = $temporary_entity_type
->getKey('revision');
$published_key = $temporary_entity_type
->getKey('published');
$revision_default_key = $temporary_entity_type
->getRevisionMetadataKey('revision_default');
$revision_translation_affected_key = $temporary_entity_type
->getKey('revision_translation_affected');
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
}
if (!isset($sandbox[$this->entityTypeId]['progress'])) {
$sandbox[$this->entityTypeId]['progress'] = 0;
}
$id = $original_entity_type
->getKey('id');
$entity_ids = $this->database
->select($original_base_table)
->fields($original_base_table, [
$id,
])
->condition($id, $sandbox['current_id'], '>')
->orderBy($id, 'ASC')
->range(0, $sandbox['step_size'] ?: 50)
->execute()
->fetchAllKeyed(0, 0);
$storage = $this->entityTypeManager
->getStorage($temporary_entity_type
->id());
$storage
->setEntityType($original_entity_type);
$storage
->setTableMapping($original_table_mapping);
$entities = $storage
->loadMultiple($entity_ids);
$storage
->setEntityType($temporary_entity_type);
$storage
->setTableMapping($temporary_table_mapping);
$this->entityTypeManager
->clearCachedDefinitions();
foreach ($entities as $entity_id => $entity) {
try {
$entity
->set($revision_id_key, $entity_id);
$entity
->set($revision_default_key, TRUE);
$entity
->set($published_key, TRUE);
$rev_token = '1-' . md5($entity
->id() . $entity
->uuid() . $this->random
->string(10, TRUE));
$entity
->set('_rev', $rev_token);
$entity->_rev->new_edit = FALSE;
$entity
->set('_deleted', FALSE);
if ($temporary_entity_type
->isTranslatable()) {
$entity
->set($revision_translation_affected_key, TRUE);
}
$entity
->enforceIsNew(TRUE);
$storage
->save($entity);
if ($original_entity_type
->id() == 'menu_link_content' && $this->database
->schema()
->tableExists('menu_tree')) {
$this->database
->delete('menu_tree')
->condition('id', 'menu_link_content:' . $entity
->uuid())
->execute();
}
} catch (\Exception $e) {
$this
->restoreOriginalDefinitions($sandbox);
foreach ($temporary_table_mapping
->getTableNames() as $table_name) {
$this->database
->schema()
->dropTable($table_name);
}
throw new EntityStorageException("The entity update process failed while processing the entity {$original_entity_type->id()}:{$entity_id}.", $e
->getCode(), $e);
}
$sandbox['progress']++;
$sandbox[$this->entityTypeId]['progress']++;
$sandbox['current_id'] = $entity_id;
}
$max = 0;
foreach ($sandbox['base_tables'] as $entity_type_id => $base_table) {
$entities_count = $this->database
->select($sandbox['base_tables'][$entity_type_id])
->countQuery()
->execute()
->fetchField();
$sandbox[$entity_type_id]['max'] = $entities_count;
$max += $entities_count;
}
$sandbox['max'] = $max;
$sandbox[$this->entityTypeId]['finished'] = empty($sandbox[$this->entityTypeId]['max']) ? 1 : $sandbox[$this->entityTypeId]['progress'] / $sandbox[$this->entityTypeId]['max'];
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}