1
0

Version28000Date20240828142927.php 2.0 KB

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