Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
parisek committed Feb 19, 2019
1 parent 03a43fe commit 049da51
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# commerce_order_revision_clear
Commerce order revision clear
===

This is lightweight module that solves issue that database is getting really huge because we are storing all revisions for commerce orders. In our commerce site we have orders with hundreds of revisions.

How it works?
===
We are using [https://www.drupal.org/project/commerce/issues/2442049](https://www.drupal.org/project/commerce/issues/2442049) code from provided batch to leverage Entity API module. With every CRON run we delete 50 oldest revisions which are older than 30 days. This module has no configuration, but could be easily customized in code for your special needs.

**Do not test in production and backup your database**

Author
===
Petr Parimucha

[https://pari.cz](https://pari.cz/en)
6 changes: 6 additions & 0 deletions commerce_order_revision_clear.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = Commerce order revision clear
description = Custom module for automatically clearing old order revisions via CRON.
package = Commerce
core = 7.x

dependencies[] = commerce_order
79 changes: 79 additions & 0 deletions commerce_order_revision_clear.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* Implements hook_cron().
*/
function commerce_order_revision_clear_cron() {

$day = 30;
$order_limit = 50;

$query = db_select('commerce_order_revision', 'o')
->condition('o.revision_timestamp', REQUEST_TIME-86400*$day, '<')
->fields('o',array('order_id'))
->groupBy('o.order_id')
->orderBy('oidcount', 'DESC');

$query->addExpression('COUNT(o.order_id)', 'oidcount');
$query->havingCondition('oidcount', 1, '>');

$oids = $query->execute()->fetchCol('order_id');

$oids_sliced = array_slice($oids, 0, $order_limit);

foreach ($oids_sliced as $order_id) {
$revisions = db_select('commerce_order_revision', 'r')
->condition('r.order_id', $order_id)
->fields('r',array('revision_id'))
->execute()
->fetchCol();
if(count($revisions) > 1) {
foreach ($revisions as $revision_id) {
entity_revision_delete('commerce_order', $revision_id);
}
}
}

watchdog('commerce_order_revision_clear', t('Removed revisions from @limit/@total orders in queue.', ['@limit' => $order_limit, '@total' => count($oids)]));

}

/**
* Implements hook_entity_info_alter().
*/
function commerce_order_revision_clear_entity_info_alter(&$entity_info) {
if(!isset($entity_info['commerce_order']['revision deletion callback'])) {
$entity_info['commerce_order']['revision deletion callback'] = '_commerce_order_revision_clear_revision_delete';
}
}

/**
* Deletes a commerce_order revision.
*
* @param $revision_id
* The revision ID to delete.
*/
function _commerce_order_revision_clear_revision_delete($revision_id) {

if ($revisions = commerce_order_load_multiple(array(), array('revision_id' => $revision_id))) {

$revision = $revisions ? reset($revisions) : FALSE;
// Prevent deleting the current revision.
if (!$revision || commerce_order_is_latest_revision($revision)) {
return FALSE;
}

// Delete order revision.
db_delete('commerce_order_revision')
->condition('order_id', $revision->order_id)
->condition('revision_id', $revision->revision_id)
->execute();

module_invoke_all('commerce_order_revision_delete', $revision);
field_attach_delete_revision('commerce_order', $revision);

return TRUE;
}

return FALSE;
}

0 comments on commit 049da51

Please sign in to comment.