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
10 changes: 10 additions & 0 deletions src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public function count(Query $query): int
return $result;
}

public function purge(Query $query): void
{
$qb = $this->connection->createQueryBuilder();
$qb->delete($this->table);

[$queryParameters, $queryTypes] = $this->addWheres($query, $qb);

$this->connection->executeStatement($qb->getSQL(), $queryParameters, $queryTypes);
}

private function getSchema(): Schema
{
$schema = new Schema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,21 @@ public function testCountIgnoresLimit(): void
self::assertSame(4, $storage->count($query));
}

public function testPurge(): void
{
$storage = $this->createStorage();
$storage->setup();
$this->loadFixtures($storage);

// limit is ignored by purge — all "import" executions must be deleted (3 total)
$storage->purge((new QueryBuilder())->jobs(['import'])->limit(1, 0)->getQuery());

self::assertExecutions(
[['export', '123']],
$storage->query((new QueryBuilder())->getQuery()),
);
}

public static function assertExecutionIds(array $ids, iterable $executions): void
{
$actualIds = [];
Expand Down
7 changes: 7 additions & 0 deletions src/batch/src/Storage/FilesystemJobExecutionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public function count(Query $query): int
return \count($jobExecutions);
}

public function purge(Query $query): void
{
foreach ($this->rawQuery($query) as $execution) {
$this->remove($execution);
}
}

/**
* @return list<JobExecution>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public function query(Query $query): iterable;
* Note: limit and offset from the query are ignored — all matching executions are counted.
*/
public function count(Query $query): int;

/**
* Delete all job executions matching the given query.
* Note: limit and offset from the query are ignored — all matching executions are deleted.
*/
public function purge(Query $query): void;
}
26 changes: 26 additions & 0 deletions src/batch/tests/Storage/FilesystemJobExecutionStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,30 @@ public function testRemoveFileNotWritable(): void
$jobExecution = JobExecution::createRoot('123456789', 'export');
$this->createStorage(self::READONLY_STORAGE_DIR)->remove($jobExecution);
}

public function testPurge(): void
{
$storage = $this->createStorage(
self::STORAGE_DIR . '/purge',
new JsonJobExecutionSerializer(new InMemoryJobExecutionLoggerFactory()),
);

foreach (['20210920', '20210922'] as $id) {
$storage->store(JobExecution::createRoot($id, 'export'));
}
foreach (['20210910', '20210915', '20210920'] as $id) {
$storage->store(JobExecution::createRoot($id, 'list'));
}

// limit is ignored by purge — all 3 "list" executions must be deleted
$storage->purge((new QueryBuilder())->jobs(['list'])->limit(1, 0)->getQuery());

self::assertExecutions(
[
['export', '20210920'],
['export', '20210922'],
],
$storage->query((new QueryBuilder())->getQuery()),
);
}
}
Loading