You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Collation.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin Müller <coder-hugo@users.noreply.github.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Repair;
  27. use Doctrine\DBAL\Exception\DriverException;
  28. use Doctrine\DBAL\Platforms\MySqlPlatform;
  29. use OCP\IConfig;
  30. use OCP\IDBConnection;
  31. use OCP\ILogger;
  32. use OCP\Migration\IOutput;
  33. use OCP\Migration\IRepairStep;
  34. class Collation implements IRepairStep {
  35. /** @var IConfig */
  36. protected $config;
  37. /** @var ILogger */
  38. protected $logger;
  39. /** @var IDBConnection */
  40. protected $connection;
  41. /** @var bool */
  42. protected $ignoreFailures;
  43. /**
  44. * @param IConfig $config
  45. * @param ILogger $logger
  46. * @param IDBConnection $connection
  47. * @param bool $ignoreFailures
  48. */
  49. public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) {
  50. $this->connection = $connection;
  51. $this->config = $config;
  52. $this->logger = $logger;
  53. $this->ignoreFailures = $ignoreFailures;
  54. }
  55. public function getName() {
  56. return 'Repair MySQL collation';
  57. }
  58. /**
  59. * Fix mime types
  60. */
  61. public function run(IOutput $output) {
  62. if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
  63. $output->info('Not a mysql database -> nothing to do');
  64. return;
  65. }
  66. $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  67. $tables = $this->getAllNonUTF8BinTables($this->connection);
  68. foreach ($tables as $table) {
  69. $output->info("Change row format for $table ...");
  70. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;');
  71. try {
  72. $query->execute();
  73. } catch (DriverException $e) {
  74. // Just log this
  75. $this->logger->logException($e);
  76. if (!$this->ignoreFailures) {
  77. throw $e;
  78. }
  79. }
  80. $output->info("Change collation for $table ...");
  81. if ($characterSet === 'utf8mb4') {
  82. // need to set row compression first
  83. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT=COMPRESSED;');
  84. $query->execute();
  85. }
  86. $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;');
  87. try {
  88. $query->execute();
  89. } catch (DriverException $e) {
  90. // Just log this
  91. $this->logger->logException($e);
  92. if (!$this->ignoreFailures) {
  93. throw $e;
  94. }
  95. }
  96. }
  97. if (empty($tables)) {
  98. $output->info('All tables already have the correct collation -> nothing to do');
  99. }
  100. }
  101. /**
  102. * @param IDBConnection $connection
  103. * @return string[]
  104. */
  105. protected function getAllNonUTF8BinTables(IDBConnection $connection) {
  106. $dbName = $this->config->getSystemValue("dbname");
  107. $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8';
  108. // fetch tables by columns
  109. $statement = $connection->executeQuery(
  110. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  111. " FROM INFORMATION_SCHEMA . COLUMNS" .
  112. " WHERE TABLE_SCHEMA = ?" .
  113. " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" .
  114. " AND TABLE_NAME LIKE '*PREFIX*%'",
  115. [$dbName]
  116. );
  117. $rows = $statement->fetchAll();
  118. $result = [];
  119. foreach ($rows as $row) {
  120. $result[$row['table']] = true;
  121. }
  122. // fetch tables by collation
  123. $statement = $connection->executeQuery(
  124. "SELECT DISTINCT(TABLE_NAME) AS `table`" .
  125. " FROM INFORMATION_SCHEMA . TABLES" .
  126. " WHERE TABLE_SCHEMA = ?" .
  127. " AND TABLE_COLLATION <> '" . $characterSet . "_bin'" .
  128. " AND TABLE_NAME LIKE '*PREFIX*%'",
  129. [$dbName]
  130. );
  131. $rows = $statement->fetchAll();
  132. foreach ($rows as $row) {
  133. $result[$row['table']] = true;
  134. }
  135. return array_keys($result);
  136. }
  137. }