|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2023 Louis Chemineau <louis@chmn.me> |
| 6 | + * |
| 7 | + * @author Louis Chemineau <louis@chmn.me> |
| 8 | + * |
| 9 | + * @license AGPL-3.0-or-later |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Affero General Public License as |
| 13 | + * published by the Free Software Foundation, either version 3 of the |
| 14 | + * License, or (at your option) any later version. |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Affero General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License |
| 22 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +namespace OC\Core\BackgroundJobs; |
| 27 | + |
| 28 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 29 | +use OCP\BackgroundJob\IJobList; |
| 30 | +use OCP\BackgroundJob\TimedJob; |
| 31 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 32 | +use OCP\IDBConnection; |
| 33 | + |
| 34 | +// Migrate oc_file_metadata.metadata to oc_file_metadata.value. |
| 35 | +// This was previously done in a migration, but it is taking to much time in large instances. |
| 36 | +// This job will progressively migrate the data 1 hour per night every night. |
| 37 | +// Once done, it will remove itself from the job list and drop oc_file_metadata.metadata. |
| 38 | +class MetadataMigrationJob extends TimedJob { |
| 39 | + public function __construct( |
| 40 | + ITimeFactory $time, |
| 41 | + private IDBConnection $db, |
| 42 | + private IJobList $jobList, |
| 43 | + ) { |
| 44 | + parent::__construct($time); |
| 45 | + |
| 46 | + $this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE); |
| 47 | + $this->setInterval(24 * 3600); |
| 48 | + } |
| 49 | + |
| 50 | + protected function run(mixed $argument): void { |
| 51 | + if (!$this->db->createSchema()->getTable('oc_file_metadata')->hasColumn('metadata')) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $updateQuery = $this->db->getQueryBuilder(); |
| 56 | + $updateQuery->update('file_metadata') |
| 57 | + ->set('value', $updateQuery->createParameter('value')) |
| 58 | + ->set('metadata', $updateQuery->createParameter('metadata')) |
| 59 | + ->where($updateQuery->expr()->eq('id', $updateQuery->createParameter('id'))) |
| 60 | + ->andWhere($updateQuery->expr()->eq('group_name', $updateQuery->createParameter('group_name'))); |
| 61 | + |
| 62 | + $selectQuery = $this->db->getQueryBuilder(); |
| 63 | + $selectQuery->select('id', 'group_name', 'metadata') |
| 64 | + ->from('file_metadata') |
| 65 | + ->where($selectQuery->expr()->neq('metadata', $selectQuery->createNamedParameter(''), IQueryBuilder::PARAM_STR)) |
| 66 | + ->orderBy('id', 'ASC') |
| 67 | + ->setMaxResults(1000); |
| 68 | + |
| 69 | + $movedRows = 0; |
| 70 | + $startTime = time(); |
| 71 | + |
| 72 | + do { |
| 73 | + // Stop if execution time is more than one hour. |
| 74 | + if (time() - $startTime > 3600) { |
| 75 | + return; |
| 76 | + } |
| 77 | + $movedRows = $this->chunkedCopying($updateQuery, $selectQuery); |
| 78 | + } while ($movedRows !== 0); |
| 79 | + |
| 80 | + |
| 81 | + $this->jobList->remove(MetadataMigrationJob::class); |
| 82 | + } |
| 83 | + |
| 84 | + protected function chunkedCopying(IQueryBuilder $updateQuery, IQueryBuilder $selectQuery): int { |
| 85 | + $this->db->beginTransaction(); |
| 86 | + |
| 87 | + $results = $selectQuery->executeQuery(); |
| 88 | + |
| 89 | + while ($row = $results->fetch()) { |
| 90 | + $updateQuery |
| 91 | + ->setParameter('id', (int)$row['id']) |
| 92 | + ->setParameter('group_name', $row['group_name']) |
| 93 | + ->setParameter('value', $row['metadata']) |
| 94 | + ->setParameter('metadata', '') |
| 95 | + ->executeStatement(); |
| 96 | + } |
| 97 | + |
| 98 | + $results->closeCursor(); |
| 99 | + $this->db->commit(); |
| 100 | + |
| 101 | + return $results->rowCount(); |
| 102 | + } |
| 103 | +} |
0 commit comments