Passed
Push — master ( 40f426...82e3ed )
by René
04:17 queued 11s
created

RemoveIndices::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 René Gieling <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Migration;
25
26
use OC\DB\Connection;
0 ignored issues
show
Bug introduced by
The type OC\DB\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use OC\DB\SchemaWrapper;
0 ignored issues
show
Bug introduced by
The type OC\DB\SchemaWrapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use OCP\Migration\IRepairStep;
29
use OCP\Migration\IOutput;
30
31
class RemoveIndices implements IRepairStep {
32
	/** @var Connection */
33
	private $connection;
34
35
	public function __construct(Connection $connection) {
36
		$this->connection = $connection;
37
	}
38
39
	public function getName() {
40
		return 'Remove polls table indices';
41
	}
42
43
	public function run(IOutput $output) {
44
		$this->removeUniqueIndices('polls_options');
45
		$this->removeUniqueIndices('polls_log');
46
		$this->removeUniqueIndices('polls_notif');
47
		$this->removeUniqueIndices('polls_share');
48
		$this->removeUniqueIndices('polls_votes');
49
		$this->removeUniqueIndices('polls_preferences');
50
		$this->removeUniqueIndices('polls_preferences');
51
	}
52
53
	/**
54
	 * remove an index with $indexName from $table
55
	 */
56
	private function removeIndex(string $tableName, string $indexName) {
57
		$schema = new SchemaWrapper($this->connection);
58
		if ($schema->hasTable($tableName)) {
59
			$table = $schema->getTable($tableName);
60
			if ($table->hasIndex($indexName)) {
61
				$table->dropIndex($indexName);
62
				$this->connection->migrateToSchema($schema->getWrappedSchema());
63
			}
64
		}
65
	}
66
67
	/**
68
	 * remove all UNIQUE indices from $table
69
	 */
70
	private function removeUniqueIndices(string $tableName) {
71
		$schema = new SchemaWrapper($this->connection);
72
		if ($schema->hasTable($tableName)) {
73
			$table = $schema->getTable($tableName);
74
			foreach ($table->getIndexes() as $index) {
75
				if (strpos($index->getName(), 'UNIQ_') === 0) {
76
					$this->removeIndex($tableName, $index->getName());
77
				}
78
			}
79
		}
80
	}
81
}
82