nswdpc/silverstripe-pruner

Prune records from configured DataObject classes

Installs: 1 024

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 1

Type:silverstripe-vendormodule

v1.0.0-rc1 2023-06-26 04:31 UTC

This package is auto-updated.

Last update: 2024-02-22 08:24:15 UTC


README

Removes records from the database attached to a Silverstripe project, using a queued job with configurable options.

It can be applied to any SilverStripe\ORM\DataObject record and is useful for records in tables that can continue to grow, like form submissions.

Install

composer require nswdpc/silverstripe-pruner

Userforms support

See nswdpc/silverstripe-pruner-userforms module for silverstripe/userforms support.

Requirements

See composer.json

Configuration

Configure a project .yml file like so:

---
Name: prunerconfiglocal
After:
  - '#nswdpc-pruner'
---
NSWDPC\Pruner\Pruner:
  # sample target_models
  target_models:
    # remove/backup submitted forms from the userforms module
    - 'SilverStripe\UserForms\Model\Submission\SubmittedForm'
    # a namespaced DataObject
    - 'Some\Namespaced\DataObject'

Interface

When adding a SilverStripe\ORM\DataObject to the list of target_models, these DataObjects must:

  • implement the interface NSWDPC\Utility\Pruner\Interfaces\PrunerInterface OR
  • have an extension that implements that Interface

The module will ignore models passed to it that do not implement this interface.

See the PrunerInterface for documentation.

Pruning records via the queued job

The PruneJob exists to remove the relevant records from the target models.

You can pass in the following constructor arguments:

  • $days_ago - the minimum number of days in the past records should be truncated. Older records are removed first. Default = 30
  • $limit - the maximum number of records to remove at one time. Default = 50 (in the case of SubmittedForm records this is a limit per parent class)
  • $targets - a comma separated model of DataObject classnames (namespace). If left empty the configured names will be used, if any. This allow you to schedule removal of a certain class of records at a certain time.
  • $report_only - 0|1 - pass in 1 to only report on what would be removed.

Returning a list of records to remove

While the most common list to return would be records older than the days_ago age parameter, you are in control of how records are filtered as returned from pruneList.

You can add extra filters to the query or modify/ignore the age parameter entirely. For instance, you could remove records older than a certain date and not authored by members in a specific domain.

Operations pre/post prune

In addition to the usual onBeforeDelete/onAfterDelete Silverstripe methods, the module calls onBeforePrune and onAfterPrune before/after record deletion, respectively.

The order of operation for each record removal is:

  1. onBeforePrune
  2. delete action:
    1. onBeforeDelete
    2. delete
    3. onAfterDelete
  3. onAfterPrune

You must implement these methods, even if you are not carrying out any actions pre/post record pruning.

Reporting task

A task exists to provide quick report showing what would be removed based on the arguments provided:

Report for the targeted models, older than 15 days, limit 50 records removed per model

./vendor/bin/sake dev/tasks/ReportOnlyPrunerTask age=15 limit=50 targets=SilverStripe\\UserForms\\Model\\Submission\\SubmittedForm

Multiple targets can be separated by a comma. If not targets are provided, the configured value of NSWDPC\Pruner\Pruner.target_models is used.

Maintainers

License

BSD-3-Clause

Security

If you have found a security issue with this module, please email digital[@]dpc.nsw.gov.au in the first instance, detailing your findings.

Bugtracker

We welcome bug reports, pull requests and feature requests on the Github Issue tracker for this project.

Please review the code of conduct prior to opening a new issue.

Development and contribution

If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers.

Please review the code of conduct prior to completing a pull request.