-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Migrate existing bg jobs to use sha256
Signed-off-by: Louis Chemineau <louis@chmn.me>
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Core\Migrations; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
|
||
/** | ||
* Migrate the argument_hash column of oc_jobs to use sha256 instead of md5. | ||
*/ | ||
class Version28000Date20240828142927 extends SimpleMigrationStep { | ||
Check failure on line 20 in core/Migrations/Version28000Date20240828142927.php GitHub Actions / static-code-analysisUndefinedClass
Check failure Code scanning / Psalm UndefinedClass Error
Class, interface or enum named OC\Core\Migrations\SimpleMigrationStep does not exist
|
||
public function __construct( | ||
protected IDBConnection $connection, | ||
) { | ||
} | ||
|
||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
// Increase the column size from 32 to 64 | ||
$table = $schema->getTable('jobs'); | ||
$table->modifyColumn('argument_hash', [ | ||
'notnull' => false, | ||
'length' => 64, | ||
]); | ||
|
||
return $schema; | ||
} | ||
|
||
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
$chunkSize = 1000; | ||
$offset = 0; | ||
$nullHash = hash('sha256', 'null'); | ||
|
||
$selectQuery = $this->connection->getQueryBuilder() | ||
->select('*') | ||
->from('jobs') | ||
->setMaxResults($chunkSize); | ||
|
||
$insertQuery = $this->connection->getQueryBuilder(); | ||
$insertQuery->update('jobs') | ||
->set('argument_hash', $insertQuery->createParameter('argument_hash')) | ||
->where($insertQuery->expr()->eq('id', $insertQuery->createParameter('id'))); | ||
|
||
do { | ||
$result = $selectQuery | ||
->setFirstResult($offset) | ||
->executeQuery(); | ||
|
||
$jobs = $result->fetchAll(); | ||
$count = count($jobs); | ||
|
||
foreach ($jobs as $jobRow) { | ||
if ($jobRow['argument'] === 'null') { | ||
$hash = $nullHash; | ||
} else { | ||
$hash = hash('sha256', $jobRow['argument']); | ||
} | ||
$insertQuery->setParameter('id', (string)$jobRow['id'], IQueryBuilder::PARAM_INT); | ||
$insertQuery->setParameter('argument_hash', $hash); | ||
$insertQuery->executeStatement(); | ||
} | ||
|
||
$offset += $chunkSize; | ||
} while ($count === $chunkSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters