2

I'm currently trying to migrate a site with drush migrate-upgrade --configure-only ... and copying the export migrate_plus.migration_*.yml into a custom module for tweaking. Now I want to enable that module but I get PreExistingConfigException.php and ... provided by MYMODULE already exist in active configuration.

I'd like to delete all config but it looks like the latest drush has removed drush config-delete and drupal config:delete is gone too. How do I achieve this?

  • Btw, doing drush pm-uninstall migrate_upgrade migrate_plus migrate_tools does NOT clean up these config entities either. – Nic Sep 8 '16 at 8:53
  • 1
    FWIW I'm having the same issue. Currently scouring the d.o comments for a solution. Will report back if I find something. – CR47 Sep 8 '16 at 20:54
4

This isn't the greatest solution, but it does work, and also allows you to not have to execute a dozen steps/commands to register your migrations.

In my custom module where I'm creating migration sources/plugins, I added a .install file and implemented hook_uninstall(). In there I delete the configurations remaining manually.

<?php

/**
 * @file
 * Contains my_module_migrate.install.
 */

/**
 * Implements hook_uninstall().
 *
 * Removes stale migration configs during uninstall.
 */
function my_module_migrate_uninstall() {
  $query = db_select('config', 'c')
    ->fields('c', array('name'))
    ->condition('name', db_like('migrate_plus.') . '%', 'LIKE')
    ->execute();

  $config_names = $query->fetchAll();

  // Delete each config using configFactory.
  foreach ($config_names as $config_name) {
    \Drupal::configFactory()->getEditable($config_name->name)->delete();
  }
}

Note: You may need to clear cache after uninstalling for the changes to show before re-enabling the module.

If you want to remove them before you uninstall you can either run each line from hook_uninstall() implementation in devel/php, or you can run them via drush: drush ev "\Drupal:;configFactory()->getEditable('migrate_plus.migration.xxxxxx')->delete();"
You will probably have to clear cache after removing all of them manually via drush as well.

If you'd like to just run a couple of drush commands to re-register your migrations, you can add this drush plugin file my_module.drush.inc to your modules' root directory, clear cache, and run drush mpc -y && drush mcr my_module. You may need to also run drush cache-clear drush for the commands to be come available.

Here's the code for the my_module.drush.inc file:

<?php
/**
 * @file
 * Contains my_module_migrate.drush.inc
 */

/**
 * Implements hook_drush_command().
 *
 * Defines drush commands.
 */
function my_module_migrate_drush_command() {
  // Company update Dynamically
  $items['migrate-prune-configs'] = array(
    'description' => dt('Prune migrate configs.'),
    'aliases' => array('mpc'),
    'examples' => array(
      'drush migrate-prune-configs' => 'Removes migrate configuations.',
    ),
    'callback' => 'my_module_migrate_migrate_prune_configs',
  );

  $items['module-config-reset'] = array(
    'description' => dt('Resets default config for a given module.'),
    'arguments' => [
      'module_name' => dt('Module name'),
    ],
    'aliases' => ['mcr'],
    'required-arguments' => 1,
    'callback' => 'my_module_migrate_config_reset',
  );

  return $items;
}

/**
 * Callback for drush command migrate-prune-configs to prune migratin configs.
 */
function my_module_migrate_migrate_prune_configs() {
  //Confirmation will be Y/N when use type “y” the condition will be executed if not it will not
  if (drush_confirm(dt("Are you sure you want to remove migrate configs?"))) {
    drush_print("Removing.....");
    // Get all of the migration config names.
    $query = db_select('config', 'c')
      ->fields('c', array('name'))
      ->condition('name', db_like('migrate_plus.') . '%', 'LIKE')
      ->execute();

    $config_names = $query->fetchAll();

    // Delete each config using configFactory.
    foreach ($config_names as $config_name) {
      drush_print("..." . $config_name->name);
      \Drupal::configFactory()->getEditable($config_name->name)->delete();
    }

    drush_print("Removed succesfully");

    return TRUE;
  }
}

/**
 * Reinstall default module's config files
 */
function my_module_migrate_config_reset($module_name) {
  if (!in_array($module_name, array_keys(\Drupal::moduleHandler()->getModuleList()))) {
    return drush_set_error(dt('Module @module_name not found.', ['@module_name' => $module_name]));
  }
  // Initiate config installer for given module.
  \Drupal::service('config.installer')->installDefaultConfig('module', $module_name);

  drush_print('Configuration was successfully reset.');

  return TRUE;
}

The code for resetting the module's configs was found in this issue on d.o. I found that it doesn't work unless the configs are already removed, hence the first command to remove them.

  • Tomorrow I will try to write a drush plugin that will remove migrate configs. – CR47 Sep 8 '16 at 22:26
  • Change my_module_migrate_drush_command() to the my_module_drush_command() according to the example. – Vlad Moyseenko Jan 11 '17 at 18:34
0

config:delete is in core drush 8 and drush9. it is not removed. make sure your site is bootstrapping properly for drush requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.