Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drush deregister improvements #4272

Merged
merged 12 commits into from
Sep 16, 2024
17 changes: 15 additions & 2 deletions docs/source/drush_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,22 @@ dkan:harvest:archive
dkan:harvest:deregister
-----------------------

Deregister a harvest.
Deregister a harvest plan. This action removes the harvest plan from the
system, but will leave the datasets in place. This is good if you are using
harvest to migrate content and you do not plan on using the harvest feature
to update the datasets.

If you want to remove the harvest plan AND the datasets associated with it,
use the --revert option.

**Arguments**
Copy link
Member

@janette janette Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define this a little more on line 121.

Deregister a harvest. This action removes the harvest plan from the system, but will leave the datasets in place. This is good if you are using harvest to migrate content and you do not plan on using the harvest feature to update the datasets.
If you want to remove the harvest plan AND the datasets associated with it, use the --revert option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also update the dkan:harvest:revert description

Revert a harvest, i.e. remove harvested entities and unpublish orhpaned keywords, themes, and distributions. The harvest plan will remain and can be run again to generate the datasets after any issues have been resolved.


- **harvestId** The harvest id
- **revert** Perform a revert before deregistering.

**Usage**

``drush dkan:harvest:deregister --revert myHarvestId``

~~~~~~

Expand Down Expand Up @@ -187,7 +198,9 @@ dkan:harvest:register
dkan:harvest:revert
--------------------

Revert a harvest, i.e. remove harvested entities and unpublish orhpaned keywords, themes, and distributions.
Revert a harvest. Removes harvested entities and unpublishes orhpaned
keywords, themes, and distributions. The harvest plan will remain and can
be run again to generate the datasets after any issues have been resolved.

**Arguments**

Expand Down
51 changes: 39 additions & 12 deletions modules/harvest/src/Commands/HarvestCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\harvest\HarvestUtility;
use Drupal\harvest\Load\Dataset;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
use Harvest\ETL\Extract\DataJson;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand Down Expand Up @@ -135,24 +134,52 @@ protected function buildPlanFromOpts(mixed $opts) {
}

/**
* Deregister a harvest.
* Deregister a harvest plan, optionally reverting it.
*
* @param string $plan_id
* Harvest plan identifier.
* The harvest plan ID to deregister.
* @param array $options
* Options.
*
* @command dkan:harvest:deregister
* @option revert Revert the harvest plan before deregistering it.
* @usage dkan:harvest:deregister --revert PLAN_ID
* Deregister the PLAN_ID plan, after reverting all the data resources
* associated with it.
*/
public function deregister($plan_id) {
$this->logger()->warning('If you deregister a harvest with published datasets, you will not be able to bulk revert the datasets connected to this harvest.');
if ($this->io()->confirm('Deregister harvest ' . $plan_id)) {
if ($this->harvestService->deregisterHarvest($plan_id)) {
$this->logger()->notice('Successfully deregistered the ' . $plan_id . ' harvest.');
return DrushCommands::EXIT_SUCCESS;
}
public function deregister($plan_id, array $options = ['revert' => FALSE]) {
// Short circuit if the plan doesn't exist.
try {
$this->validateHarvestPlan($plan_id);
}
else {
throw new UserAbortException();
catch (\InvalidArgumentException $exception) {
$this->logger()->error($exception->getMessage());
return DrushCommands::EXIT_FAILURE;
}

// Are You Sure?
$message = 'Are you sure you want to deregister ' . $plan_id;
if ($options['revert'] ?? FALSE) {
$message = 'Are you sure you want to revert and deregister ' . $plan_id;
}
if (!$this->io()->confirm($message)) {
return DrushCommands::EXIT_FAILURE;
}

// Try to revert if the user wants to.
if (
($options['revert'] ?? FALSE) &&
($this->revert($plan_id) === DrushCommands::EXIT_FAILURE)
) {
return DrushCommands::EXIT_FAILURE;
}

// Do the deregister.
if ($this->harvestService->deregisterHarvest($plan_id)) {
$this->logger()->notice('Successfully deregistered the ' . $plan_id . ' harvest.');
return DrushCommands::EXIT_SUCCESS;
}

$this->logger()->error('Could not deregister the ' . $plan_id . ' harvest.');
return DrushCommands::EXIT_FAILURE;
}
Expand Down