Configure Revisions Operation


  • Use ConfigureRevisionsOperation to apply the following revisions configuration to the database:

    • Default configuration - applies to all document collections.
    • Collection-specific configurations - override the default settings for these collections.
    • To apply a configuration for conflict document revisions see configure conflict revisions.
  • The configuration passed to this operation will REPLACE the current revisions configuration in the database.
    To MODIFY existing configuration, fetch the current configuration from the database record first.

  • After applying the configuration,
    revisions are created and purged for a document whenever the document is created, modified, or deleted.

  • To create a revision when there is no configuration defined (or enabled) see: force revision creation

  • By default, the operation will be applied to the default database.
    To operate on a different database see switch operations to different database.

  • In this page:


Replace configuration

In this example, we create a new RevisionsConfiguration for the database.
If revisions configuration already exists in the database - it will be replaced.

// ==============================================================================
// Define default settings that will apply to ALL collections
// Note: this is optional
$defaultRevConfig = new RevisionsCollectionConfiguration();

// With this configuration:
// ------------------------
// * A revision will be created anytime a document is modified or deleted.
// * Revisions of a deleted document can be accessed in the Revisions Bin view.
// * At least 100 of the latest revisions will be kept.
// * Older revisions will be removed if they exceed 7 days on next revision creation.
// * A maximum of 15 revisions will be deleted each time a document is updated,
//   until the defined '# of revisions to keep' limit is reached.
$defaultRevConfig->setMinimumRevisionsToKeep(100);
$defaultRevConfig->setMinimumRevisionAgeToKeep(Duration::ofDays(7));
$defaultRevConfig->setMaximumRevisionsToDeleteUponDocumentUpdate(15);
$defaultRevConfig->setPurgeOnDelete(false);
$defaultRevConfig->setDisabled(false);


// ==============================================================================
// Define a specific configuration for the EMPLOYEES collection
// This will override the default settings
$employeesRevConfig = new RevisionsCollectionConfiguration();

// With this configuration:
// ------------------------
// * A revision will be created anytime an Employee document is modified.
// * When a document is deleted all its revisions will be removed.
// * At least 50 of the latest revisions will be kept.
// * Older revisions will be removed if they exceed 12 hours on next revision creation.

$employeesRevConfig->setMinimumRevisionsToKeep(50);
$employeesRevConfig->setMinimumRevisionAgeToKeep(Duration::ofHours(12));
$employeesRevConfig->setPurgeOnDelete(true);
$employeesRevConfig->setDisabled(false);

// ==============================================================================
// Define a specific configuration for the PRODUCTS collection
// This will override the default settings
$productsRevConfig = new RevisionsCollectionConfiguration();

// No revisions will be created for the Products collection,
// even though default configuration is enabled
$productsRevConfig->setDisabled(true);

// ==============================================================================
// Combine all configurations in the RevisionsConfiguration object
$revisionsConfig = new RevisionsConfiguration();
$revisionsConfig->setDefaultConfig($defaultRevConfig);
$revisionsConfig->setCollections([
    "Employees" => $employeesRevConfig,
    "Products" => $productsRevConfig
]);

// ==============================================================================
// Define the configure revisions operation, pass the configuration
$configureRevisionsOp = new ConfigureRevisionsOperation($revisionsConfig);

// Execute the operation by passing it to Maintenance.Send
// Any existing configuration will be replaced with the new configuration passed
$documentStore->maintenance()->send($configureRevisionsOp);

Modify configuration

In this example, we fetch the existing revisions configuration from the database record and modify it.

// ==============================================================================
// Define the get database record operation:
$getDatabaseRecordOp = new GetDatabaseRecordOperation($documentStore->getDatabase());
// Get the current revisions configuration from the database record:
/** @var RevisionsConfiguration $revisionsConfig */
$revisionsConfig = $documentStore->maintenance()->server()->send($getDatabaseRecordOp)->getRevisions();

// ==============================================================================
// If no revisions configuration exists, then create a new configuration
if ($revisionsConfig == null)
{
    $revisionsConfig = new RevisionsConfiguration();
    $revisionsConfig->setDefaultConfig($defaultRevConfig);
    $revisionsConfig->setCollections([
        "Employees" => $employeesRevConfig,
        "Products" => $productsRevConfig
    ]);
}

// ==============================================================================
// If a revisions configuration already exists, then modify it
else
{
    $revisionsConfig->setDefaultConfig($defaultRevConfig);
    $collections = $revisionsConfig->getCollections();
    $collections["Employees"] = $employeesRevConfig;
    $collections["Products"] = $productsRevConfig;
    $revisionsConfig->setCollections($collections);
}

// ==============================================================================
// Define the configure revisions operation, pass the configuration
$configureRevisionsOp = new ConfigureRevisionsOperation($revisionsConfig);

// Execute the operation by passing it to Maintenance.Send
// The existing configuration will be updated
$documentStore->maintenance()->send($configureRevisionsOp);

Syntax

new ConfigureRevisionsOperation(?RevisionsConfiguration $configuration);
Parameter Type Description
configuration ?RevisionsConfiguration The revisions configuration to apply

class RevisionsConfiguration
{
    public function getDefaultConfig(): ?RevisionsCollectionConfiguration;
    public function setDefaultConfig(?RevisionsCollectionConfiguration $defaultConfig): void;
    public function getCollections(): ?RevisionsCollectionConfigurationArray;
    public function setCollections(null|RevisionsCollectionConfigurationArray|array $collections): void;
}
Property Type Description
defaultConfig RevisionsCollectionConfiguration Optional default settings that apply to any collection Not specified in collections.
collections null or RevisionsCollectionConfigurationArray or array An array of collection-specific configurations
The keys are the collection names
The values are the corresponding configurations.
Overrides the default settings for the collections defined.

class RevisionsCollectionConfiguration
{
    private ?int $minimumRevisionsToKeep = null;
    private ?Duration $minimumRevisionAgeToKeep = null;
    private bool $disabled = false;
    private bool $purgeOnDelete = false;
    private ?int $maximumRevisionsToDeleteUponDocumentUpdate = null;

    // ... getters and setters ...
}

Property Type Description
minimumRevisionsToKeep ?int
  • This number of revisions will be kept per document.
  • Older revisions exceeding this number will be purged upon the next document modification.
  • Default : None = no limit
minimumRevisionAgeToKeep ?Duration
  • Limit the number of revisions kept per document by their age.
  • Revisions that are older than this time will be purged upon the next document modification.
  • Default : None = no age limit
    disabled bool
    • fFalse ( Default ) - Revisions will be created and purged according to the configuration.
    • True - No revisions will be created or purged.
    purgeOnDelete bool
    • False ( Default ) - Revisions of a deleted document are moved to the Revisions Bin.
    • True - When a document is deleted all its revisions are also deleted.
    maximumRevisionsToDeleteUponDocumentUpdate ?int
    • The maximum number of revisions to delete upon each document modification.
    • Default : None = no limit,
      all revisions that pend purging will be deleted.