@@ -35,120 +35,120 @@ |
||
| 35 | 35 | use OCP\Migration\IRepairStep; |
| 36 | 36 | |
| 37 | 37 | class Collation implements IRepairStep { |
| 38 | - /** @var IConfig */ |
|
| 39 | - protected $config; |
|
| 40 | - |
|
| 41 | - /** @var ILogger */ |
|
| 42 | - protected $logger; |
|
| 43 | - |
|
| 44 | - /** @var IDBConnection */ |
|
| 45 | - protected $connection; |
|
| 46 | - |
|
| 47 | - /** @var bool */ |
|
| 48 | - protected $ignoreFailures; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param IConfig $config |
|
| 52 | - * @param ILogger $logger |
|
| 53 | - * @param IDBConnection $connection |
|
| 54 | - * @param bool $ignoreFailures |
|
| 55 | - */ |
|
| 56 | - public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) { |
|
| 57 | - $this->connection = $connection; |
|
| 58 | - $this->config = $config; |
|
| 59 | - $this->logger = $logger; |
|
| 60 | - $this->ignoreFailures = $ignoreFailures; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function getName() { |
|
| 64 | - return 'Repair MySQL collation'; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Fix mime types |
|
| 69 | - */ |
|
| 70 | - public function run(IOutput $output) { |
|
| 71 | - if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 72 | - $output->info('Not a mysql database -> nothing to do'); |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 77 | - |
|
| 78 | - $tables = $this->getAllNonUTF8BinTables($this->connection); |
|
| 79 | - foreach ($tables as $table) { |
|
| 80 | - $output->info("Change row format for $table ..."); |
|
| 81 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
| 82 | - try { |
|
| 83 | - $query->execute(); |
|
| 84 | - } catch (DriverException $e) { |
|
| 85 | - // Just log this |
|
| 86 | - $this->logger->logException($e); |
|
| 87 | - if (!$this->ignoreFailures) { |
|
| 88 | - throw $e; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $output->info("Change collation for $table ..."); |
|
| 93 | - if ($characterSet === 'utf8mb4') { |
|
| 94 | - // need to set row compression first |
|
| 95 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT=COMPRESSED;'); |
|
| 96 | - $query->execute(); |
|
| 97 | - } |
|
| 98 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
| 99 | - try { |
|
| 100 | - $query->execute(); |
|
| 101 | - } catch (DriverException $e) { |
|
| 102 | - // Just log this |
|
| 103 | - $this->logger->logException($e); |
|
| 104 | - if (!$this->ignoreFailures) { |
|
| 105 | - throw $e; |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - if (empty($tables)) { |
|
| 110 | - $output->info('All tables already have the correct collation -> nothing to do'); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param IDBConnection $connection |
|
| 116 | - * @return string[] |
|
| 117 | - */ |
|
| 118 | - protected function getAllNonUTF8BinTables(IDBConnection $connection) { |
|
| 119 | - $dbName = $this->config->getSystemValue("dbname"); |
|
| 120 | - $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 121 | - |
|
| 122 | - // fetch tables by columns |
|
| 123 | - $statement = $connection->executeQuery( |
|
| 124 | - "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 125 | - " FROM INFORMATION_SCHEMA . COLUMNS" . |
|
| 126 | - " WHERE TABLE_SCHEMA = ?" . |
|
| 127 | - " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
| 128 | - " AND TABLE_NAME LIKE '*PREFIX*%'", |
|
| 129 | - array($dbName) |
|
| 130 | - ); |
|
| 131 | - $rows = $statement->fetchAll(); |
|
| 132 | - $result = []; |
|
| 133 | - foreach ($rows as $row) { |
|
| 134 | - $result[$row['table']] = true; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - // fetch tables by collation |
|
| 138 | - $statement = $connection->executeQuery( |
|
| 139 | - "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 140 | - " FROM INFORMATION_SCHEMA . TABLES" . |
|
| 141 | - " WHERE TABLE_SCHEMA = ?" . |
|
| 142 | - " AND TABLE_COLLATION <> '" . $characterSet . "_bin'" . |
|
| 143 | - " AND TABLE_NAME LIKE '*PREFIX*%'", |
|
| 144 | - [$dbName] |
|
| 145 | - ); |
|
| 146 | - $rows = $statement->fetchAll(); |
|
| 147 | - foreach ($rows as $row) { |
|
| 148 | - $result[$row['table']] = true; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return array_keys($result); |
|
| 152 | - } |
|
| 38 | + /** @var IConfig */ |
|
| 39 | + protected $config; |
|
| 40 | + |
|
| 41 | + /** @var ILogger */ |
|
| 42 | + protected $logger; |
|
| 43 | + |
|
| 44 | + /** @var IDBConnection */ |
|
| 45 | + protected $connection; |
|
| 46 | + |
|
| 47 | + /** @var bool */ |
|
| 48 | + protected $ignoreFailures; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param IConfig $config |
|
| 52 | + * @param ILogger $logger |
|
| 53 | + * @param IDBConnection $connection |
|
| 54 | + * @param bool $ignoreFailures |
|
| 55 | + */ |
|
| 56 | + public function __construct(IConfig $config, ILogger $logger, IDBConnection $connection, $ignoreFailures) { |
|
| 57 | + $this->connection = $connection; |
|
| 58 | + $this->config = $config; |
|
| 59 | + $this->logger = $logger; |
|
| 60 | + $this->ignoreFailures = $ignoreFailures; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function getName() { |
|
| 64 | + return 'Repair MySQL collation'; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Fix mime types |
|
| 69 | + */ |
|
| 70 | + public function run(IOutput $output) { |
|
| 71 | + if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 72 | + $output->info('Not a mysql database -> nothing to do'); |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 77 | + |
|
| 78 | + $tables = $this->getAllNonUTF8BinTables($this->connection); |
|
| 79 | + foreach ($tables as $table) { |
|
| 80 | + $output->info("Change row format for $table ..."); |
|
| 81 | + $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
| 82 | + try { |
|
| 83 | + $query->execute(); |
|
| 84 | + } catch (DriverException $e) { |
|
| 85 | + // Just log this |
|
| 86 | + $this->logger->logException($e); |
|
| 87 | + if (!$this->ignoreFailures) { |
|
| 88 | + throw $e; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $output->info("Change collation for $table ..."); |
|
| 93 | + if ($characterSet === 'utf8mb4') { |
|
| 94 | + // need to set row compression first |
|
| 95 | + $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT=COMPRESSED;'); |
|
| 96 | + $query->execute(); |
|
| 97 | + } |
|
| 98 | + $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
| 99 | + try { |
|
| 100 | + $query->execute(); |
|
| 101 | + } catch (DriverException $e) { |
|
| 102 | + // Just log this |
|
| 103 | + $this->logger->logException($e); |
|
| 104 | + if (!$this->ignoreFailures) { |
|
| 105 | + throw $e; |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + if (empty($tables)) { |
|
| 110 | + $output->info('All tables already have the correct collation -> nothing to do'); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param IDBConnection $connection |
|
| 116 | + * @return string[] |
|
| 117 | + */ |
|
| 118 | + protected function getAllNonUTF8BinTables(IDBConnection $connection) { |
|
| 119 | + $dbName = $this->config->getSystemValue("dbname"); |
|
| 120 | + $characterSet = $this->config->getSystemValue('mysql.utf8mb4', false) ? 'utf8mb4' : 'utf8'; |
|
| 121 | + |
|
| 122 | + // fetch tables by columns |
|
| 123 | + $statement = $connection->executeQuery( |
|
| 124 | + "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 125 | + " FROM INFORMATION_SCHEMA . COLUMNS" . |
|
| 126 | + " WHERE TABLE_SCHEMA = ?" . |
|
| 127 | + " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
| 128 | + " AND TABLE_NAME LIKE '*PREFIX*%'", |
|
| 129 | + array($dbName) |
|
| 130 | + ); |
|
| 131 | + $rows = $statement->fetchAll(); |
|
| 132 | + $result = []; |
|
| 133 | + foreach ($rows as $row) { |
|
| 134 | + $result[$row['table']] = true; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + // fetch tables by collation |
|
| 138 | + $statement = $connection->executeQuery( |
|
| 139 | + "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 140 | + " FROM INFORMATION_SCHEMA . TABLES" . |
|
| 141 | + " WHERE TABLE_SCHEMA = ?" . |
|
| 142 | + " AND TABLE_COLLATION <> '" . $characterSet . "_bin'" . |
|
| 143 | + " AND TABLE_NAME LIKE '*PREFIX*%'", |
|
| 144 | + [$dbName] |
|
| 145 | + ); |
|
| 146 | + $rows = $statement->fetchAll(); |
|
| 147 | + foreach ($rows as $row) { |
|
| 148 | + $result[$row['table']] = true; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return array_keys($result); |
|
| 152 | + } |
|
| 153 | 153 | } |
| 154 | 154 | |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | $tables = $this->getAllNonUTF8BinTables($this->connection); |
| 79 | 79 | foreach ($tables as $table) { |
| 80 | 80 | $output->info("Change row format for $table ..."); |
| 81 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT = DYNAMIC;'); |
|
| 81 | + $query = $this->connection->prepare('ALTER TABLE `'.$table.'` ROW_FORMAT = DYNAMIC;'); |
|
| 82 | 82 | try { |
| 83 | 83 | $query->execute(); |
| 84 | 84 | } catch (DriverException $e) { |
@@ -92,10 +92,10 @@ discard block |
||
| 92 | 92 | $output->info("Change collation for $table ..."); |
| 93 | 93 | if ($characterSet === 'utf8mb4') { |
| 94 | 94 | // need to set row compression first |
| 95 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` ROW_FORMAT=COMPRESSED;'); |
|
| 95 | + $query = $this->connection->prepare('ALTER TABLE `'.$table.'` ROW_FORMAT=COMPRESSED;'); |
|
| 96 | 96 | $query->execute(); |
| 97 | 97 | } |
| 98 | - $query = $this->connection->prepare('ALTER TABLE `' . $table . '` CONVERT TO CHARACTER SET ' . $characterSet . ' COLLATE ' . $characterSet . '_bin;'); |
|
| 98 | + $query = $this->connection->prepare('ALTER TABLE `'.$table.'` CONVERT TO CHARACTER SET '.$characterSet.' COLLATE '.$characterSet.'_bin;'); |
|
| 99 | 99 | try { |
| 100 | 100 | $query->execute(); |
| 101 | 101 | } catch (DriverException $e) { |
@@ -121,10 +121,10 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | // fetch tables by columns |
| 123 | 123 | $statement = $connection->executeQuery( |
| 124 | - "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 125 | - " FROM INFORMATION_SCHEMA . COLUMNS" . |
|
| 126 | - " WHERE TABLE_SCHEMA = ?" . |
|
| 127 | - " AND (COLLATION_NAME <> '" . $characterSet . "_bin' OR CHARACTER_SET_NAME <> '" . $characterSet . "')" . |
|
| 124 | + "SELECT DISTINCT(TABLE_NAME) AS `table`". |
|
| 125 | + " FROM INFORMATION_SCHEMA . COLUMNS". |
|
| 126 | + " WHERE TABLE_SCHEMA = ?". |
|
| 127 | + " AND (COLLATION_NAME <> '".$characterSet."_bin' OR CHARACTER_SET_NAME <> '".$characterSet."')". |
|
| 128 | 128 | " AND TABLE_NAME LIKE '*PREFIX*%'", |
| 129 | 129 | array($dbName) |
| 130 | 130 | ); |
@@ -136,10 +136,10 @@ discard block |
||
| 136 | 136 | |
| 137 | 137 | // fetch tables by collation |
| 138 | 138 | $statement = $connection->executeQuery( |
| 139 | - "SELECT DISTINCT(TABLE_NAME) AS `table`" . |
|
| 140 | - " FROM INFORMATION_SCHEMA . TABLES" . |
|
| 141 | - " WHERE TABLE_SCHEMA = ?" . |
|
| 142 | - " AND TABLE_COLLATION <> '" . $characterSet . "_bin'" . |
|
| 139 | + "SELECT DISTINCT(TABLE_NAME) AS `table`". |
|
| 140 | + " FROM INFORMATION_SCHEMA . TABLES". |
|
| 141 | + " WHERE TABLE_SCHEMA = ?". |
|
| 142 | + " AND TABLE_COLLATION <> '".$characterSet."_bin'". |
|
| 143 | 143 | " AND TABLE_NAME LIKE '*PREFIX*%'", |
| 144 | 144 | [$dbName] |
| 145 | 145 | ); |