|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2023 Louis Chmn <louis@chmn.me> |
| 7 | + * |
| 8 | + * @author Louis Chmn <louis@chmn.me> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 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 |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OC\Core\Migrations; |
| 28 | + |
| 29 | +use Closure; |
| 30 | +use OCP\DB\ISchemaWrapper; |
| 31 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 32 | +use OCP\DB\Types; |
| 33 | +use OCP\IDBConnection; |
| 34 | +use OCP\Migration\IOutput; |
| 35 | +use OCP\Migration\SimpleMigrationStep; |
| 36 | + |
| 37 | +/** |
| 38 | + * Migrate oc_file_metadata.metadata as JSON type to oc_file_metadata.value a STRING type |
| 39 | + * @see \OC\Metadata\FileMetadata |
| 40 | + */ |
| 41 | +class Version27000Date20230309104325 extends SimpleMigrationStep { |
| 42 | + public function __construct( |
| 43 | + private IDBConnection $connection |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param IOutput $output |
| 49 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 50 | + * @param array $options |
| 51 | + * @return null|ISchemaWrapper |
| 52 | + */ |
| 53 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 54 | + /** @var ISchemaWrapper $schema */ |
| 55 | + $schema = $schemaClosure(); |
| 56 | + $metadataTable = $schema->getTable('file_metadata'); |
| 57 | + |
| 58 | + if ($metadataTable->hasColumn('value')) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + $metadataTable->addColumn('value', Types::STRING, [ |
| 63 | + 'notnull' => true, |
| 64 | + ]); |
| 65 | + return $schema; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * @param IOutput $output |
| 71 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 72 | + * @param array $options |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 76 | + /** @var ISchemaWrapper $schema */ |
| 77 | + $schema = $schemaClosure(); |
| 78 | + $metadataTable = $schema->getTable('file_metadata'); |
| 79 | + |
| 80 | + if (!$metadataTable->hasColumn('metadata')) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + $insertQuery = $this->connection->getQueryBuilder(); |
| 85 | + $insertQuery->insert('file_metadata') |
| 86 | + ->where($insertQuery->expr()->eq('id', $insertQuery->createParameter('id'))) |
| 87 | + ->values(['value' => $insertQuery->createParameter('value')]); |
| 88 | + |
| 89 | + $selectQuery = $this->connection->getQueryBuilder(); |
| 90 | + $selectQuery->select('id, metadata') |
| 91 | + ->from('file_metadata') |
| 92 | + ->orderBy('id', 'ASC') |
| 93 | + ->setMaxResults(1000); |
| 94 | + |
| 95 | + $offset = 0; |
| 96 | + $movedRows = 0; |
| 97 | + do { |
| 98 | + $movedRows = $this->chunkedCopying($insertQuery, $selectQuery, $offset); |
| 99 | + $offset += $movedRows; |
| 100 | + } while ($movedRows !== 0); |
| 101 | + } |
| 102 | + |
| 103 | + protected function chunkedCopying(IQueryBuilder $insertQuery, IQueryBuilder $selectQuery, int $offset): int { |
| 104 | + $this->connection->beginTransaction(); |
| 105 | + |
| 106 | + $results = $selectQuery |
| 107 | + ->setFirstResult($offset) |
| 108 | + ->executeQuery(); |
| 109 | + |
| 110 | + while ($row = $results->fetch()) { |
| 111 | + $insertQuery |
| 112 | + ->setParameter('id', (int)$row['id']) |
| 113 | + ->setParameter('value', (int)$row['metadata']) |
| 114 | + ->executeStatement(); |
| 115 | + } |
| 116 | + |
| 117 | + $results->closeCursor(); |
| 118 | + $this->connection->commit(); |
| 119 | + |
| 120 | + return $results->rowCount(); |
| 121 | + } |
| 122 | +} |
0 commit comments