Skip to content

Instantly share code, notes, and snippets.

@brankoajzele
Created December 9, 2011 19:55
Show Gist options
  • Save brankoajzele/1453027 to your computer and use it in GitHub Desktop.
Save brankoajzele/1453027 to your computer and use it in GitHub Desktop.
Truncate all sales related tables in Magento in a "Multi Database Support" way
<?php
/**
* @author Branko Ajzele <ajzele@gmail.com>
*/
if (version_compare(phpversion(), '5.2.0', '<')===true) {
echo '<div style="font:12px/1.35em arial, helvetica, sans-serif;"><div style="margin:0 0 25px 0; border-bottom:1px solid #ccc;"><h3 style="margin:0; font-size:1.7em; font-weight:normal; text-transform:none; text-align:left; color:#2f2f2f;">Whoops, it looks like you have an invalid PHP version.</h3></div><p>Magento supports PHP 5.2.0 or newer. <a href="http://www.magentocommerce.com/install" target="">Find out</a> how to install</a> Magento using PHP-CGI as a work-around.</p></div>';
exit;
}
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
echo $mageFilename." was not found";
exit;
}
require_once $mageFilename;
Mage::app();
$salesEntitiesConf = array_merge(
Mage::getSingleton('core/config')->init()->getXpath('global/models/sales_entity/entities//table'),
Mage::getSingleton('core/config')->init()->getXpath('global/models/sales_resource/entities//table')
);
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_write');
/*
Multiple RDBMS Support in Magento CE 1.6+ / EE 1.11+
http://www.magentocommerce.com/images/uploads/RDBMS_Guide2.pdf
2.2. Adapters:
... The new Varien_DB_Adapter_Interface was added to sign a contract that all
developed adapters must execute in order to get Magento working on an actual
database. The interface describes the list of methods and constants that can be used by resource models...
Used below in the loop:
* Varien_Db_Adapter_Interface::getTableName()
* Varien_Db_Adapter_Interface::isTableExists()
* Varien_Db_Adapter_Interface::truncateTable()
*/
while ($table = current($salesEntitiesConf) ){
$table = $resource->getTableName($table);
if ($connection->isTableExists($table)) {
try {
$connection->truncateTable($table);
printf('Successfully truncated the <i style="color:green;">%s</i> table.<br />', $table);
} catch(Exception $e) {
printf('Error <i style="color:red;">%s</i> occurred truncating the <i style="color:red;">%s</i> table.<br />', $e->getMessage(), $table);
}
}
next($salesEntitiesConf);
}
exit('All done...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment