|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC\Core\Migrations; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCP\DB\ISchemaWrapper; |
| 14 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 15 | +use OCP\IDBConnection; |
| 16 | + |
| 17 | +/** |
| 18 | + * Migrate the argument_hash column of oc_jobs to use sha256 instead of md5. |
| 19 | + */ |
| 20 | +class Version28000Date20240828142927 extends SimpleMigrationStep { |
| 21 | + public function __construct( |
| 22 | + protected IDBConnection $connection, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 27 | + /** @var ISchemaWrapper $schema */ |
| 28 | + $schema = $schemaClosure(); |
| 29 | + |
| 30 | + // Increase the column size from 32 to 64 |
| 31 | + $table = $schema->getTable('jobs'); |
| 32 | + $table->modifyColumn('argument_hash', [ |
| 33 | + 'notnull' => false, |
| 34 | + 'length' => 64, |
| 35 | + ]); |
| 36 | + |
| 37 | + return $schema; |
| 38 | + } |
| 39 | + |
| 40 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 41 | + $chunkSize = 1000; |
| 42 | + $offset = 0; |
| 43 | + $nullHash = hash('sha256', 'null'); |
| 44 | + |
| 45 | + $selectQuery = $this->connection->getQueryBuilder() |
| 46 | + ->select('*') |
| 47 | + ->from('jobs') |
| 48 | + ->setMaxResults($chunkSize); |
| 49 | + |
| 50 | + $insertQuery = $this->connection->getQueryBuilder(); |
| 51 | + $insertQuery->update('jobs') |
| 52 | + ->set('argument_hash', $insertQuery->createParameter('argument_hash')) |
| 53 | + ->where($insertQuery->expr()->eq('id', $insertQuery->createParameter('id'))); |
| 54 | + |
| 55 | + do { |
| 56 | + $result = $selectQuery |
| 57 | + ->setFirstResult($offset) |
| 58 | + ->executeQuery(); |
| 59 | + |
| 60 | + $jobs = $result->fetchAll(); |
| 61 | + $count = count($jobs); |
| 62 | + |
| 63 | + foreach ($jobs as $jobRow) { |
| 64 | + if ($jobRow['argument'] === 'null') { |
| 65 | + $hash = $nullHash; |
| 66 | + } else { |
| 67 | + $hash = hash('sha256', $jobRow['argument']); |
| 68 | + } |
| 69 | + $insertQuery->setParameter('id', (string)$jobRow['id'], IQueryBuilder::PARAM_INT); |
| 70 | + $insertQuery->setParameter('argument_hash', $hash); |
| 71 | + $insertQuery->executeStatement(); |
| 72 | + } |
| 73 | + |
| 74 | + $offset += $chunkSize; |
| 75 | + } while ($count === $chunkSize); |
| 76 | + } |
| 77 | +} |
0 commit comments