1
0

Version28000Date20240828142927.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Attributes\ColumnType;
  13. use OCP\Migration\Attributes\ModifyColumn;
  14. use OCP\Migration\IOutput;
  15. use OCP\Migration\SimpleMigrationStep;
  16. /**
  17. * Migrate the argument_hash column of oc_jobs to use sha256 instead of md5.
  18. */
  19. #[ModifyColumn(table: 'jobs', name: 'argument_hash', type: ColumnType::STRING, description: 'Increase the column size from 32 to 64')]
  20. #[ModifyColumn(table: 'jobs', name: 'argument_hash', type: ColumnType::STRING, description: 'Rehash the argument_hash column using sha256')]
  21. class Version28000Date20240828142927 extends SimpleMigrationStep {
  22. public function __construct(
  23. protected IDBConnection $connection,
  24. ) {
  25. }
  26. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
  27. /** @var ISchemaWrapper $schema */
  28. $schema = $schemaClosure();
  29. // Increase the column size from 32 to 64
  30. $table = $schema->getTable('jobs');
  31. $table->modifyColumn('argument_hash', [
  32. 'notnull' => false,
  33. 'length' => 64,
  34. ]);
  35. return $schema;
  36. }
  37. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  38. $chunkSize = 1000;
  39. $offset = 0;
  40. $nullHash = hash('sha256', 'null');
  41. $selectQuery = $this->connection->getQueryBuilder()
  42. ->select('*')
  43. ->from('jobs')
  44. ->setMaxResults($chunkSize);
  45. $insertQuery = $this->connection->getQueryBuilder();
  46. $insertQuery->update('jobs')
  47. ->set('argument_hash', $insertQuery->createParameter('argument_hash'))
  48. ->where($insertQuery->expr()->eq('id', $insertQuery->createParameter('id')));
  49. do {
  50. $result = $selectQuery
  51. ->setFirstResult($offset)
  52. ->executeQuery();
  53. $jobs = $result->fetchAll();
  54. $count = count($jobs);
  55. foreach ($jobs as $jobRow) {
  56. if ($jobRow['argument'] === 'null') {
  57. $hash = $nullHash;
  58. } else {
  59. $hash = hash('sha256', $jobRow['argument']);
  60. }
  61. $insertQuery->setParameter('id', (string)$jobRow['id'], IQueryBuilder::PARAM_INT);
  62. $insertQuery->setParameter('argument_hash', $hash);
  63. $insertQuery->executeStatement();
  64. }
  65. $offset += $chunkSize;
  66. } while ($count === $chunkSize);
  67. }
  68. }