Skip to content

Commit e9b96c9

Browse files
committed
fixup! fix(dav): move orphan cleaning logic to a chunked background job
1 parent 694487c commit e9b96c9

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

apps/dav/lib/BackgroundJob/CleanupOrphanedChildrenJob.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ private function cleanUpOrphans(
7474
$result = $selectQb->executeQuery();
7575
$rows = $result->fetchAll();
7676
$result->closeCursor();
77+
if (empty($rows)) {
78+
return 0;
79+
}
7780

7881
$orphanItems = array_map(static fn ($row) => $row['id'], $rows);
79-
if (!empty($orphanItems)) {
80-
$deleteQb = $this->connection->getQueryBuilder();
81-
$deleteQb->delete($childTable)
82-
->where($deleteQb->expr()->in('id', $deleteQb->createNamedParameter($orphanItems, IQueryBuilder::PARAM_INT_ARRAY)));
83-
$deleteQb->executeStatement();
84-
}
82+
$deleteQb = $this->connection->getQueryBuilder();
83+
$deleteQb->delete($childTable)
84+
->where($deleteQb->expr()->in('id', $deleteQb->createNamedParameter($orphanItems, IQueryBuilder::PARAM_INT_ARRAY)));
85+
$deleteQb->executeStatement();
8586

8687
return count($orphanItems);
8788
}

apps/dav/tests/unit/BackgroundJob/CleanupOrphanedChildrenJobTest.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,42 @@ private function getMockQueryBuilder(): IQueryBuilder&MockObject {
7777

7878
public function testRunWithoutOrphans(): void {
7979
$argument = $this->getArgument();
80-
$qb = $this->getMockQueryBuilder();
80+
$selectQb = $this->getMockQueryBuilder();
8181
$result = $this->createMock(IResult::class);
8282

8383
$this->connection->expects(self::once())
8484
->method('getQueryBuilder')
85-
->willReturn($qb);
86-
$qb->expects(self::once())
85+
->willReturn($selectQb);
86+
$selectQb->expects(self::once())
8787
->method('executeQuery')
8888
->willReturn($result);
8989
$result->expects(self::once())
9090
->method('fetchAll')
9191
->willReturn([]);
9292
$result->expects(self::once())
9393
->method('closeCursor');
94-
$qb->expects(self::never())
95-
->method('delete');
96-
$qb->expects(self::never())
97-
->method('executeStatement');
9894
$this->jobList->expects(self::never())
9995
->method('add');
10096

10197
self::invokePrivate($this->job, 'run', [$argument]);
10298
}
10399

104100
public function testRunWithPartialBatch(): void {
105-
$batch = 0;
106101
$argument = $this->getArgument();
107-
$qb = $this->getMockQueryBuilder();
102+
$selectQb = $this->getMockQueryBuilder();
103+
$deleteQb = $this->getMockQueryBuilder();
108104
$result = $this->createMock(IResult::class);
109105

110-
$this->connection->expects(self::once())
106+
$qbInvocationCount = self::exactly(2);
107+
$this->connection->expects($qbInvocationCount)
111108
->method('getQueryBuilder')
112-
->willReturn($qb);
113-
$qb->expects(self::once())
109+
->willReturnCallback(function () use ($qbInvocationCount, $selectQb, $deleteQb) {
110+
return match ($qbInvocationCount->getInvocationCount()) {
111+
1 => $selectQb,
112+
2 => $deleteQb,
113+
};
114+
});
115+
$selectQb->expects(self::once())
114116
->method('executeQuery')
115117
->willReturn($result);
116118
$result->expects(self::once())
@@ -121,10 +123,10 @@ public function testRunWithPartialBatch(): void {
121123
]);
122124
$result->expects(self::once())
123125
->method('closeCursor');
124-
$qb->expects(self::once())
126+
$deleteQb->expects(self::once())
125127
->method('delete')
126128
->willReturnSelf();
127-
$qb->expects(self::once())
129+
$deleteQb->expects(self::once())
128130
->method('executeStatement');
129131
$this->jobList->expects(self::never())
130132
->method('add');
@@ -133,26 +135,32 @@ public function testRunWithPartialBatch(): void {
133135
}
134136

135137
public function testRunWithFullBatch(): void {
136-
$batch = 0;
137138
$argument = $this->getArgument();
138-
$qb = $this->getMockQueryBuilder();
139+
$selectQb = $this->getMockQueryBuilder();
140+
$deleteQb = $this->getMockQueryBuilder();
139141
$result = $this->createMock(IResult::class);
140142

141-
$this->connection->expects(self::once())
143+
$qbInvocationCount = self::exactly(2);
144+
$this->connection->expects($qbInvocationCount)
142145
->method('getQueryBuilder')
143-
->willReturn($qb);
144-
$qb->expects(self::once())
146+
->willReturnCallback(function () use ($qbInvocationCount, $selectQb, $deleteQb) {
147+
return match ($qbInvocationCount->getInvocationCount()) {
148+
1 => $selectQb,
149+
2 => $deleteQb,
150+
};
151+
});
152+
$selectQb->expects(self::once())
145153
->method('executeQuery')
146154
->willReturn($result);
147155
$result->expects(self::once())
148156
->method('fetchAll')
149157
->willReturn(array_map(static fn ($i) => ['id' => 42 + $i], range(0, 999)));
150158
$result->expects(self::once())
151159
->method('closeCursor');
152-
$qb->expects(self::once())
160+
$deleteQb->expects(self::once())
153161
->method('delete')
154162
->willReturnSelf();
155-
$qb->expects(self::once())
163+
$deleteQb->expects(self::once())
156164
->method('executeStatement');
157165
$this->jobList->expects(self::once())
158166
->method('add')

0 commit comments

Comments
 (0)