2014-07-07 19:37:35 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-07-07 19:37:35 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-07-07 19:37:35 +04:00
|
|
|
namespace OC\Repair;
|
|
|
|
|
2016-10-18 12:10:55 +03:00
|
|
|
use Doctrine\DBAL\Exception\DriverException;
|
2014-07-07 19:37:35 +04:00
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform;
|
2016-10-18 12:10:55 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\ILogger;
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
2014-07-07 19:37:35 +04:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
class Collation implements IRepairStep {
|
2016-10-18 12:10:55 +03:00
|
|
|
/** @var IConfig */
|
2014-07-07 19:37:35 +04:00
|
|
|
protected $config;
|
|
|
|
|
2016-10-18 12:10:55 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
2014-07-07 19:37:35 +04:00
|
|
|
protected $connection;
|
|
|
|
|
2016-10-18 12:10:55 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $ignoreFailures;
|
|
|
|
|
2014-07-07 19:37:35 +04:00
|
|
|
/**
|
2016-10-18 12:10:55 +03:00
|
|
|
* @param IConfig $config
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
* @param bool $ignoreFailures
|
2014-07-07 19:37:35 +04:00
|
|
|
*/
|
2016-10-18 12:10:55 +03:00
|
|
|
public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) {
|
2014-07-07 19:37:35 +04:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->config = $config;
|
2016-10-18 12:10:55 +03:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->ignoreFailures = $ignoreFailures;
|
2014-07-07 19:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Repair MySQL collation';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix mime types
|
|
|
|
*/
|
2016-04-22 16:35:39 +03:00
|
|
|
public function run(IOutput $output) {
|
2014-07-07 19:37:35 +04:00
|
|
|
if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
|
2017-03-19 23:52:54 +03:00
|
|
|
$output->info('Not a mysql database -> nothing to do');
|
2014-07-07 19:37:35 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-30 14:57:04 +03:00
|
|
|
$characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
|
|
|
|
|
2014-07-07 19:37:35 +04:00
|
|
|
$tables = $this->getAllNonUTF8BinTables($this->connection);
|
|
|
|
foreach ($tables as $table) {
|
2017-01-12 18:54:29 +03:00
|
|
|
$output->info("Change row format for $table ...");
|
|
|
|
$query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;');
|
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (DriverException $e) {
|
|
|
|
// Just log this
|
|
|
|
$this->logger->logException($e);
|
|
|
|
if (!$this->ignoreFailures) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$output->info("Change collation for $table ...");
|
2015-07-30 11:57:16 +03:00
|
|
|
if ($characterSet === 'utf8mb4') {
|
|
|
|
// need to set row compression first
|
|
|
|
$query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT=COMPRESSED;');
|
|
|
|
$query->execute();
|
|
|
|
}
|
2015-07-30 14:57:04 +03:00
|
|
|
$query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;');
|
2016-10-18 12:10:55 +03:00
|
|
|
try {
|
|
|
|
$query->execute();
|
|
|
|
} catch (DriverException $e) {
|
|
|
|
// Just log this
|
|
|
|
$this->logger->logException($e);
|
|
|
|
if (!$this->ignoreFailures) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2014-07-07 19:37:35 +04:00
|
|
|
}
|
2015-07-30 11:57:16 +03:00
|
|
|
if (empty($tables)) {
|
|
|
|
$output->info('All tables already have the correct collation -> nothing to do');
|
|
|
|
}
|
2014-07-07 19:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-30 11:57:16 +03:00
|
|
|
* @param IDBConnection $connection
|
2014-07-07 19:37:35 +04:00
|
|
|
* @return string[]
|
|
|
|
*/
|
2015-07-30 11:57:16 +03:00
|
|
|
protected function getAllNonUTF8BinTables(IDBConnection $connection) {
|
2014-07-07 19:37:35 +04:00
|
|
|
$dbName = $this->config->getSystemValue("dbname");
|
2015-07-30 14:57:04 +03:00
|
|
|
$characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
|
2015-07-30 11:57:16 +03:00
|
|
|
|
|
|
|
// fetch tables by columns
|
|
|
|
$statement = $connection->executeQuery(
|
2014-07-07 19:37:35 +04:00
|
|
|
"SELECT DISTINCT(TABLE_NAME) AS `table`" .
|
|
|
|
" FROM INFORMATION_SCHEMA . COLUMNS" .
|
|
|
|
" WHERE TABLE_SCHEMA = ?" .
|
2015-07-30 14:57:04 +03:00
|
|
|
" AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" .
|
2014-07-07 19:37:35 +04:00
|
|
|
" AND TABLE_NAME LIKE \"*PREFIX*%\"",
|
|
|
|
array($dbName)
|
|
|
|
);
|
2015-07-30 11:57:16 +03:00
|
|
|
$rows = $statement->fetchAll();
|
|
|
|
$result = [];
|
2014-07-07 19:37:35 +04:00
|
|
|
foreach ($rows as $row) {
|
2015-07-30 11:57:16 +03:00
|
|
|
$result[$row['table']] = true;
|
2014-07-07 19:37:35 +04:00
|
|
|
}
|
2015-07-30 11:57:16 +03:00
|
|
|
|
|
|
|
// fetch tables by collation
|
|
|
|
$statement = $connection->executeQuery(
|
|
|
|
"SELECT DISTINCT(TABLE_NAME) AS `table`" .
|
|
|
|
" FROM INFORMATION_SCHEMA . TABLES" .
|
|
|
|
" WHERE TABLE_SCHEMA = ?" .
|
|
|
|
" AND TABLE_COLLATION <> '" . $characterSet . "_bin'" .
|
|
|
|
" AND TABLE_NAME LIKE \"*PREFIX*%\"",
|
|
|
|
[$dbName]
|
|
|
|
);
|
|
|
|
$rows = $statement->fetchAll();
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$result[$row['table']] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_keys($result);
|
2014-07-07 19:37:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|