Commit cbeff372 authored by Derimagia's avatar Derimagia

Revert config on import

parent 4b36b845
......@@ -14,4 +14,10 @@ cli coloring and named variable debugging.
Options are:
* `--migrate-debug` - Prints out rows as they run.
Can be used in `migrate-import` or `migrate-status` and will revert
existing migrations to the default and clear the cache for them.
This requires [config_update](https://www.drupal.org/project/config_update)
if you use [migrate_plus](https://www.drupal.org/project/migrate_plus)
because migrations go into config.
* `--migrate-debug-pre` - Same as above before the process is run on the row.
Can be used in `migrate-import`.
......@@ -4,6 +4,12 @@
* @file
* File for Drush Integration.
*/
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
use Drupal\config_update\ConfigListInterface;
use Drupal\config_update\ConfigRevertInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\migrate_plus\Plugin\Discovery\ConfigEntityDiscovery;
use Drush\Log\LogLevel;
/**
* Implements hook_drush_help_alter().
......@@ -13,4 +19,94 @@ function migrate_devel_drush_help_alter(&$command) {
$command['options']['migrate-debug'] = 'Enable Debug Mode';
$command['options']['migrate-debug-pre'] = 'Enable Debug Mode (Before Row Save)';
}
if ($command['command'] == 'migrate-status') {
$command['options']['migrate-debug'] = 'Enable Debug Mode';
}
}
/**
* Implements hook_drush_command_alter().
*/
function migrate_devel_drush_command_alter(&$command) {
$cmd = $command['command'];
if ($cmd == 'migrate-import' || $cmd == 'migrate-status') {
// Reset all migrations
if (drush_get_option('migrate-debug')) {
migrate_devel_rebuild_migrations();
}
}
}
/**
* Clears cache for migrations and reverts config of migrations if needed.
*/
function migrate_devel_rebuild_migrations() {
/* @var $discovery CachedDiscoveryInterface */
if (\Drupal::moduleHandler()->moduleExists('migrate_plus')) {
$discovery = \Drupal::service('plugin.manager.config_entity_migration');
migrate_devel_revert_migrate_config();
} else {
$discovery = \Drupal::service('plugin.manager.migration');
}
// Reset cached migrations
$discovery->clearCachedDefinitions();
drush_log(dt('Reset Cached Migrations'), LogLevel::DEBUG);
}
/**
* Reverts migrate config for migrate_plus
*/
function migrate_devel_revert_migrate_config() {
// If migrate_plus and config_update exists, revert the config.
if (\Drupal::moduleHandler()->moduleExists('config_update')) {
/* @var $config_revert ConfigRevertInterface */
$config_revert = \Drupal::service('config_update.config_update');
$migration_discovery = new ConfigEntityDiscovery('migration');
foreach ($migration_discovery->getDefinitions() as $definition) {
$config_revert->revert('migration', $definition['id']);
}
drush_log(dt('Reverted Existing Migration Configs'));
// Revert missing config
migrate_devel_revert_missing_config('migration');
} else {
drush_log(dt('Missing config_update for revert.'));
}
}
/**
* Reverts missing config for a specific config type.
*
* @param string $type
*/
function migrate_devel_revert_missing_config($type) {
/* @var ConfigRevertInterface $config_revert */
$config_revert = \Drupal::service('config_update.config_update');
// Now we need to add any new migrations.
/* @var ConfigListInterface $config_lister */
$config_lister = \Drupal::service('config_update.config_list');
// Add any new migrations we need to.
list($active_list, $install_list, $optional_list) = $config_lister->listConfig('type', $type);
$missing = array_diff($install_list, $active_list);
/* @var StorageInterface $config_reader */
$config_reader = \Drupal::service('config_update.extension_storage');
foreach ($missing as $name) {
$config = $config_reader->read($name);
$config_type = $config_lister->getTypeNameByConfigName($name);
$definition = $config_lister->getType($config_type);
$id_key = $definition->getKey('id');
if ($config_revert->import($config_type, $config[$id_key])) {
drush_log(dt('Imported @config', ['@config' => $name]), LogLevel::ALERT);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment