Skip to content

Commit ba25daf

Browse files
committed
fix: Migrate existing bg jobs to use sha256
Signed-off-by: Louis Chemineau <louis@chmn.me> [skip ci] Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent f4fc57c commit ba25daf

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@
12321232
'OC\\Core\\Migrations\\Version28000Date20231004103301' => $baseDir . '/core/Migrations/Version28000Date20231004103301.php',
12331233
'OC\\Core\\Migrations\\Version28000Date20231103104802' => $baseDir . '/core/Migrations/Version28000Date20231103104802.php',
12341234
'OC\\Core\\Migrations\\Version28000Date20231126110901' => $baseDir . '/core/Migrations/Version28000Date20231126110901.php',
1235+
'OC\\Core\\Migrations\\Version28000Date20240828142927' => $baseDir . '/core/Migrations/Version28000Date20240828142927.php',
12351236
'OC\\Core\\Migrations\\Version30000Date20240814180800' => $baseDir . '/core/Migrations/Version30000Date20240814180800.php',
12361237
'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
12371238
'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12651265
'OC\\Core\\Migrations\\Version28000Date20231004103301' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231004103301.php',
12661266
'OC\\Core\\Migrations\\Version28000Date20231103104802' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231103104802.php',
12671267
'OC\\Core\\Migrations\\Version28000Date20231126110901' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20231126110901.php',
1268+
'OC\\Core\\Migrations\\Version28000Date20240828142927' => __DIR__ . '/../../..' . '/core/Migrations/Version28000Date20240828142927.php',
12681269
'OC\\Core\\Migrations\\Version30000Date20240814180800' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240814180800.php',
12691270
'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
12701271
'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',

0 commit comments

Comments
 (0)