2014-07-07 19:37:35 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
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>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Müller <coder-hugo@users.noreply.github.com>
|
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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
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;
|
2021-01-03 17:28:31 +03: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) {
|
2021-01-03 17:28:31 +03: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 . "')" .
|
2018-02-16 11:27:16 +03:00
|
|
|
" AND TABLE_NAME LIKE '*PREFIX*%'",
|
2020-03-26 11:30:18 +03:00
|
|
|
[$dbName]
|
2014-07-07 19:37:35 +04:00
|
|
|
);
|
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'" .
|
2018-02-16 11:27:16 +03:00
|
|
|
" AND TABLE_NAME LIKE '*PREFIX*%'",
|
2015-07-30 11:57:16 +03:00
|
|
|
[$dbName]
|
|
|
|
);
|
|
|
|
$rows = $statement->fetchAll();
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$result[$row['table']] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_keys($result);
|
2014-07-07 19:37:35 +04:00
|
|
|
}
|
|
|
|
}
|