2014-07-07 11:37:35 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2026-02-09 04:53:58 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2014-07-07 11:37:35 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-07-07 11:37:35 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Repair;
|
|
|
|
|
|
2016-10-18 05:10:55 -04:00
|
|
|
use Doctrine\DBAL\Exception\DriverException;
|
|
|
|
|
use OCP\IConfig;
|
|
|
|
|
use OCP\IDBConnection;
|
2016-04-22 09:35:39 -04:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
|
use OCP\Migration\IRepairStep;
|
2022-03-17 11:42:53 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-07-07 11:37:35 -04:00
|
|
|
|
2016-04-22 09:35:39 -04:00
|
|
|
class Collation implements IRepairStep {
|
2022-03-17 11:42:53 -04:00
|
|
|
public function __construct(
|
2025-11-17 09:32:54 -05:00
|
|
|
protected IConfig $config,
|
|
|
|
|
protected LoggerInterface $logger,
|
|
|
|
|
protected IDBConnection $connection,
|
2025-11-24 03:17:34 -05:00
|
|
|
protected bool $ignoreFailures,
|
2022-03-17 11:42:53 -04:00
|
|
|
) {
|
2014-07-07 11:37:35 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 03:17:34 -05:00
|
|
|
public function getName(): string {
|
2014-07-07 11:37:35 -04:00
|
|
|
return 'Repair MySQL collation';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fix mime types
|
|
|
|
|
*/
|
2025-11-24 03:17:34 -05:00
|
|
|
public function run(IOutput $output): void {
|
2024-07-01 10:59:47 -04:00
|
|
|
if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) {
|
2017-03-19 16:52:54 -04:00
|
|
|
$output->info('Not a mysql database -> nothing to do');
|
2014-07-07 11:37:35 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 06:50:08 -04:00
|
|
|
$characterSet = $this->config->getSystemValueBool('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
|
2015-07-30 07:57:04 -04:00
|
|
|
|
2014-07-07 11:37:35 -04:00
|
|
|
$tables = $this->getAllNonUTF8BinTables($this->connection);
|
|
|
|
|
foreach ($tables as $table) {
|
2017-01-12 10:54:29 -05: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
|
2022-03-17 11:42:53 -04:00
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]);
|
2017-01-12 10:54:29 -05:00
|
|
|
if (!$this->ignoreFailures) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 09:35:39 -04:00
|
|
|
$output->info("Change collation for $table ...");
|
2015-07-30 07:57:04 -04:00
|
|
|
$query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;');
|
2016-10-18 05:10:55 -04:00
|
|
|
try {
|
|
|
|
|
$query->execute();
|
|
|
|
|
} catch (DriverException $e) {
|
|
|
|
|
// Just log this
|
2022-03-17 11:42:53 -04:00
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e]);
|
2016-10-18 05:10:55 -04:00
|
|
|
if (!$this->ignoreFailures) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-07 11:37:35 -04:00
|
|
|
}
|
2015-07-30 04:57:16 -04:00
|
|
|
if (empty($tables)) {
|
|
|
|
|
$output->info('All tables already have the correct collation -> nothing to do');
|
|
|
|
|
}
|
2014-07-07 11:37:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
2025-11-24 03:17:34 -05:00
|
|
|
protected function getAllNonUTF8BinTables(IDBConnection $connection): array {
|
2023-04-05 06:50:08 -04:00
|
|
|
$dbName = $this->config->getSystemValueString('dbname');
|
|
|
|
|
$characterSet = $this->config->getSystemValueBool('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
|
2015-07-30 04:57:16 -04:00
|
|
|
|
|
|
|
|
// fetch tables by columns
|
|
|
|
|
$statement = $connection->executeQuery(
|
2014-07-07 11:37:35 -04:00
|
|
|
'SELECT DISTINCT(TABLE_NAME) AS `table`'
|
|
|
|
|
. ' FROM INFORMATION_SCHEMA . COLUMNS'
|
|
|
|
|
. ' WHERE TABLE_SCHEMA = ?'
|
2015-07-30 07:57:04 -04:00
|
|
|
. " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')"
|
2018-02-16 03:27:16 -05:00
|
|
|
. " AND TABLE_NAME LIKE '*PREFIX*%'",
|
2020-03-26 04:30:18 -04:00
|
|
|
[$dbName]
|
2014-07-07 11:37:35 -04:00
|
|
|
);
|
2015-07-30 04:57:16 -04:00
|
|
|
$rows = $statement->fetchAll();
|
|
|
|
|
$result = [];
|
2014-07-07 11:37:35 -04:00
|
|
|
foreach ($rows as $row) {
|
2015-07-30 04:57:16 -04:00
|
|
|
$result[$row['table']] = true;
|
2014-07-07 11:37:35 -04:00
|
|
|
}
|
2015-07-30 04:57:16 -04: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 03:27:16 -05:00
|
|
|
. " AND TABLE_NAME LIKE '*PREFIX*%'",
|
2015-07-30 04:57:16 -04:00
|
|
|
[$dbName]
|
|
|
|
|
);
|
|
|
|
|
$rows = $statement->fetchAll();
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$result[$row['table']] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_keys($result);
|
2014-07-07 11:37:35 -04:00
|
|
|
}
|
|
|
|
|
}
|