-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from City-of-Helsinki/UHF-7324
UHF-7324: Support deleting old revisions
- Loading branch information
Showing
14 changed files
with
907 additions
and
1 deletion.
There are no files selected for viewing
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
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
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,36 @@ | ||
# Automatic entity revision deletion | ||
|
||
Allows old revisions to be deleted. By default, five revisions are kept per entity translation. | ||
|
||
## Configuration | ||
|
||
This can be configured in code with: | ||
```php | ||
|
||
# public/sites/default/*.settings.php | ||
$config['helfi_api_base.delete_revisions']['entity_types'] = [ | ||
'node', | ||
'paragraph', | ||
'tpr_unit', | ||
'tpr_service', | ||
'tpr_errand_service', | ||
]; | ||
``` | ||
|
||
or by creating a configuration yml file: | ||
|
||
```yaml | ||
# conf/cmi/helfi_api_base.delete_revisions.yml | ||
entity_types: | ||
- node | ||
- paragraph | ||
- tpr_unit | ||
- tpr_service | ||
- tpr_errand_service | ||
``` | ||
## Usage | ||
All configured entities are queued for clean up on entity save. | ||
To clean up existing entities at once, run: `drush helfi:revision:delete {entity_type}`. |
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
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
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
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
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_api_base\Commands; | ||
|
||
use Drupal\Component\Render\FormattableMarkup; | ||
use Drupal\Core\Database\Connection; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\helfi_api_base\Entity\Revision\RevisionManager; | ||
use Drush\Attributes\Command; | ||
use Drush\Attributes\Option; | ||
use Drush\Commands\DrushCommands; | ||
|
||
/** | ||
* A drush command file to manage revisions. | ||
*/ | ||
final class RevisionCommands extends DrushCommands { | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @param \Drupal\helfi_api_base\Entity\Revision\RevisionManager $revisionManager | ||
* The revision manager service. | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
* The entity type manager service. | ||
* @param \Drupal\Core\Database\Connection $connection | ||
* The connection service. | ||
*/ | ||
public function __construct( | ||
private readonly RevisionManager $revisionManager, | ||
private readonly EntityTypeManagerInterface $entityTypeManager, | ||
private readonly Connection $connection, | ||
) { | ||
} | ||
|
||
/** | ||
* Deletes the old revisions. | ||
* | ||
* @param string $entityType | ||
* The entity type. | ||
* @param array $options | ||
* The options. | ||
* | ||
* @return int | ||
* The exit code. | ||
*/ | ||
#[Command(name: 'helfi:revision:delete')] | ||
#[Option(name: 'keep', description: 'Number of revisions to keep')] | ||
public function delete(string $entityType, array $options = ['keep' => RevisionManager::KEEP_REVISIONS]) : int { | ||
if (!$this->revisionManager->entityTypeIsSupported($entityType)) { | ||
$this->io()->writeln('Given entity type is not supported.'); | ||
return DrushCommands::EXIT_SUCCESS; | ||
} | ||
|
||
$definition = $this->entityTypeManager->getDefinition($entityType); | ||
$entityIds = $this->connection->select($definition->getBaseTable(), 't') | ||
->fields('t', [$definition->getKey('id')]) | ||
->execute() | ||
->fetchCol(); | ||
|
||
$totalEntities = $remainingEntities = count($entityIds); | ||
$this->io()->writeln((string) new FormattableMarkup('Found @count @type entities', [ | ||
'@count' => $totalEntities, | ||
'@type' => $entityType, | ||
])); | ||
|
||
foreach ($entityIds as $id) { | ||
$revisions = $this->revisionManager->getRevisions($entityType, $id, $options['keep']); | ||
$revisionCount = count($revisions); | ||
|
||
$message = sprintf('Entity has less than %s revisions. Skipping', $options['keep']); | ||
|
||
if ($revisionCount > 0) { | ||
$message = (string) new FormattableMarkup('Deleting @count revisions', ['@count' => $revisionCount]); | ||
} | ||
$this->io()->writeln((string) new FormattableMarkup('[@current/@entities] @message ...', [ | ||
'@current' => $remainingEntities--, | ||
'@entities' => $totalEntities, | ||
'@message' => $message, | ||
])); | ||
$this->revisionManager->deleteRevisions($entityType, $revisions); | ||
} | ||
|
||
return DrushCommands::EXIT_SUCCESS; | ||
} | ||
|
||
} |
Oops, something went wrong.