Skip to content

Commit 4c99455

Browse files
juliusknorrbackportbot[bot]
authored andcommitted
Add hashed attribute column for indexed job existence check
Signed-off-by: Julius Härtl <[email protected]>
1 parent 58b4db7 commit 4c99455

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
6+
*
7+
* @author Julius Härtl <[email protected]>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OC\Core\Migrations;
27+
28+
use Closure;
29+
use OCP\DB\ISchemaWrapper;
30+
use OCP\DB\Types;
31+
use OCP\IDBConnection;
32+
use OCP\Migration\IOutput;
33+
use OCP\Migration\SimpleMigrationStep;
34+
35+
class Version24000Date20211230140012 extends SimpleMigrationStep {
36+
/** @var IDBConnection */
37+
protected $connection;
38+
39+
public function __construct(IDBConnection $connection) {
40+
$this->connection = $connection;
41+
}
42+
43+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
44+
/** @var ISchemaWrapper $schema */
45+
$schema = $schemaClosure();
46+
47+
$table = $schema->getTable('jobs');
48+
if (!$table->hasColumn('argument_hash')) {
49+
$table->addColumn('argument_hash', Types::STRING, [
50+
'notnull' => false,
51+
'length' => 32,
52+
]);
53+
$table->addIndex(['class', 'argument_hash'], 'job_argument_hash');
54+
return $schema;
55+
}
56+
return null;
57+
}
58+
59+
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
60+
$update = $this->connection->getQueryBuilder();
61+
62+
$update->update('jobs')
63+
->set('argument_hash', $update->func()->md5('argument'));
64+
65+
$update->executeStatement();
66+
}
67+
}

lib/private/BackgroundJob/JobList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function add($job, $argument = null) {
8282
->values([
8383
'class' => $query->createNamedParameter($class),
8484
'argument' => $query->createNamedParameter($argumentJson),
85+
'argument_hash' => $query->createNamedParameter(md5($argumentJson)),
8586
'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
8687
'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT),
8788
]);
@@ -90,7 +91,7 @@ public function add($job, $argument = null) {
9091
->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
9192
->set('last_checked', $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT))
9293
->where($query->expr()->eq('class', $query->createNamedParameter($class)))
93-
->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argumentJson)));
94+
->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argumentJson))));
9495
}
9596
$query->executeStatement();
9697
}
@@ -145,7 +146,7 @@ public function has($job, $argument) {
145146
$query->select('id')
146147
->from('jobs')
147148
->where($query->expr()->eq('class', $query->createNamedParameter($class)))
148-
->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)))
149+
->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argument))))
149150
->setMaxResults(1);
150151

151152
$result = $query->execute();

0 commit comments

Comments
 (0)