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

CreateIndices::__construct()   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 1
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
25
namespace OCA\Polls\Migration;
26
27
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...
28
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...
29
use OCP\Migration\IRepairStep;
30
use OCP\Migration\IOutput;
31
32
class CreateIndices implements IRepairStep {
33
34
	/** @var Connection */
35
	private $connection;
36
37
	public function __construct(Connection $connection) {
38
		$this->connection = $connection;
39
	}
40
41
	public function getName() {
42
		return 'Create polls table indices';
43
	}
44
45
	public function run(IOutput $output) {
46
		$this->createIndex('polls_options', 'UNIQ_options', ['poll_id', 'poll_option_text', 'timestamp'], true);
47
		$this->createIndex('polls_log', 'UNIQ_unprocessed', ['processed', 'poll_id', 'user_id', 'message_id'], true);
48
		$this->createIndex('polls_notif', 'UNIQ_subscription', ['poll_id', 'user_id'], true);
49
		$this->createIndex('polls_share', 'UNIQ_shares', ['poll_id', 'user_id'], true);
50
		$this->createIndex('polls_votes', 'UNIQ_votes', ['poll_id', 'user_id', 'vote_option_text'], true);
51
		$this->createIndex('polls_preferences', 'UNIQ_preferences', ['user_id'], true);
52
	}
53
54
	/**
55
	 * Create index for $table
56
	 */
57
	private function createIndex(string $tableName, string $indexName, array $columns, bool $unique = false) {
58
		$schema = new SchemaWrapper($this->connection);
59
		if ($schema->hasTable($tableName)) {
60
			$table = $schema->getTable($tableName);
61
			if (!$table->hasIndex($indexName)) {
62
				if ($unique) {
63
					$table->addUniqueIndex($columns, $indexName);
64
				} else {
65
					$table->addIndex($columns, $indexName);
66
				}
67
				$this->connection->migrateToSchema($schema->getWrappedSchema());
68
			}
69
		}
70
	}
71
}
72