Skip to content
Snippets Groups Projects
Commit b9ead8e8 authored by Zoltan Attila Horvath's avatar Zoltan Attila Horvath Committed by Zoltan Attila Horvath
Browse files

Issue #3307552 by huzooka: Outsource migration-specific cache clearing

parent a8ded3af
Branches
Tags
No related merge requests found
......@@ -9,7 +9,6 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\UserSession;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate_drupal\Plugin\MigrationWithFollowUpInterface;
......@@ -253,22 +252,9 @@ class MigrateRunnerCommands extends OriginalMigrateRunnerCommands {
* {@inheritdoc}
*/
protected function executeMigration(MigrationInterface $migration, string $migrationId, array $userData): void {
// We have to drop stale entity caches - some base fields (e.g. provided by
// Content Translation) might be installed after the default translations
// are imported. If we don't drop cached entities and try to import
// entity translations, the entity storage will return a stale entity
// without the values of the base fields which were installed later.
// @todo Consider killing risky caches during migration instead.
if ($migration->getDestinationPlugin() instanceof EntityContentBase) {
$entity_type_id = explode(':', $migration->getDestinationConfiguration()['plugin'])[1] ?? NULL;
// Book destination plugin does not correspond to an entity type ID.
if ($entity_type_id) {
$this->entityTypeManager->getStorage($entity_type_id)->resetCache();
}
}
$migration = $this->getFreshMigrationInstance($migration->id());
static::prepareMigration($migration);
$this->prepareMigration($migration);
try {
parent::executeMigration($migration, $migrationId, $userData);
......
......@@ -3,6 +3,7 @@
namespace Drupal\smart_migrate_cli\Traits;
use Drupal\Core\Site\Settings;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drush\Utils\StringUtils;
......@@ -135,26 +136,60 @@ trait MigrateRunnerTrait {
/**
* Prepares a migration plugin instance before being executed.
*
* Right now, this method only updates file migration's base path accordingly,
* basically the same way how Migrate Drupal UI's MigrateUpgradeImportBatch
* does it.
*
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The migration to prepare for execution.
*/
protected function prepareMigration(MigrationInterface $migration): void {
$this->ensureUptodateCaches($migration);
$this->prepareFileMigrations($migration);
}
/**
* Adds the relevant source path to file migration source plugins.
*
* This method updates file migration's base path the same way how Migrate
* Drupal UI's MigrateUpgradeImportBatch does it.
*
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* A migration plugin instance.
*
* @see \Drupal\migrate_drupal_ui\Batch\MigrateUpgradeImportBatch::run()
*/
protected static function prepareMigration(MigrationInterface $migration): void {
if ($migration->getDestinationConfiguration()['plugin'] === 'entity:file') {
// Use the private file path if the scheme property is set in the source
// plugin definition and is 'private' otherwise use the public file path.
$source_config = $migration->getSourceConfiguration();
$scheme = $source_config['scheme'] ?? NULL;
$base_path = ($scheme === 'private') && Settings::get('source_private_file_path')
? Settings::get('source_private_file_path', '')
: Settings::get('source_base_path', '');
$source_config['constants']['source_base_path'] = rtrim($base_path, '/');
$migration->set('source', $source_config);
protected function prepareFileMigrations(MigrationInterface $migration): void {
if ($migration->getDestinationConfiguration()['plugin'] !== 'entity:file') {
return;
}
// Use the private file path if the scheme property is set in the source
// plugin definition and is 'private' otherwise use the public file path.
$source_config = $migration->getSourceConfiguration();
$scheme = $source_config['scheme'] ?? NULL;
$base_path = ($scheme === 'private') && Settings::get('source_private_file_path')
? Settings::get('source_private_file_path', '')
: Settings::get('source_base_path', '');
$source_config['constants']['source_base_path'] = rtrim($base_path, '/');
$migration->set('source', $source_config);
}
/**
* Resets relevant content entity storage cache.
*
* @param \Drupal\migrate\Plugin\MigrationInterface $migration
* The migration plugin instance.
*/
protected function ensureUptodateCaches(MigrationInterface $migration): void {
if (!$migration->getDestinationPlugin() instanceof EntityContentBase) {
return;
}
// We have to drop stale entity caches - some base fields (e.g. provided by
// Content Translation) might be installed after the default translations
// are imported. If we don't drop cached entities and try to import
// entity translations, the entity storage will return a stale entity
// without the values of the base fields which were installed later.
$entity_type_id = explode(':', $migration->getDestinationConfiguration()['plugin'])[1] ?? NULL;
// Book destination plugin does not correspond to an entity type ID.
if ($entity_type_id) {
$this->entityTypeManager->getStorage($entity_type_id)->resetCache();
}
}
......
......@@ -6,7 +6,6 @@ namespace Drupal\smart_migrate_cli\Traits;
use Consolidation\SiteProcess\SiteProcess;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\migrate\id_map\Sql;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_drupal\Plugin\MigrationWithFollowUpInterface;
......@@ -540,21 +539,7 @@ trait SmartMigrateHelperTrait {
* @see \Drush\Drupal\Commands\core\MigrateRunnerCommands::executeMigration()
*/
protected function executeMigration(MigrationInterface $migration): array {
// We have to drop stale entity caches - some base fields (e.g. provided by
// Content Translation) might be installed after the default translations
// are imported. If we don't drop cached entities and try to import
// entity translations, the entity storage will return a stale entity
// without the values of the base fields which were installed later.
// @todo Consider killing risky caches during migration instead.
if ($migration->getDestinationPlugin() instanceof EntityContentBase) {
$entity_type_id = explode(':', $migration->getDestinationConfiguration()['plugin'])[1] ?? NULL;
// Book destination plugin does not correspond to an entity type ID.
if ($entity_type_id) {
$this->entityTypeManager->getStorage($entity_type_id)->resetCache();
}
}
static::prepareMigration($migration);
$this->prepareMigration($migration);
$executable = new SmartMigrateExecutable($migration, $this->getMigrateMessage(), $this->output());
drush_op([$executable, 'import']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment