-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drush job to clear out old revisions (#2283)
* Add drush job to clear out old revisions * phpcs fixes * Remove extra log message * Only remove revisisonsf rom 1884
- Loading branch information
Neil Hastings
authored
Jul 15, 2020
1 parent
863ebdb
commit 8791596
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
docroot/modules/custom/va_gov_migrate/va_gov_migrate.drush.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Drush jobs to clean up old revisions. | ||
*/ | ||
|
||
use Drupal\Core\Entity\EntityStorageException; | ||
use Drush\Log\LogLevel; | ||
|
||
/** | ||
* Implements hook_drush_command(). | ||
*/ | ||
function va_gov_migrate_drush_command() { | ||
$items = []; | ||
|
||
$items['va-gov-clean-revs'] = [ | ||
'description' => 'Clean up bad node revisions', | ||
'aliases' => ['vg-cr'], | ||
]; | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Command for cleaning up revisions. | ||
* | ||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | ||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | ||
*/ | ||
function drush_va_gov_migrate_va_gov_clean_revs() { | ||
$time = 1572551719; | ||
|
||
$query = \Drupal::database()->select('node_revision', 'nr'); | ||
$query->condition('revision_timestamp', $time); | ||
$query->fields('nr', ['vid']); | ||
$vids = $query->execute()->fetchCol(); | ||
|
||
$query = \Drupal::database()->select('node_revision', 'nr'); | ||
$query->condition('revision_log', 'Update of status by migration.'); | ||
$query->condition('nid', 1884); | ||
$query->fields('nr', ['vid']); | ||
$more_vids = $query->execute()->fetchCol(); | ||
|
||
$vids = array_merge($vids, $more_vids); | ||
|
||
drush_log('Attempting to Delete ' . count($vids) . ' node revisions', LogLevel::INFO); | ||
$missed_vids = []; | ||
foreach ($vids as $vid) { | ||
try { | ||
drush_log('Deleting: ' . $vid, LogLevel::INFO); | ||
\Drupal::entityTypeManager()->getStorage('node')->deleteRevision($vid); | ||
} | ||
catch (EntityStorageException $e) { | ||
drush_log('Vid ' . $vid . ' could not be deleted: ' . $e->getMessage(), LogLevel::WARNING); | ||
$missed_vids[] = $vid; | ||
} | ||
} | ||
|
||
$count = count($vids) - count($missed_vids); | ||
drush_log('Deleted ' . $count . ' revision', LogLevel::OK); | ||
drush_log('The following revisions were not deleted: ' . implode(', ', $missed_vids), LogLevel::WARNING); | ||
} |
8791596
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ Test Failed:
va/tests/accessibility