Collation.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Repair;
  8. use Doctrine\DBAL\Exception\DriverException;
  9. use Doctrine\DBAL\Platforms\MySQLPlatform;
  10. use OCP\IConfig;
  11. use OCP\IDBConnection;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\IRepairStep;
  14. use Psr\Log\LoggerInterface;
  15. class Collation implements IRepairStep {
  16. /** @var IConfig */
  17. protected $config;
  18. protected LoggerInterface $logger;
  19. /** @var IDBConnection */
  20. protected $connection;
  21. /** @var bool */
  22. protected $ignoreFailures;
  23. /**
  24. * @param bool $ignoreFailures
  25. */
  26. public function __construct(
  27. IConfig $config,
  28. LoggerInterface $logger,
  29. IDBConnection $connection,
  30. $ignoreFailures
  31. ) {
  32. $this->connection = $connection;
  33. $this->config = $config;
  34. $this->logger = $logger;
  35. $this->ignoreFailures = $ignoreFailures;
  36. }
  37. public function getName() {
  38. return 'Repair MySQL collation';
  39. }
  40. /**
  41. * Fix mime types
  42. */
  43. public function run(IOutput $output) {
  44. if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
  45. $output->info('Not a mysql database -> nothing to do');
  46. return;
  47. }
  48. $characterSet = $this->config->getSystemValueBool('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  49. $tables = $this->getAllNonUTF8BinTables($this->connection);
  50. foreach ($tables as $table) {
  51. $output->info("Change row format for $table ...");
  52. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;');
  53. try {
  54. $query->execute();
  55. } catch (DriverException $e) {
  56. // Just log this
  57. $this->logger->error($e->getMessage(), ['exception' => $e]);
  58. if (!$this->ignoreFailures) {
  59. throw $e;
  60. }
  61. }
  62. $output->info("Change collation for $table ...");
  63. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;');
  64. try {
  65. $query->execute();
  66. } catch (DriverException $e) {
  67. // Just log this
  68. $this->logger->error($e->getMessage(), ['exception' => $e]);
  69. if (!$this->ignoreFailures) {
  70. throw $e;
  71. }
  72. }
  73. }
  74. if (empty($tables)) {
  75. $output->info('All tables already have the correct collation -> nothing to do');
  76. }
  77. }
  78. /**
  79. * @param IDBConnection $connection
  80. * @return string[]
  81. */
  82. protected function getAllNonUTF8BinTables(IDBConnection $connection) {
  83. $dbName = $this->config->getSystemValueString("dbname");
  84. $characterSet = $this->config->getSystemValueBool('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  85. // fetch tables by columns
  86. $statement = $connection->executeQuery(
  87. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  88. " FROM INFORMATION_SCHEMA . COLUMNS" .
  89. " WHERE TABLE_SCHEMA = ?" .
  90. " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" .
  91. " AND TABLE_NAME LIKE '*PREFIX*%'",
  92. [$dbName]
  93. );
  94. $rows = $statement->fetchAll();
  95. $result = [];
  96. foreach ($rows as $row) {
  97. $result[$row['table']] = true;
  98. }
  99. // fetch tables by collation
  100. $statement = $connection->executeQuery(
  101. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  102. " FROM INFORMATION_SCHEMA . TABLES" .
  103. " WHERE TABLE_SCHEMA = ?" .
  104. " AND TABLE_COLLATION <> '" . $characterSet . "_bin'" .
  105. " AND TABLE_NAME LIKE '*PREFIX*%'",
  106. [$dbName]
  107. );
  108. $rows = $statement->fetchAll();
  109. foreach ($rows as $row) {
  110. $result[$row['table']] = true;
  111. }
  112. return array_keys($result);
  113. }
  114. }