Collation.php 4.3 KB

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