Skip to content

Instantly share code, notes, and snippets.

@vovsky
Created April 28, 2014 14:25
Show Gist options
  • Save vovsky/11373704 to your computer and use it in GitHub Desktop.
Save vovsky/11373704 to your computer and use it in GitHub Desktop.
changing collation script
<?php
require_once 'abstract.php';
class Atwix_Collation_Shell extends Mage_Shell_Abstract
{
/**
* Validate arguments
*
*/
protected function _validate()
{
return true;
}
public function run()
{
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$showCollationQuery = 'show variables like "%collation_database%"';
$res = $write->fetchRow($showCollationQuery);
$dbConfig = $write->getConfig();
if ($res['Value'] != 'utf8_general_ci') {
$generalQuery = 'ALTER DATABASE ' . $dbConfig['dbname'] . ' CHARACTER SET utf8 COLLATE utf8_general_ci;';
$write->query($generalQuery);
}
$query = 'SELECT T.table_name, T.table_collation, CCSA.character_set_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "' . $dbConfig['dbname'] . '"
AND T.table_collation != "utf8_general_ci"
AND T.table_name not like "%tmp%"
AND T.table_name not like "%_bak%";';
$tablesInfo = $write->fetchAll($query);
echo "Total tables to process: " . count($tablesInfo) . PHP_EOL;;
foreach ($tablesInfo as $tableInfo) {
$query = 'ALTER TABLE ' . $tableInfo['table_name'] . ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;';
echo 'processing table ' . $tableInfo['table_name'] . PHP_EOL;
Mage::log('processing table ' . $tableInfo['table_name'], null, 'atwix_collation.log');
$write->query($query);
}
}
}
$shell = new Atwix_Collation_Shell();
$shell->run();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment