Skip to content

Commit f3ee7a4

Browse files
authored
Merge pull request #1322 from nextcloud/fix/repair-duplicate-face-detections
fix: Repair duplicate face detections
2 parents c25cc96 + 9685e02 commit f3ee7a4

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

appinfo/info.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ The app does not send any sensitive data to cloud providers or similar services.
101101
<repair-steps>
102102
<post-migration>
103103
<step>OCA\Recognize\Migration\InstallDeps</step>
104-
<step>OCA\Recognize\Migration\RemoveDuplicateFaceDetections</step>
105104
</post-migration>
105+
<live-migration>
106+
<step>OCA\Recognize\Migration\RemoveDuplicateFaceDetections</step>
107+
</live-migration>
106108
<install>
107109
<step>OCA\Recognize\Migration\InstallDeps</step>
108110
</install>

lib/Migration/RemoveDuplicateFaceDetections.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ public function getName(): string {
4444
public function run(IOutput $output): void {
4545
try {
4646
$subQuery = $this->db->getQueryBuilder();
47-
$subQuery->select($subQuery->func()->min('id'))
47+
$subQuery->selectAlias($subQuery->func()->min('id'), 'id')
4848
->from('recognize_face_detections')
4949
->groupBy('file_id', 'user_id', 'x', 'y', 'height', 'width');
5050

51+
if ($this->db->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL) {
52+
$secondSubQuery = $this->db->getQueryBuilder();
53+
$secondSubQuery->select('id')->from($secondSubQuery->createFunction('(' . $subQuery->getSQL() .')'), 'x');
54+
$sql = $secondSubQuery->getSQL();
55+
} else {
56+
$sql = $subQuery->getSQL();
57+
}
58+
5159
$qb = $this->db->getQueryBuilder();
5260
$qb->delete('recognize_face_detections')
53-
->where($qb->expr()->notIn('id', $qb->createFunction('(' . $subQuery->getSQL() .')')));
61+
->where($qb->expr()->notIn('id', $qb->createFunction('(' . $sql .')')));
5462

5563
$qb->executeStatement();
5664
} catch (\Throwable $e) {

tests/RemoveDuplicateFaceDetectionsTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ public function setUp(): void {
2222
$qb = $this->db->getQueryBuilder();
2323
$qb->delete('recognize_face_detections')->executeStatement();
2424

25-
// Generate 11 face detections per file (1000 files per user; 100 users)
26-
// = 1.100.000 face detections out of which 900.000 are superfluous duplicates to be removed
27-
// After the repair step there should be 200.000 left
28-
for ($k = 0; $k < 100; $k++) {
29-
for ($j = 0; $j < 1000; $j++) {
25+
for ($k = 0; $k < 10; $k++) {
26+
for ($j = 0; $j < 10; $j++) {
3027
$user = 'user' . $k;
3128
$x = rand(0, 100) / 100;
3229
$y = rand(0, 100) / 100;
@@ -66,14 +63,14 @@ public function testRepairStep() : void {
6663
// Check
6764
$qb = $this->db->getQueryBuilder();
6865
$count = $qb->select($qb->func()->count('*'))->from('recognize_face_detections')->executeQuery()->fetchOne();
69-
$this->assertEquals(1100000, (int)$count);
66+
$this->assertEquals(10 * 10 * 11, (int)$count); // 1100
7067

7168
// Run
7269
$repairStep->run($output);
7370

7471
// Assert
7572
$qb = $this->db->getQueryBuilder();
7673
$count = $qb->select($qb->func()->count('*'))->from('recognize_face_detections')->executeQuery()->fetchOne();
77-
$this->assertEquals(200000, (int)$count);
74+
$this->assertEquals(10 * 10 * 11 - 10 * 10 * 9, (int)$count); // 200
7875
}
7976
}

0 commit comments

Comments
 (0)