Completed
Pull Request — master (#31014)
by Jörn Friedrich
10:57
created

RepairOrphanedSubshare   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 62
rs 10
wmc 3
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getName() 0 3 1
B run() 0 31 1
1
<?php
2
/**
3
 * @author Sujith Haridasan <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Repair;
23
24
use OCP\DB\QueryBuilder\IQueryBuilder;
25
use OCP\IDBConnection;
26
use OCP\Migration\IOutput;
27
use OCP\Migration\IRepairStep;
28
29
class RepairOrphanedSubshare implements IRepairStep {
30
31
	/** @var IDBConnection  */
32
	private $connection;
33
34
	/**
35
	 * RepairOrphanedSubshare constructor.
36
	 *
37
	 * @param IDBConnection $connection
38
	 */
39
	public function __construct(IDBConnection $connection) {
40
		$this->connection = $connection;
41
	}
42
43
	/**
44
	 * Returns the step's name
45
	 *
46
	 * @return string
47
	 */
48
	public function getName() {
49
		return 'Repair orphaned reshare';
50
	}
51
52
	/**
53
	 * Run repair step.
54
	 * Must throw exception on error.
55
	 *
56
	 * @param IOutput $output
57
	 * @throws \Exception in case of failure
58
	 */
59
	public function run(IOutput $output) {
60
61
		/**
62
			DELETE FROM share
63
			WHERE parent NOT IN (
64
				SELECT id FROM (
65
					SELECT id FROM share
66
					ORDER BY id -- to prevent derived table merge
67
				) mysqlerr1093hack
68
			)
69
		 */
70
71
		// subselect for all share ids
72
		$allShares = $this->connection->getQueryBuilder()
73
			->select('id')
74
			->from('share')
75
			->orderBy('id'); // to prevent derived table merge, see https://mariadb.com/kb/en/library/derived-table-merge-optimization/#factsheet
76
77
		// TODO this subquery currently cannot be built with our doctrine wrapper
78
		// TODO make the mysql hack optional? might also be needed for oracle
79
		$subquery = "SELECT `id` FROM ({$allShares->getSQL()}) mysqlerr1093hack";
80
81
		//This delete query deletes orphan shares whose parents are missing
82
		$deleteOrphanReshares = $this->connection->getQueryBuilder();
83
		$deleteOrphanReshares
84
			->delete('share')
85
			->where($deleteOrphanReshares->expr()->notIn('parent',
86
				$deleteOrphanReshares->createFunction($subquery)));
87
88
		$deleteOrphanReshares->execute();
89
	}
90
}
91