Issues (27)

lib/Migration/RemoveIndices.php (2 issues)

Labels
Severity
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
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
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
	/**
44
	 * @return void
45
	 */
46
	public function run(IOutput $output) {
47
		$this->removeUniqueIndices('polls_options');
48
		$this->removeUniqueIndices('polls_log');
49
		$this->removeUniqueIndices('polls_notif');
50
		$this->removeUniqueIndices('polls_share');
51
		$this->removeUniqueIndices('polls_votes');
52
		$this->removeUniqueIndices('polls_preferences');
53
		$this->removeUniqueIndices('polls_preferences');
54
		$this->removeUniqueIndices('polls_watch');
55
	}
56
57
	/**
58
	 * 	 * remove an index with $indexName from $table
59
	 *
60
	 * @return void
61
	 */
62
	private function removeIndex(string $tableName, string $indexName): void {
63
		$schema = new SchemaWrapper($this->connection);
64
		if ($schema->hasTable($tableName)) {
65
			$table = $schema->getTable($tableName);
66
			if ($table->hasIndex($indexName)) {
67
				$table->dropIndex($indexName);
68
				$this->connection->migrateToSchema($schema->getWrappedSchema());
69
			}
70
		}
71
	}
72
73
	/**
74
	 * 	 * remove all UNIQUE indices from $table
75
	 *
76
	 * @return void
77
	 */
78
	private function removeUniqueIndices(string $tableName): void {
79
		$schema = new SchemaWrapper($this->connection);
80
		if ($schema->hasTable($tableName)) {
81
			$table = $schema->getTable($tableName);
82
			foreach ($table->getIndexes() as $index) {
83
				if (strpos($index->getName(), 'UNIQ_') === 0) {
84
					$this->removeIndex($tableName, $index->getName());
85
				}
86
			}
87
		}
88
	}
89
}
90