Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.woltlab.wcf/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

<!--
Required order of the following steps for the update to 6.2:
<instruction type="script">acp/update_com.woltlab.wcf_6.2_backgroundJob.php</instruction>
<instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_62_step1.php</instruction>
<instruction type="script">acp/update_com.woltlab.wcf_6.2_contactOptions.php</instruction>
<instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_62_step2.php</instruction>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@

use wcf\system\database\table\column\IntDatabaseTableColumn;
use wcf\system\database\table\column\MediumtextDatabaseTableColumn;
use wcf\system\database\table\column\TextDatabaseTableColumn;
use wcf\system\database\table\column\TinyintDatabaseTableColumn;
use wcf\system\database\table\index\DatabaseTableForeignKey;
use wcf\system\database\table\index\DatabaseTableIndex;
use wcf\system\database\table\PartialDatabaseTable;

return [
PartialDatabaseTable::create('wcf1_background_job')
->indices([
DatabaseTableIndex::create('identifier')
->type(DatabaseTableIndex::UNIQUE_TYPE)
->columns(['identifier']),
]),
PartialDatabaseTable::create('wcf1_user')
->columns([
IntDatabaseTableColumn::create('avatarFileID')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This script removes duplicate background jobs.
*/

use wcf\system\WCF;

$sql = "DELETE background_jobs
FROM wcf1_background_job background_jobs
JOIN (
SELECT MIN(jobID) as keepID, identifier
FROM wcf1_background_job
WHERE identifier IS NOT NULL
GROUP BY identifier
HAVING COUNT(*) > 1
) AS duplicates
ON background_jobs.identifier = duplicates.identifier
WHERE background_jobs.jobID <> duplicates.keepID";
$statement = WCF::getDB()->prepare($sql);
$statement->execute();
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use wcf\data\user\User;
use wcf\system\background\job\AbstractBackgroundJob;
use wcf\system\background\job\AbstractUniqueBackgroundJob;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\ParentClassException;
use wcf\system\session\SessionHandler;
use wcf\system\SingletonFactory;
Expand Down Expand Up @@ -73,48 +72,17 @@ public function enqueueAt(AbstractBackgroundJob|array $jobs, int $time): void
$jobs = [$jobs];
}

$identifiers = [];
foreach ($jobs as $job) {
if (!($job instanceof AbstractBackgroundJob)) {
throw new ParentClassException(\get_class($job), AbstractBackgroundJob::class);
}

if ($job instanceof AbstractUniqueBackgroundJob) {
$identifiers[] = $job->identifier();
}
}

if ($identifiers !== []) {
$conditions = new PreparedStatementConditionBuilder();
$conditions->add("identifier IN (?)", [$identifiers]);

$sql = "SELECT DISTINCT identifier
FROM wcf1_background_job
{$conditions}";
$statement = WCF::getDB()->prepare($sql);
$statement->execute($conditions->getParameters());
$existingJobs = $statement->fetchAll(\PDO::FETCH_COLUMN);

$jobs = \array_filter(
$jobs,
function ($job) use ($existingJobs) {
if ($job instanceof AbstractUniqueBackgroundJob && \in_array($job->identifier(), $existingJobs)) {
return false;
}

return true;
}
);

if ($jobs === []) {
return;
}
}

$sql = "INSERT INTO wcf1_background_job
(job, time, identifier)
VALUES (?, ?, ?)";
$sql = "INSERT IGNORE INTO wcf1_background_job
(job, time, identifier)
VALUES (?, ?, ?)";
$statement = WCF::getDB()->prepare($sql);

foreach ($jobs as $job) {
$identifier = null;
if ($job instanceof AbstractUniqueBackgroundJob) {
Expand Down
2 changes: 1 addition & 1 deletion wcfsetup/setup/db/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ CREATE TABLE wcf1_background_job (
time INT(10) NOT NULL,
identifier VARCHAR(191) NULL,

KEY identifier (identifier),
UNIQUE KEY identifier (identifier),
KEY (status, time)
);

Expand Down
Loading