From b314aefc0a3fa19e8f56e4c726553ed2eef2bad8 Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Thu, 7 Jul 2022 17:52:43 +0300 Subject: [PATCH] reverted the removal of ALWAYS_UPDATE_META option. --- src/Commands/State/ImportCommand.php | 10 +++ src/Libs/Mappers/Import/DirectMapper.php | 82 ++++++++++++------------ src/Libs/Mappers/Import/MemoryMapper.php | 50 ++++++++------- src/Libs/Options.php | 1 + 4 files changed, 79 insertions(+), 64 deletions(-) diff --git a/src/Commands/State/ImportCommand.php b/src/Commands/State/ImportCommand.php index 45fe5f79..a1d17adf 100644 --- a/src/Commands/State/ImportCommand.php +++ b/src/Commands/State/ImportCommand.php @@ -60,6 +60,12 @@ class ImportCommand extends Command InputOption::VALUE_NONE, 'import metadata changes only. Works when there are records in database.' ) + ->addOption( + 'always-update-metadata', + null, + InputOption::VALUE_NONE, + 'Mapper option. Always update the locally stored metadata from backend.' + ) ->addOption('show-messages', null, InputOption::VALUE_NONE, 'Show internal messages.') ->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use Alternative config file.') ->addOption('servers-filter', null, InputOption::VALUE_OPTIONAL, '[DEPRECATED] Select backends.', ''); @@ -117,6 +123,10 @@ class ImportCommand extends Command $this->db->setOptions(options: [Options::DEBUG_TRACE => true]); } + if ($input->getOption('always-update-metadata')) { + $mapperOpts[Options::MAPPER_ALWAYS_UPDATE_META] = true; + } + if ($input->getOption('direct-mapper')) { $this->mapper = new DirectMapper(logger: $this->logger, db: $this->db); } diff --git a/src/Libs/Mappers/Import/DirectMapper.php b/src/Libs/Mappers/Import/DirectMapper.php index a8b2dbb9..fa31de99 100644 --- a/src/Libs/Mappers/Import/DirectMapper.php +++ b/src/Libs/Mappers/Import/DirectMapper.php @@ -296,57 +296,59 @@ final class DirectMapper implements iImport } // -- this sometimes leads to never ending updates as data from backends conflicts. - if (true === (clone $cloned)->apply(entity: $entity, fields: $keys)->isChanged(fields: $keys)) { - try { - $localFields = array_merge($keys, [iState::COLUMN_GUIDS]); + if (true === (bool)ag($this->options, Options::MAPPER_ALWAYS_UPDATE_META)) { + if (true === (clone $cloned)->apply(entity: $entity, fields: $keys)->isChanged(fields: $keys)) { + try { + $localFields = array_merge($keys, [iState::COLUMN_GUIDS]); - $entity->guids = Guid::makeVirtualGuid( - $entity->via, - ag($entity->getMetadata($entity->via), iState::COLUMN_ID) - ); + $entity->guids = Guid::makeVirtualGuid( + $entity->via, + ag($entity->getMetadata($entity->via), iState::COLUMN_ID) + ); - $local = $local->apply( - entity: $entity, - fields: array_merge($localFields, [iState::COLUMN_EXTRA]) - ); + $local = $local->apply( + entity: $entity, + fields: array_merge($localFields, [iState::COLUMN_EXTRA]) + ); - $this->removePointers($cloned)->addPointers($local, $local->id); + $this->removePointers($cloned)->addPointers($local, $local->id); - $changes = $local->diff(fields: $localFields); + $changes = $local->diff(fields: $localFields); - if (count($changes) >= 1) { - $this->logger->notice('MAPPER: [%(backend)] updated [%(title)] metadata.', [ + if (count($changes) >= 1) { + $this->logger->notice('MAPPER: [%(backend)] updated [%(title)] metadata.', [ + 'id' => $cloned->id, + 'backend' => $entity->via, + 'title' => $cloned->getName(), + 'changes' => $local->diff(fields: $localFields), + ]); + } + + if (false === $inDryRunMode) { + $this->db->update($local); + } + + if (null === ($this->changed[$local->id] ?? null)) { + $this->actions[$local->type]['updated']++; + Message::increment("{$entity->via}.{$local->type}.updated"); + } + + $this->changed[$local->id] = $this->objects[$local->id] = $local->id; + } catch (PDOException $e) { + $this->actions[$local->type]['failed']++; + Message::increment("{$entity->via}.{$local->type}.failed"); + $this->logger->error(sprintf('MAPPER: %s', $e->getMessage()), [ 'id' => $cloned->id, - 'backend' => $entity->via, 'title' => $cloned->getName(), - 'changes' => $local->diff(fields: $localFields), + 'state' => [ + 'database' => $cloned->getAll(), + 'backend' => $entity->getAll() + ], ]); } - if (false === $inDryRunMode) { - $this->db->update($local); - } - - if (null === ($this->changed[$local->id] ?? null)) { - $this->actions[$local->type]['updated']++; - Message::increment("{$entity->via}.{$local->type}.updated"); - } - - $this->changed[$local->id] = $this->objects[$local->id] = $local->id; - } catch (PDOException $e) { - $this->actions[$local->type]['failed']++; - Message::increment("{$entity->via}.{$local->type}.failed"); - $this->logger->error(sprintf('MAPPER: %s', $e->getMessage()), [ - 'id' => $cloned->id, - 'title' => $cloned->getName(), - 'state' => [ - 'database' => $cloned->getAll(), - 'backend' => $entity->getAll() - ], - ]); + return $this; } - - return $this; } if ($this->inTraceMode()) { diff --git a/src/Libs/Mappers/Import/MemoryMapper.php b/src/Libs/Mappers/Import/MemoryMapper.php index 2d899009..5c1b3b1d 100644 --- a/src/Libs/Mappers/Import/MemoryMapper.php +++ b/src/Libs/Mappers/Import/MemoryMapper.php @@ -218,36 +218,38 @@ final class MemoryMapper implements iImport } // -- this sometimes leads to never ending updates as data from backends conflicts. - if (true === (clone $cloned)->apply(entity: $entity, fields: $keys)->isChanged(fields: $keys)) { - $localFields = array_merge($keys, [iState::COLUMN_GUIDS]); - $this->changed[$pointer] = $pointer; - Message::increment("{$entity->via}.{$entity->type}.updated"); + if (true === (bool)ag($this->options, Options::MAPPER_ALWAYS_UPDATE_META)) { + if (true === (clone $cloned)->apply(entity: $entity, fields: $keys)->isChanged(fields: $keys)) { + $localFields = array_merge($keys, [iState::COLUMN_GUIDS]); + $this->changed[$pointer] = $pointer; + Message::increment("{$entity->via}.{$entity->type}.updated"); - $entity->guids = Guid::makeVirtualGuid( - $entity->via, - ag($entity->getMetadata($entity->via), iState::COLUMN_ID) - ); + $entity->guids = Guid::makeVirtualGuid( + $entity->via, + ag($entity->getMetadata($entity->via), iState::COLUMN_ID) + ); - $this->objects[$pointer] = $this->objects[$pointer]->apply( - entity: $entity, - fields: array_merge($localFields, [iState::COLUMN_EXTRA]) - ); + $this->objects[$pointer] = $this->objects[$pointer]->apply( + entity: $entity, + fields: array_merge($localFields, [iState::COLUMN_EXTRA]) + ); - $this->removePointers($cloned)->addPointers($this->objects[$pointer], $pointer); + $this->removePointers($cloned)->addPointers($this->objects[$pointer], $pointer); - $changes = $this->objects[$pointer]->diff(fields: $keys); + $changes = $this->objects[$pointer]->diff(fields: $keys); - if (count($changes) >= 1) { - $this->logger->notice('MAPPER: [%(backend)] updated [%(title)] metadata.', [ - 'id' => $cloned->id, - 'backend' => $entity->via, - 'title' => $cloned->getName(), - 'changes' => $changes, - 'fields' => implode(',', $localFields), - ]); + if (count($changes) >= 1) { + $this->logger->notice('MAPPER: [%(backend)] updated [%(title)] metadata.', [ + 'id' => $cloned->id, + 'backend' => $entity->via, + 'title' => $cloned->getName(), + 'changes' => $changes, + 'fields' => implode(',', $localFields), + ]); + } + + return $this; } - - return $this; } if ($this->inTraceMode()) { diff --git a/src/Libs/Options.php b/src/Libs/Options.php index cf44f971..0cd06111 100644 --- a/src/Libs/Options.php +++ b/src/Libs/Options.php @@ -14,6 +14,7 @@ final class Options public const IGNORE_DATE = 'IGNORE_DATE'; public const EXPORT_ALLOWED_TIME_DIFF = 'EXPORT_TIME_DIFF'; public const RAW_RESPONSE = 'SHOW_RAW_RESPONSE'; + public const MAPPER_ALWAYS_UPDATE_META = 'ALWAYS_UPDATE_META'; public const MAPPER_DISABLE_AUTOCOMMIT = 'DISABLE_AUTOCOMMIT'; public const IMPORT_METADATA_ONLY = 'IMPORT_METADATA_ONLY'; public const MISMATCH_DEEP_SCAN = 'MISMATCH_DEEP_SCAN';